MLInfiniteScrollView - 實現無限自動滾動的輪播圖 ScrollView

m47g 9年前發布 | 16K 次閱讀 iOS開發 移動開發 MLInfiniteScrollView

實現無限自動滾動的輪播圖 ScrollView, 可以通過繼承 MLInfiniteScrollViewCell 來實現自定義 Cell 的展示.

例子: UIViewController 中添加一個 MLInfiniteScrollView

  1. 聲明一個 MLInfiniteScrollView成員變量(我推薦您這么做, 因為您需要在 ViewWillDisAppear 方法中調用 MLInfiniteScrollView 實例的 startAutoScroll 方法來停止滾動, 否則由于定時器的存在, 將會無法釋放.) 和 NSMutableArray數據源數組.

    <p>@property (nonatomic, strong) MLInfiniteScrollView scrollView; </p> <p>@property (nonatomic, strong) NSMutableArray dataSource; </p> </li>

  2. 初始化您的數據源數組, 在這里我們給數據源數組添加 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>

  3. 初始化 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>

  4. 實現 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

 本文由用戶 m47g 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!