//
// 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
UICollectionView自定义实现瀑布流
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 前言 UICollectionView是个强大的视图,除了提供默认的UICollectionViewFlowLay...
- 前言 本篇文章不是分享collectionView的详细使用教程, 而是属于比较'高级'的collectionVi...
- 原文地址:http://www.cocoachina.com/ios/20160622/16784.html 本文...
- 上篇博客的实例是自带的UICollectionViewDelegateFlowLayout布局基础上来做的Demo...
- 前言: 本篇文章不是分享collectionView的详细使用教程, 而是属于比较'高级'的collectionV...