前言
超簡(jiǎn)單的瀑布流實(shí)現(xiàn)填具,這里說(shuō)一下筆者的思路,詳細(xì)代碼在這里
效果演示
實(shí)現(xiàn)思路
collectionView能實(shí)現(xiàn)各中吊炸天的布局陶耍,其精髓就在于UICollectionViewLayout犯戏,因此我們要自定義一個(gè)layout來(lái)繼承系統(tǒng)的UICollectionViewLayout,所有工作都在這個(gè)類中進(jìn)行
1.定義所需屬性
瀑布流的思路就是娘汞,從上往下歹茶,那一列最短,就把下一個(gè)item放在哪一列你弦,因此我們需要定義一個(gè)字典來(lái)記錄每一列的最大y值
每一個(gè)item都有一個(gè)attributes惊豺,因此定義一個(gè)數(shù)組來(lái)保存每一個(gè)item的attributes
我們還必須知道有多少列以及列間距、行間距禽作、section到collectionView的邊距
//總列數(shù)
@property (nonatomic, assign) NSInteger columnCount;
//列間距
@property (nonatomic, assign) NSInteger columnSpacing;
//行間距
@property (nonatomic, assign) NSInteger rowSpacing;
//section到collectionView的邊距
@property (nonatomic, assign) UIEdgeInsets sectionInset;
//保存每一列最大y值的數(shù)組
@property (nonatomic, strong) NSMutableDictionary *maxYDic;
//保存每一個(gè)item的attributes的數(shù)組
@property (nonatomic, strong) NSMutableArray *attributesArray;
2.重寫(xiě)系統(tǒng)方法
我們一共需要重寫(xiě)4個(gè)方法
a.- (void)prepareLayout
b.- (CGSize)collectionViewContentSize
c.- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
d.- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
- (void)prepareLayout 方法
布局前的一些準(zhǔn)備工作都在這里進(jìn)行
初始化字典尸昧,有幾列就有幾個(gè)鍵值對(duì),key為第幾列旷偿,value為列的最大y值烹俗,初始值為上內(nèi)邊距
for (int i = 0; i < self.columnCount; i++) {
self.maxYDic[@(i)] = @(self.sectionInset.top);
}
創(chuàng)建每個(gè)item的attributes,并存入數(shù)組
//根據(jù)collectionView獲取總共有多少個(gè)item
NSInteger itemCount = [self.collectionView numberOfItemsInSection:0];
//為每一個(gè)item創(chuàng)建一個(gè)attributes并存入數(shù)組
for (int i = 0; i < itemCount; i++) {
UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];
[self.attributesArray addObject:attributes];
}
- (CGSize)collectionViewContentSize 方法
用來(lái)計(jì)算collectionView的contentSize
一般瀑布流只能垂直滾動(dòng)萍程,不能水平滾動(dòng)幢妄,因此contentSize.width = 0,我們只需要計(jì)算contentSize.height即可
從字典中找出最長(zhǎng)列的最大y值茫负,再加上下面的內(nèi)邊距蕉鸳,即為contentSize.height
- (CGSize)collectionViewContentSize {
//假設(shè)第0列是最長(zhǎng)的那列
__block NSNumber *maxIndex = @0;
//遍歷字典,找出最長(zhǎng)的那一列
[self.maxYDic enumerateKeysAndObjectsUsingBlock:^(NSNumber *key, NSNumber *obj, BOOL *stop) {
//如果maxColumn列的最大y值小于obj忍法,則讓maxColumn等于obj所屬的列
if ([self.maxYDic[maxIndex] floatValue] < obj.floatValue) {
maxIndex = key;
}
}];
//collectionView的contentSize.height就等于最長(zhǎng)列的最大y值+下內(nèi)邊距
return CGSizeMake(0, [self.maxYDic[maxIndex] floatValue] + self.sectionInset.bottom);
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath 方法
該方法則用來(lái)設(shè)置每個(gè)item的attributes潮尝,在這里,我們只需要簡(jiǎn)單的設(shè)置每個(gè)item的attributes.frame即可
首先我們必須得知collectionView的尺寸饿序,然后我們根據(jù)collectionView的寬度勉失,以及列數(shù)、各個(gè)間距來(lái)計(jì)算每個(gè)item的寬度
item的寬度 = (collectionView的寬度 - 內(nèi)邊距及列邊距) / 列數(shù)
CGFloat collectionViewWidth = self.collectionView.frame.size.width;
//self.sectionInset.left:左邊距 self.sectionInset.right:右邊距
//(self.columnCount - 1) * columnSpacing:一行中所有的列邊距
CGFloat itemWidth = (collectionViewWidth - self.sectionInset.left - self.sectionInset.right - (self.columnCount - 1) * self.columnSpacing) / self.columnCount;
接下來(lái)計(jì)算item的坐標(biāo)嗤堰,要想計(jì)算坐標(biāo)戴质,那就必須知道最短的那一列,先遍歷字典踢匣,找出最短列是哪一列(minColumn)以及其最大y值
item的y值就等于最短列的最大y值再加上行間距告匠,x值就等于左邊距 + (item寬度 + 列間距) * minColumn
//找出最短的那一列
__block NSNumber *minIndex = @0;
[self.maxYDic enumerateKeysAndObjectsUsingBlock:^(NSNumber *key, NSNumber *obj, BOOL *stop) {
if ([self.maxYDic[minIndex] floatValue] > obj.floatValue) {
minIndex = key;
}
}];
//根據(jù)最短列的列數(shù)計(jì)算item的x值
CGFloat itemX = self.sectionInset.left + (self.columnSpacing + itemWidth) * minIndex.integerValue;
//item的y值 = 最短列的最大y值 + 行間距
CGFloat itemY = [self.maxYDic[minIndex] floatValue] + self.rowSpacing;
接下來(lái)便是item的高度,我們應(yīng)該根據(jù)圖片的原始尺寸以及計(jì)算出來(lái)的寬度离唬,等比例縮放來(lái)計(jì)算高度后专,但是在layout類中,我們是拿不到圖片的输莺,因此我們可以定義一個(gè)block屬性戚哎,或者代理裸诽,讓外界來(lái)計(jì)算并返回給我們,我們需要將item的寬度以及indexPath傳遞給外界
@property (nonatomic, strong) CGFloat(^itemHeightBlock)(CGFloat itemHeight,NSIndexPath *indexPath);
根據(jù)返回值來(lái)設(shè)置item的高度
if (self.itemHeightBlock) itemHeight = self.itemHeightBlock(itemWidth, indexPath);
最后設(shè)置attributes的frame并更新字典
//設(shè)置attributes的frame
attributes.frame = CGRectMake(itemX, itemY, itemWidth, itemHeight);
//更新字典中的最短列的最大y值
self.maxYDic[minIndex] = @(CGRectGetMaxY(attributes.frame));
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect 方法
該方法用來(lái)返回rect范圍內(nèi)型凳,item的attributes
直接返回attributesArray即可
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
return self.attributesArray;
}
使用
布局類寫(xiě)完了丈冬,接下來(lái)就可以直接使用了
//創(chuàng)建布局對(duì)象
XRWaterfallLayout *waterfall = [[XRWaterfallLayout alloc] init];
//設(shè)置相關(guān)屬性
waterfall.columnCount = 3;//共多少列
waterfall.columnSpacing = 10;//列間距
waterfall.rowSpacing = 10;//行間距
waterfall.sectionInset = UIEdgeInsetsMake(10, 10 , 10, 10);//內(nèi)邊距
[waterfall setItemHeightBlock:^CGFloat(CGFloat itemWidth, NSIndexPath *indexPath) {
//根據(jù)圖片的原始尺寸,及顯示寬度甘畅,等比例縮放來(lái)計(jì)算顯示高度
XRImage *image = self.images[indexPath.item];
return image.imageH / image.imageW * itemWidth;
}];
collectionView.collectionViewLayout = waterfall;
......
......
具體代碼請(qǐng)到這里下載https://github.com/codingZero/XRWaterfallLayout埂蕊,覺(jué)得不錯(cuò)的,請(qǐng)獻(xiàn)上你的star