直接上代碼了,比較簡單。演示下載地址:Demooop
// // UYViewController.m // 圖片輪播器 // // Created by jiangys on 15/5/23. // Copyright (c) 2015年 uxiaoyuan. All rights reserved. // #import "UYViewController.h" #define kImageCount 5 @interface UYViewController ()<UIScrollViewDelegate> @property (nonatomic,strong) UIScrollView *scrollView; @property (nonatomic,strong) UIPageControl *pageControl; @property (nonatomic, strong) NSTimer *timer; @end @implementation UYViewController -(UIScrollView *)scrollView { if (_scrollView==nil) { //若是爲空,建立一個 _scrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(10, 20, 300, 130)]; _scrollView.backgroundColor=[UIColor redColor]; //取消彈簧效果 _scrollView.bounces=NO; // 取消水平滾動條 _scrollView.showsHorizontalScrollIndicator = NO; _scrollView.showsVerticalScrollIndicator = NO; // 要分頁 _scrollView.pagingEnabled = YES; // contentSize _scrollView.contentSize = CGSizeMake(kImageCount * _scrollView.bounds.size.width, 0); // 設置代理 _scrollView.delegate = self; [self.view addSubview:_scrollView]; } return _scrollView; } - (UIPageControl *)pageControl { if (_pageControl == nil) { // 分頁控件,本質上和scrollView沒有任何關係,是兩個獨立的控件 _pageControl = [[UIPageControl alloc] init]; // 總頁數 _pageControl.numberOfPages = kImageCount; // 控件尺寸 CGSize size = [_pageControl sizeForNumberOfPages:kImageCount]; _pageControl.bounds = CGRectMake(0, 0, size.width, size.height); _pageControl.center = CGPointMake(self.view.center.x, 130); // 設置顏色 _pageControl.pageIndicatorTintColor = [UIColor redColor]; _pageControl.currentPageIndicatorTintColor = [UIColor blackColor]; [self.view addSubview:_pageControl]; // 添加監聽方法 /** 在OC中,絕大多數"控件",均可以監聽UIControlEventValueChanged事件,button除外" */ [_pageControl addTarget:self action:@selector(pageChanged:) forControlEvents:UIControlEventValueChanged]; } return _pageControl; } // 分頁控件的監聽方法 - (void)pageChanged:(UIPageControl *)page { // 根據頁數,調整滾動視圖中的圖片位置 contentOffset CGFloat x = page.currentPage * self.scrollView.bounds.size.width; [self.scrollView setContentOffset:CGPointMake(x, 0) animated:YES]; } - (void)viewDidLoad { [super viewDidLoad]; //設置圖片 for (int i=0; i<kImageCount; i++) { NSString *imageName=[NSString stringWithFormat:@"img_%02d",i+1]; UIImage *image=[UIImage imageNamed:imageName]; UIImageView *imageView=[[UIImageView alloc] initWithFrame:self.scrollView.bounds]; imageView.image=image; [self.scrollView addSubview:imageView]; } //計算imageView [self.scrollView.subviews enumerateObjectsUsingBlock:^(UIImageView *imageView, NSUInteger idx, BOOL *stop) { // 調整x => origin => frame CGRect frame = imageView.frame; frame.origin.x = idx * frame.size.width; imageView.frame = frame; }]; // 分頁初始頁數爲0 self.pageControl.currentPage = 0; // 啓動時鐘 [self startTimer]; } - (void)startTimer { self.timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES]; // 添加到運行循環 [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes]; } - (void)updateTimer { // 頁號發生變化 // (當前的頁數 + 1) % 總頁數 int page = (self.pageControl.currentPage + 1) % kImageCount; self.pageControl.currentPage = page; // 調用監聽方法,讓滾動視圖滾動 [self pageChanged:self.pageControl]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.s } #pragma mark - ScrollView的代理方法 // 滾動視圖停下來,修改頁面控件的小點(頁數) - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { // 計算頁數 int page = scrollView.contentOffset.x / scrollView.bounds.size.width; self.pageControl.currentPage = page; } /** 修改時鐘所在的運行循環的模式後,抓不住圖片 解決方法:抓住圖片時,中止時鐘,送售後,開啓時鐘 */ - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { // 中止時鐘,中止以後就不能再使用,若是要啓用時鐘,須要從新實例化 [self.timer invalidate]; } - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { [self startTimer]; } @end