1 // 2 // ViewController.m 3 // 图片轮播器 4 // 5 // Created by 刘羽 on 15/12/30. 6 // Copyright © 2015年 LX. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 //宏定义 图片数量 11 #define kImageCount 5 12 13 @interface ViewController () <UIScrollViewDelegate> 14 15 @property (strong,nonatomic)UIScrollView *scrollView; 16 @property (strong,nonatomic)UIPageControl *pageControl; 17 @property (strong,nonatomic)NSTimer *timer; 18 19 20 21 @end 22 23 @implementation ViewController 24 25 -(UIScrollView *)scrollView 26 { 27 if (_scrollView == nil) { 28 _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(10, 20, 300, 130)]; 29 _scrollView.backgroundColor = [UIColor greenColor]; 30 [self.view addSubview:_scrollView]; 31 32 //取消弹簧效果,不然拖动最后一张图片时会露出底色 很难看 33 _scrollView.bounces = NO; 34 //取消水平垂直滚动高条 35 _scrollView.showsHorizontalScrollIndicator = NO; 36 _scrollView.showsVerticalScrollIndicator = NO; 37 //要分页 显示图片 38 _scrollView.pagingEnabled = YES; 39 //设置contentSize 因高度不越出屏幕,设为0 40 _scrollView.contentSize = CGSizeMake(kImageCount * _scrollView.bounds.size.width, 0); 41 //设置代理 42 _scrollView.delegate = self; 43 44 } 45 return _scrollView; 46 } 47 48 -(UIPageControl *)pageControl 49 { 50 if (_pageControl == nil) { 51 //分页控件本质上和scrollView控件没有任何关系,是两个独立的控件 52 _pageControl = [[UIPageControl alloc]init]; 53 //分页空间总页数 = 图片数 54 _pageControl.numberOfPages = kImageCount; 55 //空间尺寸:可以根据图片数量,自动调整大小 56 CGSize size = [_pageControl sizeForNumberOfPages:kImageCount]; 57 58 _pageControl.bounds = CGRectMake(0, 0, size.width, size.height); 59 //控件的中心点和view的中心点一致即可 60 _pageControl.center = CGPointMake(self.view.center.x, 130); 61 62 //设置控件颜色 63 _pageControl.pageIndicatorTintColor = [UIColor redColor]; 64 _pageControl.currentPageIndicatorTintColor = [UIColor blackColor]; 65 66 [self.view addSubview:_pageControl]; 67 68 //添加监听方法;在OC中,绝大多数控件都可以监听UIControlEventValueChanged 69 //button除外 70 [_pageControl addTarget:self action:@selector(pageChanged:) forControlEvents:UIControlEventValueChanged]; 71 } 72 return _pageControl; 73 } 74 //实现分页控件的监听方法 75 -(void)pageChanged:(UIPageControl *)page 76 { 77 //根据页数 调整滚动视图中的图片位置 78 //计算每张图片的x坐标 79 CGFloat x = page.currentPage * self.scrollView.bounds.size.width; 80 //当控件的数值变化时,监听方法执行,此方法就可以根据控件数值改变为对应的图片,后面参数为是否使用动画 81 [self.scrollView setContentOffset:CGPointMake(x, 0) animated:YES]; 82 } 83 84 //视图加载完成后调用,通常用来设置数据 85 - (void)viewDidLoad { 86 [super viewDidLoad]; 87 //设置图片 88 for (int i = 0; i < kImageCount; i++) { 89 NSString *imageName = [NSString stringWithFormat:@"img_%02d",i+1]; 90 UIImage *image = [UIImage imageNamed:imageName]; 91 92 UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.scrollView.bounds]; 93 imageView.image = image; 94 //这里注意 是self.scrollView 而不是self.view 95 [self.scrollView addSubview:imageView]; 96 97 } 98 //块代码的遍历:滚动视图scrollView的所有子视图(imageview) idx指索引 99 //计算imageView的位置 (一个imageView中并排放了五张图片,要计算imgeView的位置,以精确显示每张图片) 100 [self.scrollView.subviews enumerateObjectsUsingBlock:^(UIImageView *imageView, NSUInteger idx, BOOL *stop) { 101 //把imageView的frame取出 重新赋值给imageView 102 CGRect frame = imageView.frame; 103 //frame由每张图片宽度*index决定 104 frame.origin.x = idx * frame.size.width; 105 imageView.frame = frame; 106 107 }]; 108 109 //分页初始页数为0 正好初始化pageControl控件 110 self.pageControl.currentPage = 0; 111 //启动时钟 112 [self startTimer]; 113 114 115 } 116 117 //启动时钟 118 -(void)startTimer 119 {//每隔2s执行updateTimer方法 120 self.timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES]; 121 //不要忘记这句! 添加到运行循环! 122 [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes]; 123 } 124 125 -(void)updateTimer 126 { 127 //每隔2s 将页面发生变化 128 //(当前页数 + 1) % 总页数 无线循环 129 int page = (self.pageControl.currentPage + 1) % kImageCount; 130 self.pageControl.currentPage = page; 131 //调用方法 移动图片 132 [self pageChanged:self.pageControl]; 133 } 134 135 136 #pragma mark - scrollView的代理方法 137 //滚动视图移动时,pageControl的小点随之改变 138 -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 139 { 140 //计算当前页数 141 int page = scrollView.contentOffset.x/scrollView.bounds.size.width; 142 self.pageControl.currentPage = page;//此时小点对应的颜色会改变 143 } 144 //修改时钟所在的运行循环模式后,会抓不住图片 145 //解决方法:抓住图片时停止时钟,松手后再开启时钟 146 -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView 147 { 148 [self.timer invalidate]; 149 } 150 -(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate 151 { 152 [self startTimer]; 153 } 154 155 156 @end