UICollectionView自定义实现瀑布流

//
//  vanFlowLayout.m
//  flowLayoutTest
//
//  Created by ZAKER on 2019/5/30.
//  Copyright © 2019 ZAKER. All rights reserved.
//

#import "vanFlowLayout.h"

@interface vanFlowLayout ()

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

@property (nonatomic, assign) CGFloat topMargin;

@property (nonatomic, assign) CGFloat leftSet;

@property (nonatomic, assign) CGFloat rightSet;

@property (nonatomic, assign) NSInteger cols;

@property (nonatomic, strong) NSMutableArray *heightArr;

@end

@implementation vanFlowLayout

- (instancetype)init{
    if (self = [super init]) {
        _topMargin = 10;
        _leftSet = 10;
        _rightSet = 10;
        _cols = 3;
        self.heightArr = [NSMutableArray arrayWithCapacity:self.cols];
        [self.heightArr addObject:[NSNumber numberWithFloat:self.topMargin]];
        [self.heightArr addObject:[NSNumber numberWithFloat:self.topMargin]];
        [self.heightArr addObject:[NSNumber numberWithFloat:self.topMargin]];
    }
    return self;
}

- (void)prepareLayout{
    [super prepareLayout];

    self.itemarr = [NSMutableArray array];
    
    for (int i = 0; i<20; i++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
       UICollectionViewLayoutAttributes *attr = [self layoutAttributesForItemAtIndexPath:indexPath];
        [self.itemarr addObject:attr];
    }
}

- (nullable NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    [super layoutAttributesForElementsInRect:rect];
    
    return self.itemarr;
}
- (nullable UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
    [super layoutAttributesForItemAtIndexPath:indexPath];
    
    UICollectionViewLayoutAttributes *attr = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    
    CGFloat attrWidth = ([UIScreen mainScreen].bounds.size.width - _leftSet - _rightSet - (_cols - 1) *10)/_cols;
    CGFloat attrHeight = arc4random_uniform(100) + 100;
    
    CGFloat minH = [self minValue];
    NSInteger ownCol = [self.heightArr indexOfObject:[NSNumber numberWithFloat:minH]];
    CGFloat attrY = minH + 10;
    CGFloat attrX = self.leftSet + ownCol *(10 + attrWidth);
    attr.frame = CGRectMake(attrX, attrY, attrWidth, attrHeight);
    [self.heightArr replaceObjectAtIndex:ownCol withObject:[NSNumber numberWithFloat:(attrY + attrHeight)]];
    
    
    return attr;
}

- (CGFloat)minValue{
    CGFloat minValue = [self.heightArr.firstObject floatValue];
    for (NSNumber *value in self.heightArr) {
        if (minValue > value.floatValue) {
            minValue = value.floatValue;
        }
    }
    return minValue;
}

- (CGFloat)maxValue{
    
    CGFloat maxValue = [self.heightArr.firstObject floatValue];
    for (NSNumber *value in self.heightArr) {
        if (maxValue < value.floatValue) {
            maxValue = value.floatValue;
        }
    }
    return maxValue;
    
}

- (CGSize)collectionViewContentSize{
    [super collectionViewContentSize];
    
    return CGSizeMake(0, [self maxValue] + 10);
}

@end


©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容