輪播圖是每個專案都會寫的東西。最直白的做法是塞 N 張 imageView 進 scrollView,設好 contentSize 讓它排成一列。問題是廣告數量往往由後台決定,十幾張圖片同時載入,記憶體與流量都很吃虞。
三屏復用的思路很簡單:不管有多少張圖,畫面上永遠只存在三個 imageView——左、中、右。滑到下一頁時把三張圖往左移一輪,再重新指定各自的內容,中間那張永遠停在可見位置。
用三個索引變數分別記錄左、中、右三張圖在陣列中的位置。中間是當前顯示的圖,左邊是上一張、右邊是下一張。當索引超出陣列範圍時,就繞回另一端:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
if (scrollView.contentOffset.x == 0) { // 往右滑,看上一張
endImageCount--;
oneImageCount--;
secondImageCount--;
if (endImageCount < 0) endImageCount = AllImageCount;
if (oneImageCount < 0) oneImageCount = AllImageCount;
if (secondImageCount < 0) secondImageCount = AllImageCount;
} else if (scrollView.contentOffset.x == ViewWidth * 2) { // 往左滑
endImageCount++;
oneImageCount++;
secondImageCount++;
if (endImageCount > AllImageCount) endImageCount = 0;
if (oneImageCount > AllImageCount) oneImageCount = 0;
if (secondImageCount > AllImageCount) secondImageCount = 0;
}
// 關鍵:把捲動位置拉回中間,完成「無限」的效果
scrollView.contentOffset = CGPointMake(ViewWidth, 0);
self.endImageView.image = self.imageArray[endImageCount];
self.oneImageView.image = self.imageArray[oneImageCount];
self.secondImageView.image = self.imageArray[secondImageCount];
self.pageCtl.currentPage = oneImageCount;
}
- (void)drawRect:(CGRect)rect {
self.circulateScrollView = [[UIScrollView alloc]
initWithFrame:CGRectMake(0, 0, ViewWidth, ViewHeight)];
endImageCount = self.imageArray.count - 1;
oneImageCount = 0;
secondImageCount = 1;
self.circulateScrollView.showsHorizontalScrollIndicator = NO;
self.circulateScrollView.pagingEnabled = YES;
self.circulateScrollView.delegate = self;
self.circulateScrollView.bounces = NO;
self.circulateScrollView.contentOffset = CGPointMake(ViewWidth, 0);
self.backgroundColor = [UIColor whiteColor];
if (!self.imageArray.count) {
NSLog(@"圖片陣列為空");
return;
}
if (self.imageArray.count <= 1) {
// 只有一張時不必三屏,直接單圖顯示
self.circulateScrollView.contentSize = CGSizeMake(ViewWidth, ViewHeight);
self.endImageView = [[UIImageView alloc]
initWithFrame:CGRectMake(0, 0, ViewWidth, ViewHeight)];
self.endImageView.image = self.imageArray[endImageCount];
[self.circulateScrollView addSubview:self.endImageView];
[self addSubview:self.circulateScrollView];
} else {
self.circulateScrollView.contentSize = CGSizeMake(ViewWidth * 3, ViewHeight);
self.endImageView = [[UIImageView alloc]
initWithFrame:CGRectMake(0, 0, ViewWidth, ViewHeight)];
self.endImageView.image = self.imageArray[endImageCount];
[self.circulateScrollView addSubview:self.endImageView];
self.oneImageView = [[UIImageView alloc]
initWithFrame:CGRectMake(ViewWidth, 0, ViewWidth, ViewHeight)];
self.oneImageView.image = self.imageArray[oneImageCount];
[self.circulateScrollView addSubview:self.oneImageView];
self.secondImageView = [[UIImageView alloc]
initWithFrame:CGRectMake(ViewWidth * 2, 0, ViewWidth, ViewHeight)];
self.secondImageView.image = self.imageArray[secondImageCount];
[self.circulateScrollView addSubview:self.secondImageView];
[self addSubview:self.circulateScrollView];
[self pageNumControl];
}
}
- (void)pageNumControl {
self.pageCtl = [[UIPageControl alloc]
initWithFrame:CGRectMake(0, ViewHeight - 20, ViewWidth, 20)];
self.pageCtl.numberOfPages = AllImageCount + 1;
[self addSubview:self.pageCtl];
}
上面範例走的是網路圖片。若要換成本機圖,把赋值方式改掉即可:
self.endImageView.image = [UIImage imageNamed:self.imageArray[endImageCount]];
並在建立時直接傳入圖片名稱:
cview.imageArray = [[NSMutableArray alloc] initWithObjects:@"圖片1", @"圖片2", @"圖片3", nil];
圖片只有兩張時要特別處理。此時「上一張」與「下一張」會是同一張,索引容易互相覆蓋,需如範例般在每次更新後額外做一次邊界檢查。
不要用 drawRect 做布局。drawRect 可能因任何原因被重複呼叫,導致子視圖層層堆疊。請改用 initWithFrame 或 awakeFromNib 建立一次。
記得處理 App 切背景。若加上自動播放計時器,進入背景時必須 invalidate,回前景再重啟,否則回來時可能一口氣跳好幾頁。