iOS瀑布流

瀑布流Demo


瀑布流截圖.gif

使用UICollectionView實現瀑布流

自定義UICollectionViewLayout中的主要代碼:

YJWaterFlowLayout.h中代碼:

#import <UIKit/UIKit.h>
@class YJWaterFlowLayout;

@protocol YJWaterFlowLayoutDelegate <NSObject>
@required
-(CGFloat)waterFlowLayout:(YJWaterFlowLayout *)waterFlowLayout heightForItemAtIndexPath:(NSUInteger)index itemWidth:(CGFloat)itemWidth;

@optional
/** 列數*/
-(CGFloat)columnCountInWaterFlowLayout:(YJWaterFlowLayout *)waterFlowLayout;
/** 列間距*/
-(CGFloat)columnMarginInWaterFlowLayout:(YJWaterFlowLayout *)waterFlowLayout;
/** 行間距*/
-(CGFloat)rowMarginInWaterFlowLayout:(YJWaterFlowLayout *)waterFlowLayout;
/** 邊緣之間的間距*/
-(UIEdgeInsets)edgeInsetInWaterFlowLayout:(YJWaterFlowLayout *)waterFlowLayout;

@end

@interface YJWaterFlowLayout : UICollectionViewLayout

/** delegate*/
@property (nonatomic, weak) id<YJWaterFlowLayoutDelegate> delegate;

@end

YJWaterFlowLayout.m文件代碼:


#import "YJWaterFlowLayout.h"
/** 默認的列數*/
static const NSInteger YJDefaultColumeCount = 3;
/** 每一列之間的間距*/
static const NSInteger YJDefaultColumeMargin = 10;
/** 每一行之間的間距*/
static const CGFloat YJDefaultRowMargin = 10;
/** 邊緣之間的間距*/
static const UIEdgeInsets YJDefaultEdgeInset = {10, 10, 10, 10};

@interface YJWaterFlowLayout ()
/** 存放所有cell的布局屬性*/
@property (strong, nonatomic) NSMutableArray *attrsArray;
/** 存放每一列的最大y值*/
@property (nonatomic, strong) NSMutableArray *columnHeights;
/** 每一行之間的間距*/
-(CGFloat)rowMargin;
/** 每一列之間的間距*/
-(CGFloat)columnMargin;
/** 列數*/
-(NSInteger)columnCount;
/** 邊緣之間的間距*/
-(UIEdgeInsets)edgeInsets;

/** 內容的高度*/
@property (nonatomic, assign) CGFloat maxColumnHeight;

@end

@implementation YJWaterFlowLayout

#pragma mark 代理方法處理
-(CGFloat)rowMargin {
    if ([self.delegate respondsToSelector:@selector(rowMarginInWaterFlowLayout:)]) {
       return [self.delegate rowMarginInWaterFlowLayout:self];
    } else {
        return YJDefaultRowMargin;
    }
}

-(CGFloat)columnMargin {
    if ([self.delegate respondsToSelector:@selector(columnMarginInWaterFlowLayout:)]) {
        return [self.delegate columnMarginInWaterFlowLayout:self];
    } else {
        return YJDefaultColumeMargin;
    }
}

-(NSInteger)columnCount {
    if ([self.delegate respondsToSelector:@selector(columnCountInWaterFlowLayout:)]) {
        return [self.delegate columnCountInWaterFlowLayout:self];
    } else {
        return YJDefaultColumeCount;
    }
}

-(UIEdgeInsets)edgeInsets {
    if ([self.delegate respondsToSelector:@selector(edgeInsetInWaterFlowLayout:)]) {
        return [self.delegate edgeInsetInWaterFlowLayout:self];
    } else {
        return YJDefaultEdgeInset;
    }
}

#pragma mark - 懶加載
- (NSMutableArray *)columnHeights {
    if (!_columnHeights) {
        _columnHeights = [NSMutableArray array];
    }
    return _columnHeights;
}

-(NSMutableArray *)attrsArray {
    if (_attrsArray == nil) {
        _attrsArray = [NSMutableArray array];
    }
    return _attrsArray;
}

/** 初始化*/
-(void)prepareLayout {
    [super prepareLayout];
    
    //清除以前計算的所有高度
    self.maxColumnHeight = 0;
    [self.columnHeights removeAllObjects];
    for (NSInteger i = 0; i < self.columnCount; i++) {
        [self.columnHeights addObject:@(self.edgeInsets.top)];
    }
    
    //清除之前數組
    [self.attrsArray removeAllObjects];
    
    //開始創(chuàng)建每一個cell
    NSInteger count = [self.collectionView numberOfItemsInSection:0];
    for (NSInteger i = 0; i < count; i++) {
        //創(chuàng)建位置
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        //獲取indexPath位置cell對應的成員屬性
        UICollectionViewLayoutAttributes *attrs = [self layoutAttributesForItemAtIndexPath:indexPath];
        [self.attrsArray addObject:attrs];
    }
}

/** 決定cell的排布*/
-(NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    return self.attrsArray;
}

/** 返回indexPath位置cell對應的布局屬性*/
-(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
    //設置布局屬性
    UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes  layoutAttributesForCellWithIndexPath:indexPath];
    //collectionView的寬度
    CGFloat collectionW = self.collectionView.frame.size.width;
    //設置布局屬性的frame
    CGFloat w = (collectionW - self.edgeInsets.left - self.edgeInsets.right - (self.columnCount - 1) * self.columnMargin) / self.columnCount;
    CGFloat h = [self.delegate waterFlowLayout:self heightForItemAtIndexPath: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.rowMargin);
    CGFloat y = minColumnHeight;
    if (y != self.edgeInsets.top) {
        if ([self.delegate respondsToSelector:@selector(rowMarginInWaterFlowLayout:)]) {
            y += [self.delegate rowMarginInWaterFlowLayout:self];
        } else {
            y += self.rowMargin;
        }
    }
    
    attrs.frame = CGRectMake(x, y, w, h);
    //更新最短那列的高度
    self.columnHeights[destColumn] = @(CGRectGetMaxY(attrs.frame));
    //記錄內容的高度
    CGFloat columnHeight = [self.columnHeights[destColumn] doubleValue];
    if (self.maxColumnHeight < columnHeight) {
        self.maxColumnHeight = columnHeight;
    }
    
    return attrs;
}


-(CGSize)collectionViewContentSize {
    return CGSizeMake(0, self.maxColumnHeight + self.edgeInsets.bottom);
}

@end

瀑布流Demo地址

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市醉蚁,隨后出現的幾起案子爷辙,更是在濱河造成了極大的恐慌华弓,老刑警劉巖萨惑,帶你破解...
    沈念sama閱讀 211,194評論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件隔心,死亡現場離奇詭異掠剑,居然都是意外死亡,警方通過查閱死者的電腦和手機盘榨,發(fā)現死者居然都...
    沈念sama閱讀 90,058評論 2 385
  • 文/潘曉璐 我一進店門喻粹,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人草巡,你說我怎么就攤上這事守呜。” “怎么了山憨?”我有些...
    開封第一講書人閱讀 156,780評論 0 346
  • 文/不壞的土叔 我叫張陵查乒,是天一觀的道長。 經常有香客問我郁竟,道長玛迄,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,388評論 1 283
  • 正文 為了忘掉前任棚亩,我火速辦了婚禮蓖议,結果婚禮上,老公的妹妹穿的比我還像新娘讥蟆。我一直安慰自己勒虾,他們只是感情好,可當我...
    茶點故事閱讀 65,430評論 5 384
  • 文/花漫 我一把揭開白布瘸彤。 她就那樣靜靜地躺著修然,像睡著了一般。 火紅的嫁衣襯著肌膚如雪钧栖。 梳的紋絲不亂的頭發(fā)上低零,一...
    開封第一講書人閱讀 49,764評論 1 290
  • 那天婆翔,我揣著相機與錄音拯杠,去河邊找鬼。 笑死啃奴,一個胖子當著我的面吹牛潭陪,可吹牛的內容都是我干的。 我是一名探鬼主播最蕾,決...
    沈念sama閱讀 38,907評論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼依溯,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了瘟则?” 一聲冷哼從身側響起黎炉,我...
    開封第一講書人閱讀 37,679評論 0 266
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎醋拧,沒想到半個月后慷嗜,有當地人在樹林里發(fā)現了一具尸體淀弹,經...
    沈念sama閱讀 44,122評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 36,459評論 2 325
  • 正文 我和宋清朗相戀三年庆械,在試婚紗的時候發(fā)現自己被綠了薇溃。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,605評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡缭乘,死狀恐怖沐序,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情堕绩,我是刑警寧澤策幼,帶...
    沈念sama閱讀 34,270評論 4 329
  • 正文 年R本政府宣布,位于F島的核電站逛尚,受9級特大地震影響垄惧,放射性物質發(fā)生泄漏。R本人自食惡果不足惜绰寞,卻給世界環(huán)境...
    茶點故事閱讀 39,867評論 3 312
  • 文/蒙蒙 一到逊、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧滤钱,春花似錦觉壶、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,734評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至他炊,卻和暖如春争剿,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背痊末。 一陣腳步聲響...
    開封第一講書人閱讀 31,961評論 1 265
  • 我被黑心中介騙來泰國打工蚕苇, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人凿叠。 一個月前我還...
    沈念sama閱讀 46,297評論 2 360
  • 正文 我出身青樓涩笤,卻偏偏與公主長得像,于是被迫代替她去往敵國和親盒件。 傳聞我的和親對象是個殘疾皇子蹬碧,可洞房花燭夜當晚...
    茶點故事閱讀 43,472評論 2 348

推薦閱讀更多精彩內容

  • 傳統(tǒng)!!!依然是效果演示 特點:可以自由設置瀑布流的總列數(效果演示為2列) 雖然iphone手機的系統(tǒng)相冊沒有使...
    Macgx閱讀 1,801評論 14 13
  • 看完千篇一律的UI布局之后,當我們看到瀑布流的布局是不是覺得有種耳目一新的感覺呢?今天我們就說一下如果實現瀑布流,...
    朱凱奇閱讀 3,100評論 5 43
  • 發(fā)現 關注 消息 iOS 第三方庫、插件炒刁、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,059評論 4 62
  • 最新研究了下iOS中瀑布流的實現方法恩沽,一般瀑布流都采用ScrollView + UITableView,或者是使用...
    Aeron_Xie閱讀 1,464評論 2 17
  • 一大早就被凍醒翔始,窗外竟然下起了雨罗心,感覺到身上由于寒冷自動拽過來的被子片吊,突然醒悟,處暑都過去好幾天了协屡,秋天真...
    大大大大大西瓜G閱讀 216評論 0 2