根据网上的一些资料,封装了一套完美的瀑布流分类,下面看源码吧
下面是JYWaterfallLayout.h头文件
//
// ViewController.m
// 瀑布流
//
// Created by JY on 2017/4/4.
// Copyright © 2017年 TZW. All rights reserved.
//
#import
@class JYWaterfallLayout;
@protocol JYWaterfallLayoutDelegate
@required
//计算item高度的代理方法,将item的高度与indexPath传递给外界
- (CGFloat)waterfallLayout:(JYWaterfallLayout *)waterfallLayout itemHeightForWidth:(CGFloat)itemWidth atIndexPath:(NSIndexPath *)indexPath;
@end
@interface JYWaterfallLayout : UICollectionViewFlowLayout
#pragma mark- 属性
//总共多少列,默认是2
@property (nonatomic, assign) NSInteger columnCount;
//列间距,默认是0
@property (nonatomic, assign) NSInteger columnSpacing;
//行间距,默认是0
@property (nonatomic, assign) NSInteger rowSpacing;
//头部视图frame
@property(assign,nonatomic) CGRect headFrame;
//section与collectionView的间距,默认是(0,0,0,0)
@property (nonatomic, assign) UIEdgeInsets sectionInset;
//同时设置列间距,行间距,sectionInset
- (void)setColumnSpacing:(NSInteger)columnSpacing rowSpacing:(NSInteger)rowSepacing sectionInset:(UIEdgeInsets)sectionInset;
/**
以下代理属性与block属性二选一,用来设置每一个item的高度
会将item的宽度与indexPath传递给外界
如果两个都设置,block的优先级高,即代理无效
*/
//代理,用来计算item的高度
@property (nonatomic, weak) id delegate;
//计算item高度的block,将item的高度与indexPath传递给外界
@property (nonatomic, strong) CGFloat(^itemHeightBlock)(CGFloat itemHeight,NSIndexPath *indexPath);
#pragma mark- 构造方法
+ (instancetype)waterFallLayoutWithColumnCount:(NSInteger)columnCount;
- (instancetype)initWithColumnCount:(NSInteger)columnCount;
@end
接下来是JYWaterfallLayout.m文件
//
// ViewController.m
// 瀑布流
//
// Created by JY on 2017/4/4.
// Copyright © 2017年 TZW. All rights reserved.
//
#import "JYWaterfallLayout.h"
@interface JYWaterfallLayout()
//用来记录每一列的最大y值
@property (nonatomic, strong) NSMutableDictionary *maxYDic;
//保存每一个item的attributes
@property (nonatomic, strong) NSMutableArray *attributesArray;
@end
@implementation JYWaterfallLayout
#pragma mark- 懒加载
- (NSMutableDictionary *)maxYDic {
if (!_maxYDic) {
_maxYDic = [[NSMutableDictionary alloc] init];
}
return _maxYDic;
}
- (NSMutableArray *)attributesArray {
if (!_attributesArray) {
_attributesArray = [NSMutableArray array];
}
return _attributesArray;
}
#pragma mark- 构造方法
- (instancetype)init {
if (self = [super init]) {
self.columnCount = 2;
}
return self;
}
- (instancetype)initWithColumnCount:(NSInteger)columnCount {
if (self = [super init]) {
self.columnCount = columnCount;
}
return self;
}
+ (instancetype)waterFallLayoutWithColumnCount:(NSInteger)columnCount {
return [[self alloc] initWithColumnCount:columnCount];
}
#pragma mark- 相关设置方法
- (void)setColumnSpacing:(NSInteger)columnSpacing rowSpacing:(NSInteger)rowSepacing sectionInset:(UIEdgeInsets)sectionInset {
self.columnSpacing = columnSpacing;
self.rowSpacing = rowSepacing;
self.sectionInset = sectionInset;
}
#pragma mark- 布局相关方法
//布局前的准备工作
- (void)prepareLayout {
[super prepareLayout];
UICollectionViewLayoutAttributes *attributeHeader = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader withIndexPath:[NSIndexPath indexPathWithIndex:0]];
attributeHeader.frame = self.headFrame;
//初始化字典,有几列就有几个键值对,key为列,value为列的最大y值,初始值为上内边距
for (int i = 0; i < self.columnCount; i++) {
self.maxYDic[@(i)] = @(self.sectionInset.top);
}
//根据collectionView获取总共有多少个item
NSInteger itemCount = [self.collectionView numberOfItemsInSection:0];
[self.attributesArray removeAllObjects];
//为每一个item创建一个attributes并存入数组
for (int i = 0; i < itemCount; i++) {
UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];
[self.attributesArray addObject:attributes];
}
[self.attributesArray addObject:attributeHeader];
}
//计算collectionView的contentSize
- (CGSize)collectionViewContentSize {
__block NSNumber *maxIndex = @0;
//遍历字典,找出最长的那一列
[self.maxYDic enumerateKeysAndObjectsUsingBlock:^(NSNumber *key, NSNumber *obj, BOOL *stop) {
if ([self.maxYDic[maxIndex] floatValue] < obj.floatValue) {
maxIndex = key;
}
}];
//collectionView的contentSize.height就等于最长列的最大y值+下内边距
return CGSizeMake(0, [self.maxYDic[maxIndex] floatValue] + self.sectionInset.bottom);
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
//根据indexPath获取item的attributes
UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
//获取collectionView的宽度
CGFloat collectionViewWidth = self.collectionView.frame.size.width;
//item的宽度 = (collectionView的宽度 - 内边距与列间距) / 列数
CGFloat itemWidth = (collectionViewWidth - self.sectionInset.left - self.sectionInset.right - (self.columnCount - 1) * self.columnSpacing) / self.columnCount;
CGFloat itemHeight = 0;
//获取item的高度,由外界计算得到
if (self.itemHeightBlock) itemHeight = self.itemHeightBlock(itemWidth, indexPath);
else {
if ([self.delegate respondsToSelector:@selector(waterfallLayout:itemHeightForWidth:atIndexPath:)])
itemHeight = [self.delegate waterfallLayout:self itemHeightForWidth:itemWidth atIndexPath:indexPath];
}
//找出最短的那一列
__block NSNumber *minIndex = @0;
[self.maxYDic enumerateKeysAndObjectsUsingBlock:^(NSNumber *key, NSNumber *obj, BOOL *stop) {
if ([self.maxYDic[minIndex] floatValue] > obj.floatValue) {
minIndex = key;
}
}];
//根据最短列的列数计算item的x值
CGFloat itemX = self.sectionInset.left + (self.columnSpacing + itemWidth) * minIndex.integerValue;
//item的y值 = 最短列的最大y值 + 行间距
CGFloat itemY = [self.maxYDic[minIndex] floatValue] + self.rowSpacing;
//设置attributes的frame(搜索的改成这样的)
/*
if (indexPath.row < self.columnCount) {
attributes.frame = CGRectMake(itemX, itemY + self.headFrame.size.height - self.rowSpacing,itemWidth, itemHeight);
} else {
attributes.frame = CGRectMake(itemX, itemY, itemWidth, itemHeight);
}
*/
//先是这样的
if (indexPath.row == 0) {
attributes.frame = CGRectMake(itemX, itemY + self.headFrame.size.height - self.rowSpacing - 10,itemWidth, itemHeight);
}else if(indexPath.row == 1){
attributes.frame = CGRectMake(itemX, itemY - self.rowSpacing,itemWidth, itemHeight);
}else {
attributes.frame = CGRectMake(itemX, itemY, itemWidth, itemHeight);
}
//更新字典中的最大y值
self.maxYDic[minIndex] = @(CGRectGetMaxY(attributes.frame));
return attributes;
}
//返回rect范围内item的attributes
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
return self.attributesArray;
// return [self.attributesArray filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id _Nullable evaluatedObject, NSDictionary * _Nullable bindings) {
// return CGRectIntersectsRect(rect, [evaluatedObject frame]);
// }]];
}
@end
相信这个可以解决一般的瀑布流使用问题.也欢迎大牛指教