本文實現一個不依賴定時器的手動無限輪播效果,核心只用兩個 cell 就搞定了看似複雜的循環滾動。
index 保存圖片索引,用來決定當前 cell 上顯示哪張圖。viewDidLoad 中設置。創建 CollectionViewController,關聯 storyboard,完成類綁定、cell 綁定與可重用標識綁定。
cell.h 文件:
// PBCollectionCell.h
// 無限滾動
#import <UIKit/UIKit.h>
@interface PBCollectionCell : UICollectionViewCell
@property (nonatomic, strong) UIImage *img;
@end
cell.m 文件:
// PBCollectionCell.m
#import "PBCollectionCell.h"
@interface PBCollectionCell ()
//不要忘記給collectionView的cell上設置圖片框,並拖線到cell分類中
@property (strong, nonatomic) IBOutlet UIImageView *imgView;
@end
@implementation PBCollectionCell
//重寫img的set方法給cell賦值(調用cell.img = img時會回調setImg:)
-(void)setImg:(UIImage *)img{
_img = img;
self.imgView.image = img;
}
@end
// ViewController.m
#import "ViewController.h"
#import "PBCollectionCell.h"
//定義宏圖片的個數
#define kPictureCount 3
@interface ViewController ()
@property (strong, nonatomic) IBOutlet UICollectionViewFlowLayout *flowLayout;
/** 圖片的索引 */
@property (nonatomic, assign) NSInteger index;
@end
聲明全局變量 index,用於在滾動過程中拼接要切換的圖片索引。
@implementation ViewController
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return kPictureCount;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *ID = @"cell";
PBCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
//圖片索引只有下一張或者上一張,即+1或者-1,為了切換圖片
//中間的cell的indexPath.item是1,滾動後變成2或者0
//湊成next值為+1或者-1,讓圖片索引加1或減1來完成切換
NSInteger next = indexPath.item - 1;
//為了不讓next越界,進行取余運算
next = (self.index + kPictureCount + next) % kPictureCount;
NSString *imgName = [NSString stringWithFormat:@"%02ld", next + 1];
UIImage *img = [UIImage imageNamed:imgName];
cell.img = img;
return cell;
}
在 viewDidLoad 中把當前顯示的 cell 設為第二個 cell。此後向前滾動時 index 減 1(此時 indexPath.item 為 0),向後滾動時 index 加 1(此時 indexPath.item 為 2)。
那圖片索引怎麼拼?關鍵就在全局變量 self.index:
self.index - 1,此時 indexPath.item 為 0,圖片索引確實需要減 1;self.index + 1,此時 indexPath.item 為 2,圖片索引確實需要加 1;indexPath.item - 1 加上 self.index,就能算出下一個入場 cell 該顯示的圖片索引。-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
//計算偏移的整數倍,偏移了0或者是2
//-1是讓self.index圖片索引+1 或者 -1以切換圖片
NSInteger offset = scrollView.contentOffset.x / scrollView.bounds.size.width - 1;
self.index = (offset + self.index + kPictureCount) % kPictureCount;
//本質就是改變cell的索引,再根據self.index來切換圖片
//使用取余讓圖片索引不至於越界
//異步主隊列執行,為了不讓連續滾動停止後圖片有閃動
dispatch_async(dispatch_get_main_queue(), ^{
[self scrollToSecondCell];
});
}
self.index 計算出來並保存到全局變量。這樣下次數據源方法返回 cell 時,就能用 indexPath.item 的 +1/-1 疊加 self.index 正確切換圖片。//封裝設置當前顯示的cell為第二個cell的方法
-(void)scrollToSecondCell{
NSIndexPath *idxPath = [NSIndexPath indexPathForItem:1 inSection:0];
[self.collectionView scrollToItemAtIndexPath:idxPath
atScrollPosition:UICollectionViewScrollPositionLeft
animated:NO];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.flowLayout.itemSize = self.collectionView.bounds.size;
self.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
self.flowLayout.minimumLineSpacing = 0;
self.collectionView.backgroundColor = [UIColor whiteColor];
self.collectionView.pagingEnabled = YES;
self.collectionView.bounces = NO;
self.collectionView.showsHorizontalScrollIndicator = NO;
[self scrollToSecondCell];
}
@end
要點一
cell 滑動前、以及每次滑動結束後,都要用代碼把當前顯示的 cell 設為第二個 cell。在數據源返回 cell 的方法中,圖片切換靠的是
indexPath.item - 1(-1 或 +1)再加上圖片本身的索引self.index,由此判斷即將滾出的 cell 該顯示上一張還是下一張。要點二
cell 滾動結束後要計算當前顯示圖片的索引:用
contentOffset.x除以 scrollView 寬度得出倍數並保存。之後再滑動時,數據源方法會用保存下來的self.index加上indexPath.item - 1來算出目標圖片索引。
這套方案的精髓在於「用複用池裡的兩個 cell 演出無限循環的錯覺」:任何時刻真正可見的只有中間那一格,滾動結束後立刻把它悄悄搬回中間,用戶視角裡就永遠看不到盡頭。相比往數據源裡塞大量重複數據,這種做法內存開銷極小。