iOS控件-UICollectionView基本使用詳解(OC)

概述

UICollectionView是從iOS6開始引入使用的,目前應用非常廣泛,很牛逼!老外的博客也是這么說的(傳送門)

美麗說HIGO界面

與UITableView的初步比較


  • UITableView應該是大家最熟悉的控件了,UICollectionView的使用與之類似,但又有所區別,如下介紹。
    相同點:

    • 1.都是通過datasource和delegate驅動的(datasource和delegate官方文檔傳送),因此在使用的時候必須實現數據源與代理協議方法;
    • 2.性能上都實現了循環利用的優化。

    不同點

    • 1.UITableView的cell是系統自動布局好的,不需要我們布局。但UICollectionView的cell是需要我們自己布局的。所以我們在創建UICollectionView的時候必須傳遞一個布局參數,系統提供并實現了一個布局樣式:流水布局(UICollectionViewFlowLayout)(流水布局官方文檔傳送)。
    • 2.UITableViewController的self.view == self.tableview;,但UICollectionViewController的self.view != self.collectionView;
    • 3.UITableView的滾動方式只能是垂直方向, UICollectionView既可以垂直滾動,也可以水平滾動;
    • 4.UICollectionView的cell只能通過注冊來確定重用標識符。

結論: 換句話說,UITableView的布局是UICollectionView的flow layout布局的一種特殊情況,類比于同矩形與正方形的關系

下面簡單介紹幾個基本用法(難度從低到高)


1. UICollectionView普通用法(FlowLayout布局)

  • 上面我們提到了UICollectionView與UITableView的用法非常類似,下面就讓我們完全根據創建UITableView的方式來創建一個UICollectionView(請讀者類比UITableView的創建方式,實現數據源,代理等,這里就只提到與之不同的方面,詳細代碼可參考示例Demo)。
  • 報錯了,提示缺少布局參數,如下:


    缺少布局參數報錯示意圖
  • 解決報錯,我們可以傳FlowLayout參數方式,也可以重寫內部init方法。我們這里采用重寫init方法,傳遞布局參數。這樣更加體現了封裝的思想,把傳遞布局參數封裝在CYXNormalCollectionViewController內,對外只提供統一的外部方法:init方法,代碼如下:
  - (instancetype)init{
    // 設置流水布局
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    // UICollectionViewFlowLayout流水布局的內部成員屬性有以下:
    /**
     @property (nonatomic) CGFloat minimumLineSpacing;
     @property (nonatomic) CGFloat minimumInteritemSpacing;
     @property (nonatomic) CGSize itemSize;
     @property (nonatomic) CGSize estimatedItemSize NS_AVAILABLE_IOS(8_0); // defaults to CGSizeZero - setting a non-zero size enables cells that self-size via -preferredLayoutAttributesFittingAttributes:
     @property (nonatomic) UICollectionViewScrollDirection scrollDirection; // default is UICollectionViewScrollDirectionVertical
     @property (nonatomic) CGSize headerReferenceSize;
     @property (nonatomic) CGSize footerReferenceSize;
     @property (nonatomic) UIEdgeInsets sectionInset;
     */
    // 定義大小
    layout.itemSize = CGSizeMake(100, 100);
    // 設置最小行間距
    layout.minimumLineSpacing = 2;
    // 設置垂直間距
    layout.minimumInteritemSpacing = 2;
    // 設置滾動方向(默認垂直滾動)
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    return [self initWithCollectionViewLayout:layout];
}
  • 這里我們使用xib自定義cell,通過xib注冊cell的代碼如下
// 通過xib注冊
  [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([CYXNormalCell class]) bundle:nil] forCellWithReuseIdentifier:reuseIdentifier];
  • 初步效果圖如下(這里就不詳細實現了,剩下請讀者參考UITableView的用法(請點這里)
    初步效果圖

2. 新手引導頁制作

  • 簡述
    • 新手引導頁,幾乎是每個應用都有的,目的為了告訴用戶應用的亮點,達到吸引用戶的作用。
    • 利用UICollectionView的優勢(循環利用)實現新手引導頁,既簡單又高效,何樂而不為呢。
  • 實現思路:
    • 1.把UICollectionView的每個cell的尺寸設置為跟屏幕一樣大;
layout.itemSize = [UIScreen mainScreen].bounds.size;
  • 2.設置為水平滾動方向,設置水平間距為0.
// 設置間距
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
// 設置滾動方向(默認垂直滾動)
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  • 3.開啟分頁滾動模式
// 開啟分頁
    self.collectionView.pagingEnabled = YES;
// 隱藏水平滾動條
    self.collectionView.showsHorizontalScrollIndicator = NO;
    // 取消彈簧效果
    self.collectionView.bounces = NO;
  • 以下是效果圖:
    新手引導頁

3. 圖片循環輪播器

4. 帶特效的圖片瀏覽器(自定義布局/上)

以下內容分為兩小節:
1> Github例子分析
2> 自己實現一個小Demo

(1)Github例子分析
  • 我們經常在Github上看到一些關于CollectionView的cell切換的炫酷效果,下面我們來分析一下Github上的這個卡片切換帶3D動畫Demo(RGCardViewLayout)<地址請點>

  • 下面是效果圖


    Github上的自定義布局Demo效果圖
  • 目錄結構分析:目錄結構一目了然,關鍵在于有一個自動布局類(如圖所示),這個類繼承自UICollectionViewFlowLayout(我們可以猜到他使用的是默認的流水布局),并重寫了- (void)prepareLayout、- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect等方法。我們試著把這個類里面的重載方法都注釋掉,得到的效果跟普通用法的效果一樣(這里就不截圖了)。由此可見,作者肯定在這些方法內做了個性化的設置。

    目錄

  • 關鍵源碼分析:

    • 首先我們看到,作者在- (void)prepareLayout方法內做了對collectionView的初始化布局操作。因此我們可以斷定重寫此方法是用做初始化的(讀者可以嘗試修改,改變效果)。
      - (void)prepareLayout
      {
         [super prepareLayout];
         [self setupLayout]; // 初始化布局
      }
      - (void)setupLayout
      {
          CGFloat inset  = self.collectionView.bounds.size.width * (6/64.0f);
          inset = floor(inset);
    
          self.itemSize = CGSizeMake(self.collectionView.bounds.size.width - (2 *inset), self.collectionView.bounds.size.height * 3/4);
          self.sectionInset = UIEdgeInsetsMake(0,inset, 0,inset);
          self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
      }
    
    • 接著這個- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect方法應該是最重要的了,同理,我們先注釋掉里面個性化的設置,只留[super layoutAttributesForElementsInRect:rect],我們發現炫酷的3D效果沒有了。因此可以斷定此方法是給每個Cell做個性化設置的。

方法解析:
這個方法的返回值是一個數組(數組里面存放著rect范圍內所有元素的布局屬性)
這個方法的返回值決定了rect范圍內所有元素的排布方式(frame)
UICollectionViewLayoutAttributes *attrs;
1.一個cell對應一個UICollectionViewLayoutAttributes對象
2.UICollectionViewLayoutAttributes對象決定了cell的frame

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
  // 獲取父類(流水布局)已經計算好的布局,在這個基礎上做個性化修改
  NSArray *attributes = [super layoutAttributesForElementsInRect:rect];
  
  NSArray *cellIndices = [self.collectionView indexPathsForVisibleItems];
  if(cellIndices.count == 0 )
  {
      return attributes;
  }
  else if (cellIndices.count == 1)
  {
      mainIndexPath = cellIndices.firstObject;
      movingInIndexPath = nil;
  }
  else if(cellIndices.count > 1)
  {
      NSIndexPath *firstIndexPath = cellIndices.firstObject;
      if(firstIndexPath == mainIndexPath)
      {
          movingInIndexPath = cellIndices[1];
      }
      else
      {
          movingInIndexPath = cellIndices.firstObject;
          mainIndexPath = cellIndices[1];
      }
      
  }
  
  difference =  self.collectionView.contentOffset.x - previousOffset;
  
  previousOffset = self.collectionView.contentOffset.x;
  // 關鍵代碼:取每一個Cell的布局屬性,并添加3D效果
  for (UICollectionViewLayoutAttributes *attribute in attributes)
  {
      [self applyTransformToLayoutAttributes:attribute];
  }
  return  attributes;
}
  • 上面關鍵方法都已經實現了,但是運行發現并沒有我們想要的效果,CollectionViewCell并沒有實時發生形變。y因此我們還需要調用以下方法。

方法解析:
只要滾動屏幕 就會調用 方法 -(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
只要布局頁面的屬性發生改變 就會重新調用 -(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect 這個方法

// indicate that we want to redraw as we scroll
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
  return YES;
}
(2)仿寫Demo
  • 經過上面對代碼的分析,我們可以簡單了解到自定義layout布局的基本實現,下面就可以仿寫一個簡單的Demo了,效果圖如下。


    效果圖
  • 參考代碼如下(詳細見Github)

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
    return YES;
}

- (void)prepareLayout{
    
    [super prepareLayout];
    
    self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    // 設置內邊距
    CGFloat inset = (self.collectionView.frame.size.width - self.itemSize.width) * 0.5;
    self.sectionInset = UIEdgeInsetsMake(0, inset, 0, inset);
    
}

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
    // 獲得super已經計算好的布局屬性
    NSArray *attributes = [super layoutAttributesForElementsInRect:rect];
    
    // 計算collectionView最中心點的x值
    CGFloat centerX = self.collectionView.contentOffset.x + self.collectionView.frame.size.width * 0.5;
    

    // 在原有布局屬性的基礎上,進行微調
    for (UICollectionViewLayoutAttributes *attrs in attributes) {
        // cell的中心點x 和 collectionView最中心點的x值 的間距
        CGFloat delta = ABS(attrs.center.x - centerX);
        // 根據間距值 計算 cell的縮放比例
        CGFloat scale = 1.2 - delta / self.collectionView.frame.size.width;
    
        NSLog(@"%f,%f",delta,scale);
        // 設置縮放比例
        attrs.transform = CGAffineTransformMakeScale(scale, scale);
    }
    
    return  attributes;
    
}

5.瀑布流布局(自定義布局/下)

  • 瀑布流布局在很多應用中非常常見,效果圖如下:
效果圖
實現思路(簡化)
  • (1)繼承自UICollectionViewLayout
  • (2)幾個需要重載的方法:
/*
 * 初始化
 */
- (void)prepareLayout;
/*
 * 返回rect中的所有的元素的布局屬性
 */
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect;
/*
 * 返回對應于indexPath的位置的cell的布局屬性
 */
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath;
/*
 * 返回collectionView的內容的尺寸
 */
- (CGSize)collectionViewContentSize;

關鍵計算代碼如下(詳細見Github)

/**
 * 返回indexPath位置cell對應的布局屬性
 */
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    // 創建布局屬性
    UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    
    // collectionView的寬度
    CGFloat collectionViewW = self.collectionView.frame.size.width;
    
    // 設置布局屬性的frame
    CGFloat w = (collectionViewW - self.edgeInsets.left - self.edgeInsets.right - (self.columnCount - 1) * self.columnMargin) / self.columnCount;
    CGFloat h = [self.delegate waterflowLayout:self heightForItemAtIndex:indexPath.item itemWidth:w];
    
    // 找出高度最短的那一列
    NSInteger destColumn = 0;
    CGFloat minColumnHeight = [self.columnHeights[0] doubleValue];
    for (NSInteger i = 1; i < self.columnCount; i++) {
        // 取得第i列的高度
        CGFloat columnHeight = [self.columnHeights[i] doubleValue];
        
        if (minColumnHeight > columnHeight) {
            minColumnHeight = columnHeight;
            destColumn = i;
        }
    }
    
    CGFloat x = self.edgeInsets.left + destColumn * (w + self.columnMargin);
    CGFloat y = minColumnHeight;
    if (y != self.edgeInsets.top) {
        y += self.rowMargin;
    }
    attrs.frame = CGRectMake(x, y, w, h);
    
    // 更新最短那列的高度
    self.columnHeights[destColumn] = @(CGRectGetMaxY(attrs.frame));
    
    // 記錄內容的高度
    CGFloat columnHeight = [self.columnHeights[destColumn] doubleValue];
    if (self.contentHeight < columnHeight) {
        self.contentHeight = columnHeight;
    }
    return attrs;
}

6. 布局切換

  • 蘋果已經為我們想好了布局切換的快捷方式,只需要通過以下方法,即可實現。

- (void)setCollectionViewLayout:(UICollectionViewLayout *)layout animated:(BOOL)animated; // transition from one layout to another
- (void)setCollectionViewLayout:(UICollectionViewLayout *)layout animated:(BOOL)animated completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(7_0);


附: 源碼github地址

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,119評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,382評論 3 415
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事?!?“怎么了?”我有些...
    開封第一講書人閱讀 176,038評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,853評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,616評論 6 408
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,112評論 1 323
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,192評論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,355評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,869評論 1 334
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,727評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,928評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,467評論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,165評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,570評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,813評論 1 282
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,585評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,892評論 2 372

推薦閱讀更多精彩內容