// 註意,在有導航欄的情況下,需要在viewDidLoad 中加上 self.automaticallyAdjustsScrollViewInsets = NO;不然會出現圖片下移64的情況 ,scrollView.frame的高度要比圖片的高度大64,不然,圖片顯示不全 源文件在這裡:http: ...
1 #import "ViewController.h" 2 #define kDeviceWidth [UIScreen mainScreen].bounds.size.width 3 #define kDeviceHeight [UIScreen mainScreen].bounds.size.height 4 #define Kpage 3 5 @interface ViewController ()<UIScrollViewDelegate> 6 7 @property(nonatomic,strong)UIPageControl *pageCtrl; 8 @property (nonatomic,weak) UIScrollView *scrollView; 9 @property (nonatomic,weak) UIImageView *currentImageView; // 當前imageView 10 @property (nonatomic,weak) UIImageView *nextImageView; // 下一個imageView 11 @property (nonatomic,weak) UIImageView *preImageView; //上一個imageView 12 @property (nonatomic,assign) BOOL isDragging; // 是否正在滾動 13 @property (nonatomic,strong)NSTimer *timer; 14 @end 15 16 @implementation ViewController 17 18 - (void)viewDidLoad { 19 [super viewDidLoad];
// 註意,在有導航欄的情況下,需要在viewDidLoad 中加上 self.automaticallyAdjustsScrollViewInsets = NO;不然會出現圖片下移64的情況 ,scrollView.frame的高度要比圖片的高度大64,不然,圖片顯示不全
20 UIScrollView *scrollView =[[UIScrollView alloc] init]; 21 scrollView.frame = CGRectMake(0, 0, kDeviceWidth, kDeviceHeight); 22 [self.view addSubview:scrollView]; 23 self.scrollView = scrollView; 24 [self.scrollView setContentSize:CGSizeMake(kDeviceWidth * 3, kDeviceWidth)]; 25 // 設置隱藏橫向條 26 self.scrollView.showsHorizontalScrollIndicator = NO; 27 // 設置自動分頁 28 self.scrollView.pagingEnabled = YES; 29 // 設置代理 30 self.scrollView.delegate = self; 31 // 設置當前點 32 self.scrollView.contentOffset = CGPointMake(kDeviceWidth, 0); 33 // 設置是否有邊界 34 self.scrollView.bounces = NO;
35 // 初始化當前視圖
// 註意:這裡如果圖片向左輪播,無需初始化上一個視圖;如果圖片向右輪播,無需初始化下一個視圖
36 UIImageView *currentImageView =[[UIImageView alloc] init]; 37 currentImageView.image = [UIImage imageNamed:@"bg_01"]; 38 [self.scrollView addSubview:currentImageView]; 39 self.currentImageView = currentImageView; 40 self.currentImageView.frame = CGRectMake(kDeviceWidth, 0, kDeviceWidth, kDeviceHeight); 41 self.currentImageView.contentMode = UIViewContentModeScaleAspectFill; 42 // 初始化下一個視圖 43 UIImageView *nextImageView = [[UIImageView alloc] init]; 44 nextImageView.image = [UIImage imageNamed:@"bg_02"]; 45 [self.scrollView addSubview:nextImageView]; 46 self.nextImageView = nextImageView; 47 self.nextImageView.frame = CGRectMake(kDeviceWidth * 2, 0, kDeviceWidth, kDeviceHeight); 48 self.nextImageView.contentMode = UIViewContentModeScaleAspectFill; 49 // 初始化上一個視圖, 50 UIImageView *preImageView =[[UIImageView alloc] init]; 51 preImageView.image = [UIImage imageNamed:@"bg_03"]; 52 preImageView.frame = CGRectMake(0, 0, kDeviceWidth, kDeviceHeight); 53 [self.scrollView addSubview:preImageView]; 54 self.preImageView = preImageView; 55 self.preImageView.contentMode =UIViewContentModeScaleAspectFill; 56 57 // 設置時鐘動畫 定時器 58 59 self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(update:) userInfo:nil repeats:YES]; 60 // 將定時器添加到主線程 61 [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes]; 62 63 } 64 // 分頁 65 -(UIPageControl *)pageCtrl{ 66 if (_pageCtrl == nil) { 67 68 //分頁控制項 69 _pageCtrl = [[UIPageControl alloc]init]; 70 _pageCtrl.numberOfPages = Kpage; 71 72 CGSize size = [_pageCtrl sizeForNumberOfPages:Kpage]; 73 _pageCtrl.bounds = CGRectMake(0, 0, size.width, size.height); 74 _pageCtrl.center = CGPointMake(self.view.center.x, CGRectGetMaxY(self.scrollView.frame) - 20); 75 _pageCtrl.pageIndicatorTintColor = [UIColor redColor]; 76 _pageCtrl.currentPageIndicatorTintColor = [UIColor greenColor]; 77 78 [self.view addSubview:_pageCtrl]; 79 80 81 } 82 return _pageCtrl; 83 } 84 85 86 -(void)updateTimer{ 87 //修改頁號 88 int page = (self.pageCtrl.currentPage + 1 ) % Kpage; 89 self.pageCtrl.currentPage = page; 90 91 } 92 93 94 - (void)update:(NSTimer *)timer{ 95 //定時移動 96 97 if (_isDragging == YES) { 98 99 return ; 100 } 101 CGPoint offSet = self.scrollView.contentOffset; 102 offSet.x +=offSet.x; 103 104 [self updateTimer]; 105 106 [self.scrollView setContentOffset:offSet animated:YES]; 107 if (offSet.x >= kDeviceWidth *2) { 108 offSet.x = kDeviceWidth; 109 } 110 111 } 112 #pragma mark - UIScrollViewDelegate 113 - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView 114 { 115 _isDragging = YES; 116 } 117 // 停止滾動 118 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 119 { 120 _isDragging = NO; 121 122 } 123 124 // 開始滾動 125 - (void) scrollViewDidScroll:(UIScrollView *)scrollView{ 126 static int i =1; // 當前展示的是第幾張圖片 127 float offset = self.scrollView.contentOffset.x;
// 註意:這裡如果圖片向左輪播,無需載入上一個視圖;如果圖片向右輪播,無需載入下一個視圖
128 if (self.nextImageView.image == nil || self.preImageView.image == nil) { 129 // 載入下一個視圖 130 NSString *imageName1 = [NSString stringWithFormat:@"bg_0%d",i == Kpage ? 1:i +1]; 131 _nextImageView.image = [UIImage imageNamed:imageName1]; 132 // 載入上一個視圖 133 NSString *imageName2 = [NSString stringWithFormat:@"bg_0%d",i==1 ? Kpage :i-1]; 134 _preImageView.image = [UIImage imageNamed:imageName2]; 135 136 }
// 註意:這裡如果圖片向左輪播,無需if(offset ==0)判斷;如果圖片向右輪播,無需if (offset == scrollView.bounds.size.width * 2)判斷
137 if(offset ==0){ 138 _currentImageView.image = _preImageView.image; 139 scrollView.contentOffset = CGPointMake(scrollView.bounds.size.width, 0); 140 _preImageView.image = nil; 141 if (i == 1) { 142 i =Kpage; 143 } else{ 144 i-=1; 145 } 146 147 } 148 if (offset == scrollView.bounds.size.width * 2) { 149 _currentImageView.image = _nextImageView.image; 150 scrollView.contentOffset = CGPointMake(scrollView.bounds.size.width, 0); 151 _nextImageView.image = nil; 152 if (i == Kpage) { 153 i = 1 ; 154 }else{ 155 i +=1 ; 156 } 157 158 } 159 160 } 161 162 163 @end
假如圖片為img1,img2,img3
//具體說明一下:以三張圖片向左輪播為例,首先當前頁面(就是看的見得)為current位置為320(以5S為例吧,寬度為320)圖片為img1,下一張圖片next位置為640,圖片為img2,
( i= 1)
1.當前圖片2秒後(假如定時器2秒執行一次)移動到640(定時器每執行一次移動一個屏幕寬度);進入if (offset == scrollView.bounds.size.width * 2) 判斷
2.判斷結果為當前頁面的圖片換成前面next的圖片,結果為img2,next的圖片為空,current當前頁面位置變成320(i= 2)
3.因為next的圖片為空,所以進入if (self.nextImageView.image == nil || self.preImageView.image == nil)
4.判斷結果,根據後面的三目運算next的圖片換成img3(因為三目運算)
5.當前圖片2秒後又(定時器2秒執行一次)又移動到640(定時器每執行一次移動一個屏幕寬度);進入if (offset == scrollView.bounds.size.width * 2) 判斷把圖片換成img3,讓next為空,從此無限迴圈
源文件在這裡:http://pan.baidu.com/s/1kVKrbkb