ios 学习 广告图片轮播器

  1 //
  2 //  ViewController.m
  3 //  图片轮播器
  4 //
  5 //  Created by zjj on 15/5/23.
  6 //  Copyright (c) 2015年 zjj. All rights reserved.
  7 //
  8 
  9 #import "ViewController.h"
 10 #define kCount 5
 11 @interface ViewController () <UIScrollViewDelegate>
 12 @property (nonatomic,strong)UIScrollView *scroView;
 13 @property (nonatomic,strong)UIPageControl *pageControl;
 14 @property (nonatomic,strong)NSTimer *time;
 15 @end
 16 
 17 @implementation ViewController
 18 /**
 19  开发思路
 20  1>创建scrollView  
 21  2>用UIimageView加载图片 并修改图片的x值
 22  3>创建分页控件UIpageControl 
 23  4>绑定分页圆点点 点击圆点点换下一页
 24  5>绑定时钟
 25  */
 26 #pragma mark 初始化scrollView
 27 - (UIScrollView *)scroView
 28 {
 29     if (_scroView == nil) {
 30         _scroView = [[UIScrollView alloc]initWithFrame:CGRectMake(36, 20, 300, 130)];
 31         _scroView.backgroundColor = [UIColor blueColor];
 32         [self.view addSubview:_scroView];
 33         _scroView.bounces = NO;
 34         _scroView.showsHorizontalScrollIndicator = NO;
 35         _scroView.showsVerticalScrollIndicator = NO;
 36         _scroView.contentSize = CGSizeMake(_scroView.bounds.size.width * kCount, 0);
 37         self.scroView.pagingEnabled = YES;
 38         _scroView.delegate = self;
 39     }
 40     return _scroView ;
 41 }
 42 #pragma mark 初始化PageCOntrol
 43 - (UIPageControl *)pageControl
 44 {
 45     if (_pageControl == nil) {
 46         // 分页控件 跟scrollView 是独立的
 47         _pageControl = [[UIPageControl alloc]init];
 48         _pageControl.numberOfPages = kCount;
 49         CGSize size = [_pageControl sizeForNumberOfPages:kCount];
 50         _pageControl.bounds = CGRectMake(0, 0, size.width, size.height);
 51         _pageControl.center = CGPointMake(self.scroView.center.x, self.scroView.center.y+(self.scroView.center.y/2));
 52         _pageControl.pageIndicatorTintColor = [UIColor redColor];
 53         _pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
 54         [self.view addSubview:_pageControl];
 55         self.pageControl = _pageControl;
 56     }
 57     return _pageControl;
 58 }
 59 
 60 - (void)viewDidLoad {
 61     [super viewDidLoad];
 62     for (int i = 0; i < kCount; i++) {
 63        NSString *icon = [NSString stringWithFormat:@"img_%02d",i + 1];
 64         UIImageView *imgView = [[UIImageView alloc]initWithFrame:self.scroView.bounds];
 65         imgView.image = [UIImage imageNamed:icon];
 66         [self.scroView addSubview:imgView];
 67     }
 68     // 添加事件之后执行该块代码重新赋值x值
 69     [self.scroView.subviews enumerateObjectsUsingBlock:^(UIImageView *imageView, NSUInteger idx, BOOL *stop) {
 70         CGRect frame = imageView.frame;
 71         frame.origin.x = idx * frame.size.width;
 72         imageView.frame = frame;
 73     }];
 74     self.pageControl.currentPage = 0;
 75     [self.pageControl addTarget:self action:@selector(pageChange:) forControlEvents:UIControlEventTouchUpInside];
 76     [self start];
 77 }
 78 #pragma mark 启动时钟
 79 - (void)start
 80 {
 81 //    self.time = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(changeTime) userInfo:nil repeats:YES];// 按住屏幕 画面会停止轮播但 时钟会累积 连跳好几页
 82     self.time = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(changeTime) userInfo:nil repeats:YES];// 按住屏幕时钟 画面仍然不会停止轮播
 83     [[NSRunLoop currentRunLoop] addTimer:self.time forMode:NSRunLoopCommonModes];// 添加到(运行循环)轮循
 84 }
 85 
 86 - (void)changeTime
 87 {
 88     int page = (self.pageControl.currentPage + 1) % kCount;
 89     self.pageControl.currentPage = page;
 90     [self pageChange:self.pageControl];
 91 }
 92 
 93 #pragma mark 对scrollView进行操作 根据图片偏移位置计算页数
 94 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
 95 {
 96 //    NSLog(@"%@,%f",  NSStringFromCGPoint(_scroView.contentOffset),_scroView.bounds.size.width);//取出scrollview中当前页的x
 97     int page = _scroView.contentOffset.x / _scroView.bounds.size.width;
 98     self.pageControl.currentPage = page; //当前页数
 99 }
100 
101 - (void)pageChange :(UIPageControl *)page
102 {
103     CGFloat x  = page.currentPage * self.scroView.bounds.size.width;
104     [self.scroView setContentOffset:CGPointMake(x, 0)animated:YES];
105 }
106 #pragma mark 拖动图片时停止时钟
107 - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
108 {
109     [self.time invalidate];
110 }
111 #pragma mark 松手后再次启动时钟
112 - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
113 {
114     [self start];
115 }
116 @end
原文地址:https://www.cnblogs.com/zhangdashao/p/4525137.html