1 #import "ViewController.h"
2
3 @interface ViewController () <UIScrollViewDelegate>
4 /**
5 * 图片轮播器
6 */
7 @property (nonatomic ,weak ) UIScrollView *scrView;
8 /**
9 * 页面指示器
10 */
11 @property (nonatomic, weak ) UIPageControl *pgControl;
12 /**
13 * 计时器
14 */
15 @property (nonatomic, strong) NSTimer *timer;
16 @end
17
18 @implementation ViewController
19 - (void)viewDidLoad
20 {
21 [super viewDidLoad];
22 //加载图片轮播器
23 [self setUpScrView];
24 //加载页面指示器
25 [self setUpPageControl];
26 //加载定时器
27 [self setUpTimer];
28 }
29 - (void)setUpScrView
30 {
31 UIScrollView *scrView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 250)];
32 scrView.alwaysBounceHorizontal = YES;
33 scrView.showsHorizontalScrollIndicator = NO;
34 scrView.pagingEnabled = YES;
35 scrView.contentSize = CGSizeMake(self.view.frame.size.width * 5, 250);
36 _scrView = scrView;
37 [self setUpImg];
38 [self.view addSubview:_scrView];
39 }
40
41 - (void)setUpPageControl
42 {
43 UIPageControl *pgControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 32)];
44 pgControl.center = CGPointMake(CGRectGetMidX(self.view.bounds), 300);
45 pgControl.currentPage = 0;
46 pgControl.numberOfPages = 5;
47 pgControl.currentPageIndicatorTintColor = [UIColor redColor];
48 pgControl.pageIndicatorTintColor = [UIColor greenColor];
49 _pgControl = pgControl;
50 [self.view addSubview:_pgControl];
51 }
52
53 - (void)scrollImage
54 {
55 NSInteger page = _pgControl.currentPage;
56 if (page == _pgControl.numberOfPages - 1) {
57 page = 0;
58 } else {
59 page ++;
60 }
61 CGFloat offsetX = page * _scrView.frame.size.width;
62 [_scrView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
63
64 _pgControl.currentPage = page;
65 }
66
67 - (void)setUpTimer
68 {
69 _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(scrollImage) userInfo:nil repeats:YES];
70 NSRunLoop *runloop = [NSRunLoop currentRunLoop];
71 [runloop addTimer:_timer forMode:NSRunLoopCommonModes];
72 }
73
74 - (void)setUpImg
75 {
76 CGFloat imgW = self.view.bounds.size.width;
77 CGFloat imgH = 250;
78 CGFloat imgY = 0;
79 for (int i = 0; i < 5; i ++) {
80 CGFloat imgX = imgW * i;
81 UIImageView *imgView = [[UIImageView alloc] init];
82 imgView.image = [UIImage imageNamed:[NSString stringWithFormat:@"img_%02d", i + 1]];
83 imgView.frame = CGRectMake(imgX, imgY, imgW, imgH);
84 [_scrView addSubview:imgView];
85 }
86 }
87
88 #pragma mark UIScrollView代理方法
89 - (void)scrollViewDidScroll:(UIScrollView *)scrollView
90 {
91 CGFloat offsetX = _scrView.contentOffset.x;
92 offsetX += _scrView.frame.size.width / 2;
93 NSInteger page = offsetX / _scrView.frame.size.width;
94 _pgControl.currentPage = page;
95 }
96
97 - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
98 {
99 [_timer invalidate];
100 _timer = nil;
101 }
102
103 - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
104 {
105 [self setUpTimer];
106 }
107
108 @end