MLInfiniteScrollView - 實現無限自動滾動的輪播圖 ScrollView
實現無限自動滾動的輪播圖 ScrollView, 可以通過繼承 MLInfiniteScrollViewCell 來實現自定義 Cell 的展示.
例子: UIViewController 中添加一個 MLInfiniteScrollView
-
聲明一個 MLInfiniteScrollView成員變量(我推薦您這么做, 因為您需要在 ViewWillDisAppear 方法中調用 MLInfiniteScrollView 實例的 startAutoScroll 方法來停止滾動, 否則由于定時器的存在, 將會無法釋放.) 和 NSMutableArray數據源數組.
<p>@property (nonatomic, strong) MLInfiniteScrollView scrollView; </p> <p>@property (nonatomic, strong) NSMutableArray dataSource; </p> </li> -
初始化您的數據源數組, 在這里我們給數據源數組添加 UIColor 實例.
self.dataSource = [[NSMutableArray alloc] initWithObjects:@{@"color":[UIColor redColor], @"title":@"紅色"}, @{@"color":[UIColor orangeColor], @"title":@"橙色"}, @{@"color":[UIColor yellowColor], @"title":@"黃色"}, @{@"color":[UIColor greenColor], @"title":@"綠色"}, @{@"color":[UIColor cyanColor], @"title":@"青色"}, @{@"color":[UIColor blueColor], @"title":@"藍色"}, @{@"color":[UIColor purpleColor], @"title":@"紫色"}, @{@"color":[UIColor brownColor], @"title":@"棕色"}, nil];
</li> -
初始化 MLInfiniteScrollView, 您只需要調用這一個工廠方法, 便可以輕松實例化一個 MLInfiniteScrollView 實例.
self.scrollView = [MLInfiniteScrollView infiniteScrollViewWithFrame: CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, 200) delegate:self dataSource:self timeInterval:4.0 inView: self.view];
</li> -
實現 MLInfiniteScrollView 的數據源方法
(NSInteger) numberOfItemsInInfiniteScrollView:(MLInfiniteScrollView *)scrollView
{
return self.dataSource.count;
}
(MLInfiniteScrollViewCell *) infiniteScrollView:(MLInfiniteScrollView *)scrollView viewAtIndex:(NSInteger)index
{
</li> </ol>MLInfiniteScrollViewCell *cell = [scrollView dequeueReusableCellWithIdentifier: @"CellIdentifier"];
if (!cell) { cell = [MLInfiniteScrollViewCell infiniteScrollViewCellWithStyle: MLInfiniteScrollViewStyleSubTitle reusableIdentifier: @"CellIdentifier" bounds: scrollView.bounds]; }
cell.contentView.backgroundColor = [self.dataSource[index] objectForKey:@"color"];
if (index%2) { cell.textLabel.text = [self.dataSource[index] objectForKey: @"title"]; } else { cell.imageView.image = [UIImage imageNamed:@"data18.bin_1"]; }
return cell;</pre>
請在 ViewController 的 ViewWillAppear 中加入以下代碼
(void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear: animated];
[self.scrollView startAutoScroll];
}</pre>
請在 ViewController 的 ViewWillDisappear 中加入以下代碼
(void) viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear: animated]; [self.scrollView stopAutoScroll];
}</pre>
簡單的幾步, 就已經實現了一個輪播圖的效果. 當然您還可以繼承 MLInfiniteScrollViewCell 類來實現自定義的 Cell, 更多詳情請參照 MLInfiniteScrollViewDemo.
https://github.com/dbseven/MLInfiniteScrollView