国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

詳解IOS中如何實(shí)現(xiàn)瀑布流效果

2020-07-26 03:16:57
字體:
供稿:網(wǎng)友

首先是效果演示

特點(diǎn):可以自由設(shè)置瀑布流的總列數(shù)(效果演示為2列)

雖然iphone手機(jī)的系統(tǒng)相冊(cè)沒有使用這種布局效果,瀑布流依然是一種很常見的布局方式!!!下面來詳細(xì)介紹如何實(shí)現(xiàn)這種布局.

首先使用的類是UICollectionView

我們要做的是自定義UICollectionViewCell和UICollectionViewLayout

     1、自定義UICollectionViewCell類,只需要一個(gè)UIImageView即可,frame占滿整個(gè)cell.

     2、重點(diǎn)是自定義UICollectionViewLayout,注意一定要繼承于UICollectionViewLayout,千萬別繼承于UIColletionViewFlowLayout.

     3、另外還需要計(jì)算圖片高度.

為什么要自定義UICollectionViewLayout ?

因?yàn)槲覀冃枰O(shè)置每個(gè)item的高度以及位置, 注意這里是位置, 我們真的會(huì)設(shè)置每個(gè)item的位置的相信我!!!自定義UICollectionViewLayout必須要重寫三個(gè)協(xié)議方法,后面會(huì)講到.

為什么要計(jì)算圖片高度 ?

因?yàn)閳D片寬度固定,所以需要按照?qǐng)D片的比例來計(jì)算高度,使圖片等比例顯示.這樣的好處是,媽媽再也不用擔(dān)心我的照片被拉伸的奇形怪狀了...而且還需要用圖片的高度來計(jì)算整個(gè)CollectionView的contentSize...打完收工!!!

主菜來了!!!

以下內(nèi)容均在自定義的CustomCollectionViewLayout類里邊

//自定義UICollectionViewLayout必須要重寫的三個(gè)協(xié)議方法//1.計(jì)算每個(gè)item的大小和位置- (void)prepareLayout;//2.返回每個(gè)item的布局屬性- (nullable NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect;//3.返回collectionView的總高度- (CGSize)collectionViewContentSize;

可以看到第三個(gè)方法使用了Nullability和泛型,系統(tǒng)的方法都添加了iOS 9新特性。

通過上邊的三個(gè)方法名我們可以大致了解需要去做什么.說一下第二個(gè)方法,需要返回一個(gè)數(shù)組,數(shù)組存放布局屬性(UICollectionViewLayoutAttributes)對(duì)象.那么我們需要寫一個(gè)屬性數(shù)組(attributesArray),將布局屬性放入這個(gè)屬性數(shù)組并返回.

還需要什么呢 ?看了文章開頭的朋友應(yīng)該注意到了,設(shè)置瀑布流的列數(shù)當(dāng)然得有個(gè)屬性(numberOfColumns)來表示列數(shù).

請(qǐng)注意,因?yàn)橐谕獠吭O(shè)置列數(shù),所以這個(gè)屬性需要寫在自定義類的.h文件中

另外為了方便,定義一個(gè)屬性(itemWidth)來表示item的寬度

再定義一個(gè)屬性(contentHeight)來表示整個(gè)collectionView的contenView的高度

首先是初始化,并沒有什么問題

- (instancetype)init {  self = [super init];  if (self) {    _attributesArray = [NSMutableArray array];    // 默認(rèn)值設(shè)置為2列    _numberOfColumns = 2;    _contentHeight = 0.0f;    _cellMargin = 5.0f;/**< 用來表示間距的屬性 */  }  return self;}

然后是getter方法,只需要使用點(diǎn)語法即可得到itemWidth的值(因?yàn)樗褪枪潭ǖ?

- (CGFloat)itemWidth {  //所有邊距的和.兩列時(shí)有三個(gè)邊距, 三列時(shí)有四個(gè)邊距,邏輯強(qiáng)大就是好...  CGFloat allMargin = (_numberOfColumns + 1) * _cellMargin;  //除去邊界之后的總寬度  CGFloat noMarginWidth = CGRectGetWidth(self.collectionView.bounds) - allMargin;  //出去邊距的總寬度除以列數(shù)得到每一列的寬度(也就是itemWidth)  return noMarginWidth / _numberOfColumns;}

---接下來是難點(diǎn)---

必須重寫的第一個(gè)方法

- (void)prepareLayout {  // 定義變量記錄高度最小的列,初始為第0列高度最小.#pragma mark - 注意這個(gè)是從0開始算的啊!!!  NSInteger shortestColumn = 0;#pragma mark - 注意這個(gè)是從0開始算的啊!!!  // 存儲(chǔ)每一列的總高度.因?yàn)樘砑訄D片的列高度會(huì)變,所以需要定義一個(gè)數(shù)組來記錄列的總高度.  NSMutableArray *columnHeightArray = [NSMutableArray array];  // 設(shè)置列的初始高度為邊距的高度,沒毛病!!!  for (int i = 0; i < _numberOfColumns; i++) {    // 所有列初始高度均設(shè)置為cell的間距    [columnHeightArray addObject:@(_cellMargin)];  }  // 遍歷collectionView中第 0 區(qū)中的所有item  for (int i = 0; i < [self.collectionView numberOfItemsInSection:0]; i++) {    //需要用到這個(gè)玩意,提前拿到.    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];    // 創(chuàng)建系統(tǒng)需要的布局屬性對(duì)象,看后邊的參數(shù)就知道這就是每個(gè)item的布局屬性了    UICollectionViewLayoutAttributes *layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath: indexPath];    // 將布局屬性放入數(shù)組中,這個(gè)數(shù)組當(dāng)然是一開始定義的布局屬性數(shù)組了    [_attributesArray addObject:layoutAttributes];    // 設(shè)置每個(gè)item的位置(x, y, width, height)    // 橫坐標(biāo)的起始位置#pragma mark - 比如一共兩列,現(xiàn)在要放一張圖片上去,需要放到高度最小的那一列.#pragma mark - 假設(shè)第0列最短,那么item的x坐標(biāo)就是從一個(gè)邊距寬度那里開始.#pragma mark - (itemWidth + cellMargin)為一個(gè)整體    CGFloat x = (self.itemWidth + _cellMargin) * shortestColumn + _cellMargin;    // 縱坐標(biāo)就是 總高度數(shù)組 中最小列對(duì)應(yīng)的高度#pragma mark - 圖片始終是添加在高度最小的那一列    CGFloat y = [columnHeightArray[column] floatValue];/**<注意類型轉(zhuǎn)換 */    // 寬度沒什么好說的    CGFloat width = self.itemWidth;#pragma mark - 這里給自定義的類聲明了一個(gè)協(xié)議,通過協(xié)議得到圖片的高度,調(diào)用時(shí)機(jī)就是需要item高度的時(shí)候#pragma mark - 將Item的寬度傳給代理人(ViewController),VC計(jì)算好高度后將高度返回給自定義類#pragma mark - 也就是↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓    CGFloat height = [self.delegate collectionView:self.collectionView                        layout:self                         width:self.itemWidth               heightForItemAtIndexPath:indexPath];    // 大功告成,設(shè)置item的位置信息,沒什么好說    layoutAttributes.frame = CGRectMake(x, y, width, height);    // 上邊廢了半天勁放了一個(gè)item上去了,總高度數(shù)組是不是該更新一下數(shù)據(jù)了    columnHeightArray[shortestColumn] = @([columnHeightArray[shortestColumn] floatValue] + height + _cellMargin);    // 整個(gè)內(nèi)容的高度,通過比較得到較大值作為整個(gè)內(nèi)容的高度    self.contentHeight = MAX(self.contentHeight, [columnHeightArray[shortestColumn] floatValue]);    // 剛才放了一個(gè)item上去,那么此時(shí)此刻哪一列的高度比較低呢    for (int i = 0; i < _numberOfColumns; i++) {      //當(dāng)前列的高度(剛才添加item的那一列)      CGFloat currentHeight = [columnHeightArray[shortestColumn] floatValue];      // 取出第i列中存放列高度      CGFloat height = [columnHeightArray[i] floatValue];      if (currentHeight > height) {        //第i列高度(height)最低時(shí),高度最低的列(shortestColumn)當(dāng)然就是第i列了        shortestColumn = i;      }    }  }// 思考下只使用上邊的代碼會(huì)出現(xiàn)什么問題// 并不能影響心情,請(qǐng)不要恐慌...// 提示:這個(gè)方法會(huì)被多次調(diào)用,數(shù)組}

---難點(diǎn)已經(jīng)結(jié)束---

沒有理解的朋友請(qǐng)重點(diǎn)看上邊的難點(diǎn)方法.

必須重寫的第二個(gè)方法

// 2.返回的是, 每個(gè)item對(duì)應(yīng)的布局屬性- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {  return _attributesArray;}

必須重寫的第三個(gè)方法

// 3.返回CollectionView的滾動(dòng)范圍- (CGSize)collectionViewContentSize {  return CGSizeMake(0, _contentHeight);}

ViewController里邊關(guān)于CollectionView的創(chuàng)建和協(xié)議方法就沒什么好說的了.

看下自定義類CustomCollectionViewLayout的創(chuàng)建以及屬性的賦值情況:

CustomCollectionViewLayout *layout = [[CustomCollectionViewLayout alloc] init];layout.numberOfColumns = 2;/**< 在ViewController里設(shè)置瀑布流的列數(shù),2列或3列為最佳 */layout.delegate = self;/**< 指定VC為計(jì)算高度協(xié)議方法的代理人 */

再看下協(xié)議方法的實(shí)現(xiàn)部分(在ViewController.m中實(shí)現(xiàn))

- (CGFloat)collectionView:(UICollectionView *)collectionView          layout:(UICollectionViewLayout *)collectionViewLayout          width:(CGFloat)width heightForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {  UIImage *image = _imagesArray[indexPath.row];  // 根據(jù)傳過來的寬度來設(shè)置一個(gè)合適的矩形, 高度設(shè)為CGFLOAT_MAX表示以寬度來計(jì)算高度  CGRect boundingRect = CGRectMake(0, 0, width, CGFLOAT_MAX);  // 通過系統(tǒng)函數(shù)來得到最終的矩形,需要引入頭文件  // #import <AVFoundation/AVFoundation.h>  CGRect imageCurrentRect = AVMakeRectWithAspectRatioInsideRect(image.size, boundingRect);  return imageCurrentRect.size.height;}

總結(jié)

到這里呢,在IOS實(shí)現(xiàn)瀑布流就算是結(jié)束了,有興趣的朋友可以自己動(dòng)手試一下,希望本文對(duì)大家開發(fā)IOS有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 郓城县| 宜丰县| 苗栗市| 渭南市| 蓬莱市| 峡江县| 嘉义县| 鄢陵县| 佛坪县| 平乡县| 九台市| 连城县| 建宁县| 奉节县| 乐业县| 攀枝花市| 化德县| 乐亭县| 广安市| 西丰县| 龙胜| 武冈市| 武平县| 商城县| 达日县| 怀安县| 江油市| 治多县| 荣昌县| 府谷县| 日喀则市| 大化| 宁陕县| 宿松县| 潞城市| 温宿县| 舟山市| 巴中市| 夏河县| 秦皇岛市| 乐亭县|