您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 程式設計技術

iOS 手動無限輪播器實現:兩個 Cell 的巧妙輪轉

編輯:iOS 程式設計技術

本文實現一個不依賴定時器的手動無限輪播效果,核心只用兩個 cell 就搞定了看似複雜的循環滾動。

實現思路

  • 整個 collectionView 中實際只使用 2 個 cell,中間位置始終展示第二個 cell。
  • 向前滾動時,當前 cell 的 index 變為 0;向後滾動時變為 2。用當前 cell 的 index 減去 1,得到 +1 或 -1,以此讓圖片索引加一或減一,完成圖片切換。
  • 聲明一個全局變量 index 保存圖片索引,用來決定當前 cell 上顯示哪張圖。
  • 滾動前先讓顯示中的 cell 的 index 變成 1,這一步在 viewDidLoad 中設置。
  • 一次滾動結束後,再用代碼把當前 cell 設回第二個 cell(本質就是把 index 改回 1)。
  • 還有一個線程問題:連續快速滾動後停下,當前圖片會閃動,這是異步並發執行導致的線程不安全。解法是把「滾動結束後重置 cell」的代碼放進主隊列執行。

準備工作

創建 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:

  • cell 向前滾動時,圖片索引為 self.index - 1,此時 indexPath.item 為 0,圖片索引確實需要減 1;
  • cell 向後滾動時,圖片索引為 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];
    });
}
  • cell 停止滾動後,把圖片索引 self.index 計算出來並保存到全局變量。這樣下次數據源方法返回 cell 時,就能用 indexPath.item 的 +1/-1 疊加 self.index 正確切換圖片。
  • 滾動結束後把「重置 cell」的動作放進主隊列異步執行:這樣不阻塞主線程,主隊列任務順序執行,等當前任務收尾後再把 cell 切回第二個,就不會出現連續滾動後閃圖的 bug。
//封裝設置當前顯示的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 演出無限循環的錯覺」:任何時刻真正可見的只有中間那一格,滾動結束後立刻把它悄悄搬回中間,用戶視角裡就永遠看不到盡頭。相比往數據源裡塞大量重複數據,這種做法內存開銷極小。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved