ios让你简单实现瀑布流

以下是方法实现的代码

#import <UIKit/UIKit.h>

@interface XMFWaterFall : UICollectionViewLayout

//  一行的列数
@property (nonatomic, assign) NSUInteger colCount;

//  collection内边距
@property (nonatomic, assign) UIEdgeInsets edgeInsets;

//  cell的间距
@property (nonatomic, assign) CGFloat space;

//  item宽
@property (nonatomic, assign) CGFloat itemWidth;

//  高度
@property (nonatomic, copy) CGFloat (^heightAction) (NSIndexPath *indexPath);

@end

#import "XMFWaterFall.h"

@interface XMFWaterFall ()

@property (nonatomic, strong) NSMutableArray<UICollectionViewLayoutAttributes *> *attributes;

@end

@implementation XMFWaterFall

//  第一步
- (void)prepareLayout {
    [super prepareLayout];
    
    NSUInteger totalCellNum = [self.collectionView numberOfItemsInSection:0];
    
    _attributes = [NSMutableArray<UICollectionViewLayoutAttributes *> arrayWithCapacity: totalCellNum];
    NSIndexPath *indexPath;
    for (int i = 0; i < totalCellNum; i++) {
        indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        [_attributes addObject: [self layoutAttributesForItemAtIndexPath:indexPath]];
    }

}

//  第二步
- (CGSize)collectionViewContentSize {
    CGFloat totalCellWidth = _edgeInsets.left + _edgeInsets.right + (_colCount - 1) * _space +_colCount * _itemWidth;
    return CGSizeMake(totalCellWidth, [self getMaxY] + _edgeInsets.bottom);
}

//  第三步
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    UICollectionViewLayoutAttributes *attributes    =   [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    NSUInteger currentColum                         =   indexPath.row % _colCount;
    CGFloat cellX                                   =   _edgeInsets.left + currentColum * (_itemWidth + _space); //  cell的x
    //  cell的y
    CGFloat cellY;
    if (indexPath.row + 1 > _colCount) {
        cellY = CGRectGetMaxY(_attributes[indexPath.row - _colCount].frame) + _space;
    }
    else
        cellY = _edgeInsets.top;
    
    //  cell的高
    CGFloat cellH = 0.0;
    if (_heightAction) {
        cellH = _heightAction(indexPath);
    }
    
    attributes.frame = CGRectMake(cellX, cellY, _itemWidth, cellH);
    
    return attributes;
}

//  第三步
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {    
    return _attributes;
}

/**
 *  获得最大y值
 */
- (CGFloat)getMaxY {
    NSUInteger totalCellNum = [self.collectionView numberOfItemsInSection:0];
    NSInteger lastLenght    = totalCellNum % _colCount;
    if (lastLenght == 0) lastLenght = _colCount;
    __block CGFloat tempY = 0.0;
    NSIndexSet *indexSet  = [NSIndexSet indexSetWithIndexesInRange: NSMakeRange(_attributes.count - lastLenght, lastLenght)];
    [_attributes enumerateObjectsAtIndexes:indexSet options:NSEnumerationConcurrent usingBlock:^(UICollectionViewLayoutAttributes * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        tempY     = MAX(tempY, CGRectGetMaxY(obj.frame));
    }];
    
    return tempY;
}

@end

这是一种简单实现瀑布流的方法,注释不多,如有不懂可以在QQ群 389400205上问我

以下是我的github可以在此下载代码 点击打开链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值