一:圖片輪播器效果如圖:能實現自動輪播,到最後一頁時,輪播回來,能夠實現拖拽滾動oop
二:代碼:動畫
1 #import "ViewController.h" 2 static CGFloat const imageCount = 5; 3 @interface ViewController ()<UIScrollViewDelegate> 4 /*scrollView*/ 5 @property (nonatomic,weak)UIScrollView *scroll; 6 /*pageControl*/ 7 @property (nonatomic,weak)UIPageControl *pageControl; 8 /*timer*/ 9 @property (nonatomic ,strong)NSTimer *timer; 10 @property (nonatomic,assign)int a; 11 @end 12 13 @implementation ViewController 14 15 - (void)viewDidLoad { 16 [super viewDidLoad]; 17 18 //0:建立底部視圖 19 UIView *bottomView = [[UIView alloc]init]; 20 bottomView.frame = CGRectMake(20, 100, self.view.frame.size.width - 40, 200); 21 [self.view addSubview:bottomView]; 22 23 24 //1:建立滾動視圖 25 UIScrollView *scrollView = [[UIScrollView alloc]init]; 26 scrollView.frame = bottomView.bounds; 27 scrollView.bounces = NO; 28 scrollView.showsVerticalScrollIndicator = NO; 29 scrollView.showsHorizontalScrollIndicator = NO; 30 scrollView.pagingEnabled = YES; 31 scrollView.delegate = self; 32 scrollView.backgroundColor = [UIColor redColor]; 33 self.scroll = scrollView; 34 [bottomView addSubview:scrollView]; 35 36 //2:建立ImageView 37 CGFloat imageWidth = scrollView.frame.size.width; 38 CGFloat imageHeight = scrollView.frame.size.height; 39 40 for (int i = 0; i < imageCount; i++) { 41 42 UIImageView *imageView = [[UIImageView alloc]init]; 43 imageView.frame = CGRectMake(i *imageWidth, 0, imageWidth, imageHeight); 44 imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"img_0%d.png",i+1]]; 45 [scrollView addSubview:imageView]; 46 47 } 48 49 //3:設置scroll的contentSize 50 scrollView.contentSize = CGSizeMake(imageCount *imageWidth, 0); 51 52 53 //4:建立pageControl 54 UIPageControl *pageControl = [[UIPageControl alloc]init]; 55 pageControl.numberOfPages = imageCount; 56 pageControl.currentPage = 0; 57 pageControl.pageIndicatorTintColor = [UIColor whiteColor]; 58 pageControl.currentPageIndicatorTintColor = [UIColor redColor]; 59 pageControl.center = CGPointMake(bottomView.frame.size.width *0.5, bottomView.frame.size.height *0.9); 60 self.pageControl = pageControl; 61 [bottomView addSubview:pageControl]; 62 63 //5:添加定時器 64 NSTimer *timer = [NSTimer timerWithTimeInterval:2 target:self selector:@selector(changeImage) userInfo:nil repeats:YES]; 65 NSRunLoop *runLoop = [NSRunLoop currentRunLoop]; 66 /* 67 forMode的參數: 68 NSDefaultRunLoopMode 69 NSRunLoopCommonModes 70 */ 71 self.timer = timer; 72 [runLoop addTimer:timer forMode: NSRunLoopCommonModes]; 73 // [timer fire]; 74 75 76 //6:添加UITextView 77 UITextView *textView = [[UITextView alloc]init]; 78 textView.frame = CGRectMake(0, 320, self.view.frame.size.width, 150); 79 textView.text = @"凡事看大佛我阿薩德fo一我啊搜ID唸佛愛上你都放那艘IDif完增強覅進去我ijefo凡事看大佛我阿薩德fo一我啊搜ID唸佛愛上你都放那艘IDif完增強覅進去我ijefo凡事看大佛我阿薩德fo一我啊搜ID唸佛愛上你都放那艘IDif完增強覅進去我ijefo凡事看大佛我阿薩德fo一我啊搜ID唸佛愛上你都放那艘IDif完增強覅進去我ijefo凡事看大佛我阿薩德fo一我啊搜ID唸佛愛上你都放那艘IDif完增強覅進去我ijefo凡事看大佛我阿薩德fo一我啊搜ID唸佛愛上你都放那艘IDif完增強覅進去我ijefo凡事看大佛我阿薩德fo一我啊搜ID唸佛愛上你都放那艘IDif完增強覅進去我ijefo凡事看大佛我阿薩德fo一我啊搜ID唸佛愛上你都放那艘IDif完增強覅進去我ijefo凡事看大佛我阿薩德fo一我啊搜ID唸佛愛上你都放那艘IDif完增強覅進去我ijefo凡事看大佛我阿薩德fo一我啊搜ID唸佛愛上你都放那艘IDif完增強覅進去我ijefo"; 80 textView.scrollEnabled = YES; 81 textView.backgroundColor = [UIColor redColor]; 82 [self.view addSubview:textView]; 83 } 84 85 - (void)changeImage { 86 87 NSInteger page = self.pageControl.currentPage; 88 89 90 91 if (page == imageCount -1) { 92 self.a = imageCount -1; 93 } 94 95 96 if (page == 0) { 97 self.a = 0; 98 } 99 100 101 if (self.a == imageCount -1) { 102 103 page -- ; 104 105 }else { 106 107 page ++; 108 } 109 110 111 // [self.scroll setContentOffset:CGPointMake(page *self.scroll.frame.size.width, 0) animated:YES]; 112 [UIView animateWithDuration:1.0 animations:^{ 113 self.scroll.contentOffset = CGPointMake(page *self.scroll.frame.size.width, 0); 114 }]; 115 116 } 117 118 - (void)scrollViewDidScroll:(UIScrollView *)scrollView { 119 120 //當下一頁滑到>=0.5寬度時,此時就要改變pageControl 121 CGFloat page = scrollView.contentOffset.x / scrollView.frame.size.width; 122 NSUInteger currentPage = page; 123 self.pageControl.currentPage = (page - currentPage) < 0.5 ? currentPage : currentPage +1; 124 } 125 126 //拖拽開始時關閉定時器,定時器一旦關閉,就不能再從新開啓 127 - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { 128 [self.timer invalidate]; 129 130 } 131 132 - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { 133 134 self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(changeImage) userInfo:nil repeats:YES]; 135 } 136 137 138 @end
三:知識點總結atom
1:建立定時器: 1:NSTimer *timer = [NSTimer timerWithTimeInterval:2 target:self selector:@selector(changeImage) userInfo:nil repeats:YES];此方法建立的定時器,必須加到NSRunLoop中,NSRunLoop *runLoop = [NSRunLoop currentRunLoop]; [runLoop addTimer:timer forMode: NSRunLoopCommonModes];第二個參數有兩種類型可供選擇: forMode的參數: NSDefaultRunLoopMode NSRunLoopCommonModes,第一個參數爲默認參數,當下面有textView,textfield等控件時,拖拽控件,此時輪播器會中止輪播,由於NSRunLoop的緣由,NSRunLoop爲一個死循環,實時監測有無事件響應,textView或是textField的事件會影響圖片輪播,使用第二個參數會解決這個問題 2: self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(changeImage) userInfo:nil repeats:YES];此種建立定時器的方式,默認加到了runloop,且默認爲第二個參數。3:定時器方法:[timer fire];此時會馬上執行定時器的方法,如果沒有調用此方法,則會等待2秒執行定時器的方法。spa
2:輪播器的拖拽:在拖拽輪播器時,不影響輪播效果,實現滾動代理的開始拖拽方法,並關閉定時器[self.timer invalidate];定時器一旦被關閉,則不能再從新開啓,須要從新建立定時器。在拖拽結束的代理中,再從新建立定時器3d
3:圖片滾動超過寬度的0.5時的計算方法:四捨五入CGFloat page = scrollView.contentOffset.x / scrollView.frame.size.width; NSUInteger currentPage = page; self.pageControl.currentPage = (page - currentPage) < 0.5 ? currentPage : currentPage +1;先求出滾動的浮點數,在轉化成整數,作差值,小於0.5的取當前的整數,大於0.5的取當前整數加1代理
4:設置scrollView的偏移量:1:UIView的動畫 2:[self.scroll setContentOffset:CGPointMake(page *self.scroll.frame.size.width, 0) animated:YES];第二種方法,滾動的時間很短,時間小於1秒,第一種就是能夠控制動畫的時間,和完成動畫的回調code
5:設置輪播效果:orm
NSInteger page = self.pageControl.currentPage;blog
if (page == imageCount -1) {事件
self.a = imageCount -1;
}
if (page == 0) {
self.a = 0;
}
if (self.a == imageCount -1) {
page -- ;
}else {
page ++;
}
注意:不要在此方法中,設置pageControl的currentPage,由於若是設置了,會馬上偏移到相應的位置,可是此時還在實時監測scrollView的滾動,在此滾動代理中,小於寬度一半時,會取當前的整數頁,大於時會取整數+1頁,因此此時pageControl會馬上到某頁,在回到-1頁,再忽然回到某頁,存在bug。不要設置pageControl的currentPage,讓其直接在滾動代理中去設置。