無限輪播圖相信是很多開發人員常用的一個功能,這里總結一下常用的兩種方式的實現原理

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
? ? NSInteger leftIndex;
? ? NSInteger rightIndex;
? ? if (scrollView.contentOffset.x == bannerScrollViewW * 2) {
? ? ? ? /** 向左滑? 計算 左 中 右 的下標索引*/
? ? ? ? leftIndex = self.centerIndex % self.imageNames.count;
? ? ? ? self.centerIndex = (self.centerIndex+1) % self.imageNames.count;
? ? ? ? rightIndex = (self.centerIndex +1) % self.imageNames.count;
? ? ? ? //NSLog(@"往左滑");
? ? }else if (scrollView.contentOffset.x == 0) {
? ? ? ? /** 向右滑? 計算 左 中 右 的下標索引*/
? ? ? ? rightIndex = self.centerIndex;
? ? ? ? self.centerIndex = (self.centerIndex - 1) < 0?(self.imageNames.count - 1):(self.centerIndex - 1);
? ? ? ? leftIndex = (self.centerIndex - 1) < 0?(self.imageNames.count - 1):(self.centerIndex - 1);
? ? ? ? //NSLog(@"往右滑");
? ? }else{
? ? ? ? // 沒有滑走 什么都不做,直接return
? ? ? ? return;
? ? }
? ? /** 設置圖片 */
? ? self.leftImageView.image = [UIImage imageNamed:self.imageNames[leftIndex]];
? ? self.centerImageView.image = [UIImage imageNamed:self.imageNames[self.centerIndex]];
? ? self.rightImageView.image = [UIImage imageNamed:self.imageNames[rightIndex]];
? ? /** 設置pageControl currentPage? 因為永遠顯示中間的圖片,故此currentPage=centerIndex */
? ? self.pageControl.currentPage = self.centerIndex;
? ? // 將 bannerScrollView 拉回到中間圖片的位置 顯示圖片
? ? [scrollView setContentOffset:CGPointMake(bannerScrollViewW, 0) animated:NO];
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView;
當我們滑動cell是是無法觸發這個函數的,雖然它是動畫結束后就可以觸發,但是它指的動畫是系統原生動畫,如我們的手動滑動是無法觸發該函數的。