輪播廣告大家都寫過,但多數人的做法是讓 contentSize 等於「圖片數量 × 螢幕寬度」,再配一個 UIPageControl。廣告數量一多,資源就白方浪費了。本文示範只用三個 UIImageView 就撐起任意數量的無限輪播。
scrollView 裡固定放三個 imageView:左、中、右。畫面上真正看得到的是中間那張,左右兩張是預先準備好的前導與後續。每次捲動結束,就把 offset 拉回中央,同時把三張圖的內容往前或往後遞一格。這樣無論廣告有幾張,永遠只佔三個 imageView 的記憶體。
順帶一提,當廣告只有一張時不需要三屏復用,直接單圖顯示即可;兩張以上才啟用。
圖片來源可以是網路,也可以是本地。若走本地,把 .m 中所有類似下面的寫法:
self.endImageView.image = self.imageArray[endImageCount];
改成:
self.endImageView.image = [UIImage imageNamed:self.imageArray[endImageCount]];
使用時直接傳圖片名稱陣列即可:
cview.imageArray = [[NSMutableArray alloc] initWithObjects:@"圖片1", @"圖片2", @"圖片3", nil];
或者不改原始碼,在外部先轉好:
NSArray *imageArr = [[NSArray alloc] initWithObjects:@"banner_理財.jpg", @"banner_惠普", @"banner_炒股", nil];
for (int i = 0; i < 3; i++) {
UIImage *cirImage1 = [UIImage imageNamed:imageArr[i]];
[cirScrollView.imageArray addObject:cirImage1];
}
如果拿到的是 URL,用 imageWithURL 之類的方法自行轉成 UIImage 即可。
以下從「已經拿到 data」開始談。先建立一個繼承 UIView 的類別。
#import <UIKit/UIKit.h>
@interface CirculateScrollview : UIView
@property (nonatomic, strong) NSMutableArray *imageArray; // 圖片陣列
@property (nonatomic, strong) UIScrollView *circulateScrollView; // 廣告容器
/* 三屏復用廣告
* 適用範圍:網路請求或固定的本地廣告圖片
* 適用所有數量的廣告,廣告 >= 2 時自動採用三屏復用技術
*
* 使用方法:在需要加入廣告的控制器中
*
* CirculateScrollview *cview = [[CirculateScrollview alloc] initWithFrame:CGRectMake(0, 20, 320, 200)];
* for (int i = 0; i < 3; i++) {
* UIImage *image = [UIImage imageNamed:@"旅行圖1"]; // 傳入圖片名稱
* // UIImage *image = [UIImage imageWithData:data]; // 或傳入網路取得的 data
* [cview.imageArray addObject:image];
* }
* [self.view addSubview:cview];
*/
/* 圖片轉 NSData 的方法(測試可用)
* NSData *data = UIImageJPEGRepresentation(image, 1);
*/
@end
核心是三個計數變數,分別記錄左、中、右三張圖在陣列中的索引。左滑時三者同步遞增,右滑時同步遞減;一旦越界就繞回另一端,如此形成無限循環。
#import "CirculateScrollview.h"
#define ViewWidth self.frame.size.width
#define ViewHeight self.frame.size.height
#define AllImageCount self.imageArray.count - 1
@interface CirculateScrollview () <UIScrollViewDelegate>
{
NSInteger endImageCount; // 左邊圖片
NSInteger oneImageCount; // 中間圖片(目前看到的)
NSInteger secondImageCount; // 右邊圖片
}
@property (nonatomic, strong) UIImageView *endImageView;
@property (nonatomic, strong) UIImageView *oneImageView;
@property (nonatomic, strong) UIImageView *secondImageView;
@property (nonatomic, strong) UIPageControl *pageCtl;
@end
@implementation CirculateScrollview
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
- (NSMutableArray *)imageArray {
if (!_imageArray) {
_imageArray = [[NSMutableArray alloc] init];
}
return _imageArray;
}
- (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;
}
// 廣告數量少於 2 張時,不採用三屏復用
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.backgroundColor = [UIColor lightGrayColor];
self.pageCtl.currentPageIndicatorTintColor = [UIColor greenColor];
self.pageCtl.pageIndicatorTintColor = [UIColor whiteColor];
self.pageCtl.alpha = 0.7;
self.pageCtl.numberOfPages = AllImageCount + 1;
[self addSubview:self.pageCtl];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
if (scrollView.contentOffset.x == 0) {
// 往左滑:三個索引同步遞減
endImageCount--;
oneImageCount--;
secondImageCount--;
if (endImageCount < 0) {
endImageCount = AllImageCount;
} else if (oneImageCount < 0) {
oneImageCount = AllImageCount;
}
// 適配只有 2 張圖片的情況
if (secondImageCount < 0) {
secondImageCount = AllImageCount;
}
} else if (scrollView.contentOffset.x == ViewWidth * 2) {
// 往右滑:三個索引同步遞增
endImageCount++;
oneImageCount++;
secondImageCount++;
if (endImageCount > AllImageCount) {
endImageCount = 0;
} else if (oneImageCount > AllImageCount) {
oneImageCount = 0;
}
// 適配只有 2 張圖片的情況
if (secondImageCount > AllImageCount) {
secondImageCount = 0;
}
}
// 把 offset 拉回中央,重新填入三張圖
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;
}
@end
第一,drawRect: 並不是初始化子視圖的好位置,它會被反覆呼叫。實務上建議改用 initWithFrame: 或 layoutSubviews(並加上已初始化的判斷)。
第二,scrollViewDidEndDecelerating: 只在慣性捲動結束時觸發。若想支援自動輪播,請另外用 NSTimer 呼叫 setContentOffset:animated:,並在 scrollViewDidEndScrollingAnimation: 中做同樣的繞回處理。
第三,索引遞增/遞減時若圖片數量為偶數,遞減分支的兩個 if 要用 if-else if 串接才不會互相搶走,這段程式碼已經處理過了。