可以使用UIScrollView、UIPageControl、NSTimer来创建,scrollview是图片的展示区域,UIPageControl是一行小圆点,用来表示当前展示的是多页面中的第几页,NSTimer用来循环执行某事件。
这里边比较重要的方法是scrollViewDidScroll,就是当scrollview滚动时计算当前pageControl的页码。
效果如下:
关于UIScrollView
contentSize是scrollView可以滚动的区域,类型为CGSize,表示尺寸。
如frame=(0,0,320,480),contentSize=(640宽,480高),表示scrollview只能左右移动,上下是不能滚动的。
contentOffSet是当前显示区域的顶点相对于frame顶点的偏移量,类型为CGPoint,表示坐标。
如上边当把scrollView滑到最右边,contentoffSet就是(320,0),也就是x偏移了320。
这里的数据源,即图片名称是写死的,格式为img_01,img_02这样的。下边是代码:
1 #define imgCount 5 2 3 @interface ViewController ()<UIScrollViewDelegate> 4 5 /** 图片展示区 */ 6 @property(nonatomic,strong) UIScrollView *imgShowView; 7 /** 图片切换按钮,UIPageControl提供一行点来指示当前显示的是多页面中的哪一页 */ 8 @property(nonatomic,strong) UIPageControl *pageControl; 9 /** 定时器 */ 10 @property(nonatomic,strong) NSTimer *imgTimer; 11 12 @end 13 14 @implementation ViewController 15 16 - (void)viewDidLoad { 17 [super viewDidLoad]; 18 19 //下面这两行代码顺序不能调换,否则代理方法不执行。因为setUpBackGroundView是实例化了一个imgShowView, 20 //若先执行self.imgShowView.delegate = self那么imgShowView是nil,空对象是不能执行方法的 21 [self setUpBackGroundView]; 22 23 self.imgShowView.delegate = self; 24 25 [self setUpTimer]; 26 27 [self setUpPageControl]; 28 } 29 30 -(void)setUpBackGroundView{ 31 //图片展示区域 32 CGFloat scrollViewW = self.view.frame.size.width - 100; 33 CGFloat scrollViewH = 300; 34 CGFloat scrollViewX = (self.view.frame.size.width - scrollViewW)/2; 35 CGFloat scrollViewY = 90; 36 UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(scrollViewX, scrollViewY, scrollViewW, scrollViewH)]; 37 self.imgShowView = scrollView; 38 [self.view addSubview:self.imgShowView]; 39 40 //图片区 41 CGFloat imgViewW = scrollViewW; 42 CGFloat imgViewH = scrollViewH; 43 CGFloat imgViewY = 0; 44 for (int i=0; i<imgCount; i++) { 45 CGFloat imgViewX = i*imgViewW; 46 UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(imgViewX, imgViewY, imgViewW, imgViewH)]; 47 48 NSString *imgName = [NSString stringWithFormat:@"img_0%d",i+1]; 49 imgView.image = [UIImage imageNamed:imgName]; 50 [self.imgShowView addSubview:imgView]; 51 } 52 53 //设置混动区域scrollView宽度 54 CGFloat contentW = imgCount * imgViewW; 55 self.imgShowView.contentSize = CGSizeMake(contentW, 0); 56 //水平滚动条 57 self.imgShowView.showsHorizontalScrollIndicator = NO; 58 //根据scrollView的宽度来分页 59 self.imgShowView.pagingEnabled = YES; 60 // self.imgShowView.clipsToBounds = NO; 61 } 62 63 -(void)setUpTimer{ 64 NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(showNextImage) userInfo:nil repeats:YES]; 65 self.imgTimer = timer; 66 //将定时器添加到当前线程,NSRunLoopCommonModes表示始终坚挺滚动事件 67 [[NSRunLoop currentRunLoop] addTimer:self.imgTimer forMode:NSRunLoopCommonModes]; 68 } 69 70 //设置分页显示 71 -(void)setUpPageControl{ 72 CGFloat pageControlW = 100; 73 CGFloat pageControlH = 80; 74 CGFloat pageControlX = (self.view.frame.size.width - pageControlW)/2; 75 UIPageControl *pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(pageControlX, 320, pageControlW, pageControlH)]; 76 pageControl.numberOfPages = imgCount; 77 pageControl.currentPage = 0; 78 //当前页小圆点颜色 79 pageControl.currentPageIndicatorTintColor = [UIColor redColor]; 80 //其余点的颜色 81 pageControl.pageIndicatorTintColor = [UIColor blueColor]; 82 self.pageControl = pageControl; 83 [self.view addSubview:self.pageControl]; 84 } 85 86 //销毁定时器 87 -(void)delImgTimer{ 88 [self.imgTimer invalidate]; 89 self.imgTimer = nil; 90 } 91 92 -(void)showNextImage{ 93 //默认当前页码为0,若为最后一页,那么当前页变为第1页;若不是,则页码加1 94 NSInteger pageIndexNum = 0; 95 if (self.pageControl.currentPage == imgCount-1) { 96 pageIndexNum = 0; 97 } else { 98 pageIndexNum = self.pageControl.currentPage + 1; 99 } 100 101 //计算scrollView滚动位置 102 CGFloat offsetX = pageIndexNum * self.imgShowView.frame.size.width; 103 CGPoint offset = CGPointMake(offsetX, 0); 104 [self.imgShowView setContentOffset:offset animated:YES]; 105 } 106 107 #pragma mark 代理方法 108 //开始拖拽scrollView调用 109 -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{ 110 [self delImgTimer]; 111 } 112 113 //停止拖拽scrollView调用 114 -(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{ 115 [self setUpTimer]; 116 } 117 118 -(void)scrollViewDidScroll:(UIScrollView *)scrollView{ 119 //根据scrollView的滚动位置决定pageControl显示第几页 120 CGFloat theScrollViewW = scrollView.frame.size.width; 121 CGFloat a = scrollView.contentOffset.x; 122 // NSLog(@"偏移的x方向距离为%f",a); 123 NSInteger pageIndex = (scrollView.contentOffset.x + theScrollViewW * 0.5)/theScrollViewW; 124 self.pageControl.currentPage = pageIndex; 125 }