UICollectionView的瀑布流

###

想必大家已經對互聯網傳統的照片布局方式司空見慣了,這種行列分明的布局雖然對用戶來說簡潔明了,但是長久的使用難免會產生審美疲勞。現在網上流行一種叫做“瀑布流”的照片布局樣式,這種行與列參差不齊的狀態著實給用戶眼前一亮的感覺

效果圖

瀑布流

實現瀑布流的三種方式

  • 第一種:(最原始,最為麻煩) 使用一個UIScrollView上面放著三個UITabelView然后在禁止UITabelView的滑動,只允許背后的UIScrollView滑動
  • 第二種:(也比較麻煩)只是用UIScrollView,但是比較多的時候 我們也是需要重用,這時我們需要自己 設計出一套重用機制。
  • 第三種:(最簡單 UICollectionViewLayout) 在UICollectionView出來之后 我們通常都是使用這種方式來實現瀑布流,因為我們不需要設計重用機制。

我今天我們講的就是第三種方式來實現瀑布流
實現思路: 就是每一個cell都會加到最短的高度上


  • UICollectionViewLayout的實現
    我們先創建一個繼承于UICollectionViewLayout WaterLayout

  • .h 文件
#import <UIKit/UIKit.h>
//前置聲明
@class XMGWaterflowLayout;
//代理
@protocol XMGWaterflowLayoutDelegate <NSObject>

@required
//必須實現的代理方法
- (CGFloat)waterflowLayout:(XMGWaterflowLayout *)waterflowLayout heightForItemAtIndex:(NSUInteger)index itemWidth:(CGFloat)itemWidth;
//可實現可不實現的代理方法 這個是通過代理調用的接口  也可以 通過set方法來實現接口
@optional
- (CGFloat)columnCountInWaterflowLayout:(XMGWaterflowLayout *)waterflowLayout;
- (CGFloat)columnMarginInWaterflowLayout:(XMGWaterflowLayout *)waterflowLayout;
- (CGFloat)rowMarginInWaterflowLayout:(XMGWaterflowLayout *)waterflowLayout;
- (UIEdgeInsets)edgeInsetsInWaterflowLayout:(XMGWaterflowLayout *)waterflowLayout;
@end

@interface XMGWaterflowLayout : UICollectionViewLayout
/** 代理 */
@property (nonatomic, weak) id<XMGWaterflowLayoutDelegate> delegate;
@end
  • 在WaterLayout的.m文件
/** 默認的列數 */
static const NSInteger XMGDefaultColumnCount = 3;
/** 每一列之間的間距 */
static const CGFloat XMGDefaultColumnMargin = 10;
/** 每一行之間的間距 */
static const CGFloat XMGDefaultRowMargin = 10;
/** 邊緣間距 */
static const UIEdgeInsets XMGDefaultEdgeInsets = {10, 10, 10, 10};
  • 在.m的@interface classion中
/** 存放所有cell的布局屬性 */
@property (nonatomic, strong) NSMutableArray *attrsArray;
/** 存放所有列的當前高度 */
@property (nonatomic, strong) NSMutableArray *columnHeights;
/** 內容的高度 */
@property (nonatomic, assign) CGFloat contentHeight;
//這里方法為了簡化 數據處理(通過getter方法來實現)
- (CGFloat)rowMargin;
- (CGFloat)columnMargin;
- (NSInteger)columnCount;
- (UIEdgeInsets)edgeInsets;
  • 這些getter方法的實現 其他的同理
#pragma mark - 常見數據處理
- (CGFloat)rowMargin
{
//如果delegate方法沒有實現的話就使用代理方法 否則就使用默認值 然后通過getter方法來判斷 來簡化代碼
    if ([self.delegate respondsToSelector:@selector(rowMarginInWaterflowLayout:)]) {
        return [self.delegate rowMarginInWaterflowLayout:self];
    } else {
        return XMGDefaultRowMargin;
    }
}
  • .m文件中的瀑布流主要的實現
/**
 * 初始化
 */
- (void)prepareLayout
{
     [super prepareLayout];
    //先初始化內容的高度為0
     self.contentHeight = 0;
    // 清除以前計算的所有高度
    [self.columnHeights removeAllObjects];
    //先初始化  存放所有列的當前高度 3個值
    for (NSInteger i = 0; i < self.columnCount; i++) {
        [self.columnHeights addObject:@(self.edgeInsets.top)];
    }

    // 清除之前所有的布局屬性
    [self.attrsArray removeAllObjects];
    // 開始創建每一個cell對應的布局屬性
    NSInteger count = [self.collectionView numberOfItemsInSection:0];
    for (NSInteger i = 0; i < count; i++) {
        // 創建位置
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        // 獲取indexPath位置cell對應的布局屬性
        UICollectionViewLayoutAttributes *attrs = [self layoutAttributesForItemAtIndexPath:indexPath];
        [self.attrsArray addObject:attrs];
    }
}

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

/**
 * 返回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];
    
    // 找出高度最短的那一列
    //找出來最短后 就把下一個cell 添加到低下
    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;
}

/*!
 *  返回值就是這個CollectionView的contensize 因為CollectionView也是ScrollView的子類
 */
- (CGSize)collectionViewContentSize
{
//    CGFloat maxColumnHeight = [self.columnHeights[0] doubleValue];
//    for (NSInteger i = 1; i < self.columnCount; i++) {
//        // 取得第i列的高度
//        CGFloat columnHeight = [self.columnHeights[i] doubleValue];
//        
//        if (maxColumnHeight < columnHeight) {
//            maxColumnHeight = columnHeight;
//        }
//    }
    return CGSizeMake(0, self.contentHeight + self.edgeInsets.bottom);
}

  • 在ViewController中的使用
 // 創建布局
    XMGWaterflowLayout *layout = [[XMGWaterflowLayout alloc] init];
    layout.delegate = self;
//然后繼承代理實現代理方法

#pragma mark - <WaterLayoutDelegate>
//返回每個cell的高度
- (CGFloat)waterflowLayout:(XMGWaterflowLayout *)waterflowLayout heightForItemAtIndex:(NSUInteger)index itemWidth:(CGFloat)itemWidth
{
    XMGShop *shop = self.shops[index];
    return itemWidth * shop.h / shop.w;
}

//每行的最小距離
- (CGFloat)rowMarginInWaterflowLayout:(XMGWaterflowLayout *)waterflowLayout
{
    return 10;
}
//有多少列
- (CGFloat)columnCountInWaterflowLayout:(XMGWaterflowLayout *)waterflowLayout
{
    if (self.shops.count <= 50) return 2;
    return 3;
}

//內邊距
- (UIEdgeInsets)edgeInsetsInWaterflowLayout:(XMGWaterflowLayout *)waterflowLayout
{
    return UIEdgeInsetsMake(10, 20, 30, 100);
}

GitHub-->Demo
UIcollectionViewLayout的布局詳解

上一篇SquareLayout方形布局

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容