//
// 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自定義實現(xiàn)瀑布流
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來愕鼓,“玉大人钙态,你說我怎么就攤上這事」交危” “怎么了册倒?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長磺送。 經(jīng)常有香客問我驻子,道長,這世上最難降的妖魔是什么估灿? 我笑而不...
- 正文 為了忘掉前任崇呵,我火速辦了婚禮,結(jié)果婚禮上馅袁,老公的妹妹穿的比我還像新娘域慷。我一直安慰自己,他們只是感情好汗销,可當我...
- 文/花漫 我一把揭開白布犹褒。 她就那樣靜靜地躺著,像睡著了一般弛针。 火紅的嫁衣襯著肌膚如雪叠骑。 梳的紋絲不亂的頭發(fā)上,一...
- 文/蒼蘭香墨 我猛地睜開眼璧帝,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了富寿?” 一聲冷哼從身側(cè)響起,我...
- 正文 年R本政府宣布麻捻,位于F島的核電站纲仍,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏贸毕。R本人自食惡果不足惜郑叠,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望明棍。 院中可真熱鬧乡革,春花似錦、人聲如沸摊腋。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽歌豺。三九已至推穷,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間类咧,已是汗流浹背馒铃。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- 前言 UICollectionView是個強大的視圖堕虹,除了提供默認的UICollectionViewFlowLay...
- 前言 本篇文章不是分享collectionView的詳細使用教程, 而是屬于比較'高級'的collectionVi...
- 原文地址:http://www.cocoachina.com/ios/20160622/16784.html 本文...
- 上篇博客的實例是自帶的UICollectionViewDelegateFlowLayout布局基礎(chǔ)上來做的Demo...
- 前言: 本篇文章不是分享collectionView的詳細使用教程, 而是屬于比較'高級'的collectionV...