利用“強(qiáng)大的UICollectionView”實(shí)現(xiàn)瀑布流

通過(guò)上篇強(qiáng)大的UICollectionView介紹了UICollectionView的基本方法,下面我們來(lái)利用UICollectionViewLayout實(shí)現(xiàn)瀑布流;

自定義UICollectionViewLayout

YANCollectionViewLayout.h

@class YANCollectionViewLayout;

@protocol YANCollectionViewLayoutDelegate <NSObject>

@required
- (CGFloat)collectionViewLayout:(YANCollectionViewLayout *)layout heightForItemAtIndext:(NSInteger)index itemWidth:(CGFloat)itemWidth;
@optional
/**
 *  設(shè)置列數(shù)
 *
 */
- (CGFloat)columnCountInCollectionFlowLayout:(YANCollectionViewLayout *)layout;
/**
 *  設(shè)置列間距
 *
 */
- (CGFloat)columnMarginInCollectionFlowLayout:(YANCollectionViewLayout *)layout;
/**
 *  設(shè)置行間距
 *
 */
- (CGFloat)rowMarginInCollectionFlowLayout:(YANCollectionViewLayout *)layout;
/**
 *  設(shè)置邊緣間距
 */
- (UIEdgeInsets)edgeInsetsInCollectionFlowLayout:(YANCollectionViewLayout *)layout;

@end

@interface YANCollectionViewLayout : UICollectionViewLayout

@property(nonatomic,weak)id<YANCollectionViewLayoutDelegate> delegate_;

@end

YANCollectionViewLayout.m

/**
 *  默認(rèn)的列數(shù)
 */
static const NSInteger defaultColumnCount = 3;
/**
 *  默認(rèn)的列間距
 */
static const CGFloat defaultColumnMargin = 10;
/**
 *  默認(rèn)的行間距
 */
static const CGFloat defaultRowMargin = 10;
/**
 *  默認(rèn)的邊緣間距
 */
static const UIEdgeInsets defaultEdgeInsets = {10,10,10,10};

@interface YANCollectionViewLayout ()

/**
 *  存放cell的布局屬性
 */
@property(nonatomic, strong)NSMutableArray *cellArray;
/**
 *  存放列的高度
 */
@property(nonatomic, strong)NSMutableArray *columnHeights;
/**
 *  cell內(nèi)容的高度
 */
@property(nonatomic, assign)CGFloat contentHeight;

- (CGFloat)rowMargin;
- (CGFloat)columnMargin;
- (CGFloat)columnCount;
- (UIEdgeInsets)edgeInsets;
@end

@implementation YANCollectionViewLayout

- (CGFloat)rowMargin{ // 沒(méi)有設(shè)置就使用默認(rèn)值

    if ([self.delegate_ respondsToSelector:@selector(rowMarginInCollectionFlowLayout:)]) {
        return [self.delegate_ rowMarginInCollectionFlowLayout:self];
    }else{
    
        return  defaultRowMargin;
    }
}
- (CGFloat)columnMargin{

    if ([self.delegate_ respondsToSelector:@selector(columnMarginInCollectionFlowLayout:)]) {
        return [self.delegate_ columnMarginInCollectionFlowLayout:self];
    }else{
    
        return defaultColumnMargin;
    }
}
- (CGFloat)columnCount{

    if ([self.delegate_ respondsToSelector:@selector(columnCountInCollectionFlowLayout:)]) {
        
        return [self.delegate_ columnCountInCollectionFlowLayout:self];
    }else{
    
        return defaultColumnCount;
    }
}
- (UIEdgeInsets)edgeInsets{

    if ([self.delegate_ respondsToSelector:@selector(edgeInsetsInCollectionFlowLayout:)]) {
        
        return [self.delegate_ edgeInsetsInCollectionFlowLayout:self];
        
    }else{
    
        return defaultEdgeInsets;
    }
}
- (NSMutableArray *)cellArray{

    if (!_cellArray) {
        _cellArray = [NSMutableArray array];
    }
    return _cellArray;
}

- (NSMutableArray *)columnHeights{

    if (!_columnHeights) {
        
        _columnHeights = [NSMutableArray array];
    }
    return _columnHeights;
}

- (void)prepareLayout{

    [super prepareLayout];
    
    self.contentHeight = 0;
    
    [self.columnHeights removeAllObjects];
    for (NSInteger i = 0; i < self.columnCount; i ++) {
        [self.columnHeights addObject:@(self.edgeInsets.top)];
    }
    [self.cellArray removeAllObjects];
    NSInteger count = [self.collectionView numberOfItemsInSection:0]; // 第一組有多少個(gè)cell
    for (NSInteger i = 0; i < count; i ++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        UICollectionViewLayoutAttributes *atts = [self layoutAttributesForItemAtIndexPath:indexPath];
        [self.cellArray addObject:atts];
    }
}

- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{

    return self.cellArray;
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath{

    UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    
    CGFloat collectionViewW = self.collectionView.frame.size.width;
    
    CGFloat w = (collectionViewW - self.edgeInsets.left - self.edgeInsets.right - (self.columnCount - 1)* self.columnMargin)/self.columnCount;
    
    CGFloat h = [self.delegate_ collectionViewLayout:self heightForItemAtIndext:indexPath.item itemWidth:w];
    
    NSInteger destColumn = 0;
    CGFloat minColumnHight = [self.columnHeights[0] doubleValue];
    for (NSInteger i = 1; i < self.columnCount; i ++) {
        
        CGFloat columnHeigt = [self.columnHeights[i] doubleValue];
        
        if (minColumnHight > columnHeigt) {
            
            minColumnHight = columnHeigt;
            destColumn = i;
        }
    }
    
    CGFloat x = self.edgeInsets.left + destColumn *(w + self.columnMargin);
    CGFloat y = minColumnHight;
    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;
}
- (CGSize)collectionViewContentSize{

    //collectionView的滾動(dòng)大小
    return CGSizeMake(0, self.contentHeight + self.edgeInsets.bottom);
}
@end

viewController.m

static NSString *ID = @"cell";
- (void)viewDidLoad {
    [super viewDidLoad];
    
    YANCollectionViewLayout *layout = [[YANCollectionViewLayout alloc] init];
    layout.delegate_ = self;
    
    UICollectionView *collection = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    collection.dataSource = self;
    collection.delegate = self;
    collection.contentInset = UIEdgeInsetsMake(20, 0, 0, 0);
    collection.backgroundColor = [UIColor grayColor];
    [self.view addSubview:collection];
    self.collectionView = collection;
    
    [collection registerClass:[YANCollectionCell class] forCellWithReuseIdentifier:ID];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return self.shopInfo.count;
}
#pragma mark - UICollectionViewDelegate
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    YANCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    cell.backgroundColor = [UIColor grayColor];
    cell.shop = self.shopInfo[indexPath.item];
    return cell;
    
}

#pragma mark - YANCollectionViewLayoutDelegate
- (CGFloat)collectionViewLayout:(YANCollectionViewLayout *)layout heightForItemAtIndext:(NSInteger)index itemWidth:(CGFloat)itemWidth{
    
    YANShop *shop = self.shopInfo[index];
    return itemWidth * shop.h / shop.w;
}
- (CGFloat)rowMarginInCollectionFlowLayout:(YANCollectionViewLayout *)layout{

    return 15;
}
- (CGFloat)columnCountInCollectionFlowLayout:(YANCollectionViewLayout *)layout{

    return 4;
}
- (CGFloat)columnMarginInCollectionFlowLayout:(YANCollectionViewLayout *)layout{

    return 15;
}
- (UIEdgeInsets)edgeInsetsInCollectionFlowLayout:(YANCollectionViewLayout *)layout{

    return UIEdgeInsetsMake(10, 10, 10, 10);
}
效果圖

demo地址

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,259評(píng)論 4 61
  • 桌面上的陰影是Launcher3加上去的,原生壁紙是沒(méi)有這層陰影。 陰影圖片:workspace_bg.9.png...
    skymhz閱讀 678評(píng)論 0 0
  • 海國(guó)桃源楊家溪, 山青水碧樂(lè)飄流。 洞怪巖奇榕楓美, 人文景觀(guān)任君游。 故里村前清溪長(zhǎng), 青山綠水似畫(huà)廊。 王郎題...
    文貝閱讀 636評(píng)論 15 25