// 代码参考小码哥
//
// Ago_flowLatout.h
// 瀑布流-text
//
// Created by mac on 16/5/23.
// Copyright © 2016年 hkzr. All rights reserved.
//
#import <UIKit/UIKit.h>
@class Ago_flowLatout;
@protocol Ago_flowLatoutDelegate <NSObject>
@required
//设置代理设置图片的高度
- (CGFloat)waterflowLayout:(Ago_flowLatout *)waterflowLayout heightForItemAtIndex:(NSUInteger)index itemWidth:(CGFloat)itemWidth;
@optional
//列数
- (CGFloat)columnCountInWaterflowLayout:(Ago_flowLatout *)waterflowLayout;
//列间距
- (CGFloat)columnMarginInWaterflowLayout:(Ago_flowLatout *)waterflowLayout;
//行间距
- (CGFloat)rowMarginInWaterflowLayout:(Ago_flowLatout *)waterflowLayout;
//edge设置
- (UIEdgeInsets)edgeInsetsInWaterflowLayout:(Ago_flowLatout *)waterflowLayout;
@end
@interface Ago_flowLatout : UICollectionViewLayout
@property (nonatomic,weak)id <Ago_flowLatoutDelegate> delegate;
@end
//
// Ago_flowLatout.m
// 瀑布流-text
//
// Created by mac on 16/5/23.
// Copyright © 2016年 hkzr. All rights reserved.
//
#import "Ago_flowLatout.h"
/**默认的列数*/
static const NSInteger DefaultColumnCount = 3;
/** 每一列之间的间距 */
static const CGFloat DefaultColumnMargin = 10;
/** 每一行之间的间距 */
static const CGFloat DefaultRowMargin = 10;
/** 边缘间距 */
static const UIEdgeInsets DefaultEdgeInsets = {10, 10, 10, 10};
@interface Ago_flowLatout()
/** 存放所有cell的布局属性 */
@property (nonatomic, strong) NSMutableArray *attrsArray;
/** 存放所有列的当前高度 */
@property (nonatomic, strong) NSMutableArray *columnHeights;
- (CGFloat)rowMargin;
- (CGFloat)columnMargin;
- (NSInteger)columnCount;
- (UIEdgeInsets)edgeInsets;
@end
@implementation Ago_flowLatout
#pragma 手动设置各种间距
- (CGFloat)rowMargin
{
if ([self.delegate respondsToSelector:@selector(rowMarginInWaterflowLayout:)]) {
return [self.delegate rowMarginInWaterflowLayout:self];
} else {
return DefaultRowMargin;
}
}
- (CGFloat)columnMargin
{
if ([self.delegate respondsToSelector:@selector(columnMarginInWaterflowLayout:)]) {
return [self.delegate columnMarginInWaterflowLayout:self];
} else {
return DefaultColumnMargin;
}
}
- (NSInteger)columnCount
{
if ([self.delegate respondsToSelector:@selector(columnCountInWaterflowLayout:)]) {
return [self.delegate columnCountInWaterflowLayout:self];
} else {
return DefaultColumnCount;
}
}
- (UIEdgeInsets)edgeInsets
{
if ([self.delegate respondsToSelector:@selector(edgeInsetsInWaterflowLayout:)]) {
return [self.delegate edgeInsetsInWaterflowLayout:self];
} else {
return DefaultEdgeInsets;
}
}
#pragma mark 懒加载
- (NSMutableArray *)columnHeights
{
if (!_columnHeights) {
_columnHeights = [NSMutableArray array];
}
return _columnHeights;
}
- (NSMutableArray *)attrsArray
{
if (!_attrsArray) {
_attrsArray = [NSMutableArray array];
}
return _attrsArray;
}
/**
* 初始化
*/
- (void)prepareLayout{
[super prepareLayout];
// 清除以前计算的所有高度
[self.columnHeights removeAllObjects];
//获取顶部Edge
for (NSInteger i = 0; i < DefaultColumnCount; i++) {
[self.columnHeights addObject:@(self.edgeInsets.top)];
}
// 清除之前所有的布局属性
[self.attrsArray removeAllObjects];
// 开始创建每一个cell对应的布局属性
NSInteger count = [self.collectionView numberOfItemsInSection:0];
for (NSInteger i = 0; i < count; i++){
// 创建位置
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
// 获取indexPath位置cell对应的布局属性
UICollectionViewLayoutAttributes *attrs = [self layoutAttributesForItemAtIndexPath:indexPath];
[self.attrsArray addObject:attrs];
}
}
/**
* 决定cell的排布
*/
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
return self.attrsArray;
}
/**
* 返回indexPath位置cell对应的布局属性
*/
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath{
// 创建布局属性
UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
// collectionView的宽度
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 = 50 + arc4random_uniform(100);
CGFloat h = [self.delegate waterflowLayout:self heightForItemAtIndex:indexPath.item itemWidth:w];
// 找出高度最短的那一列
NSInteger destColumn = 0;
//假如最小一行为第一行
CGFloat minColumnHeight = [self.columnHeights[0] doubleValue];
//比较出最短的一行
for (NSInteger i = 1; i < self.columnCount; i++){
// 取得第i列的高度
CGFloat columnHeight = [self.columnHeights[i] doubleValue];
//假如最短的一列高度 大于第i个的高度
if (minColumnHeight > columnHeight) {
//最短的更新
minColumnHeight = columnHeight;
//第i即为最短
destColumn = i;
}
}
//找到最短的一行之后,取出下个cell要放的位置
CGFloat x = self.edgeInsets.left + destColumn * (w + self.columnMargin);
CGFloat y = minColumnHeight;
if (y != self.edgeInsets.top)y += self.rowMargin;
attrs.frame = CGRectMake(x, y, w, h);
// 更新最短那列的高度
self.columnHeights[destColumn] = @(CGRectGetMaxY(attrs.frame));
return attrs;
}
- (CGSize)collectionViewContentSize{
CGFloat maxColumnHeight = [self.columnHeights[0] doubleValue];
for (NSInteger i = 1; i < self.columnCount; i++) {
// 取得第i列的高度
CGFloat columnHeight = [self.columnHeights[i] doubleValue];
if (maxColumnHeight < columnHeight) {
maxColumnHeight = columnHeight;
}
}
return CGSizeMake(0, maxColumnHeight +self.edgeInsets.bottom);
}