自定義佈局,實現瀑布流效果
自定義流水佈局,繼承UICollectionViewLayout
實現一下方法
// 每次佈局以前的準備
- (void)prepareLayout;
// 返回全部的尺寸
- (CGSize)collectionViewContentSize;
// 返回indexPath這個位置Item的佈局屬性
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
// 返回rect範圍內的佈局屬性
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect;
思路:默認有三列,添加圖片時,往三列中最大長度最小的那一列添加,
- 主要工做就在計算最大Y值,而後佈局圖片
- 用一個字典用來存儲每一列最大的Y值(每一列的高度)
遍歷字典找出最短的那一列
// 找出最短的那一列
[self.maxYDict enumerateKeysAndObjectsUsingBlock:^(NSString *column, NSNumber *maxY, BOOL *stop) {
if ([maxY floatValue] < [self.maxYDict[minColumn] floatValue]) {
minColumn = column;
}
}];
服務端返回的數據,必須包含圖片的高度和寬度,以此能夠根據寬高比佈局,根據寬度能夠經過代理計算高度。
示例代碼
YLCollectionLayout.h
//
// YLCollectionLayout.h
// Created by 邵銀嶺.
//
#import <UIKit/UIKit.h>
@class YLCollectionLayout;
@protocol YLCollectionLayoutDelegate <NSObject>
- (CGFloat)flowLayout:(YLCollectionLayout *)flowLayout heightForWidth:(CGFloat)width atIndexPath:(NSIndexPath *)indexPath;
@end
@interface YLCollectionLayout : UICollectionViewLayout
/** 列間距 */
@property(nonatomic,assign)CGFloat columnMargin;
/** 行間距 */
@property(nonatomic,assign)CGFloat rowMargin;
/** 列數 */
@property(nonatomic,assign)int columnsCount;
/** 外邊距 */
@property (nonatomic, assign) UIEdgeInsets sectionInset;
@property (nonatomic, weak) id<YLCollectionLayoutDelegate> delegate;
@end
YLCollectionLayout.m
//
// YLCollectionLayout.m
// Created by 邵銀嶺
//
#import "YLCollectionLayout.h"
@interface YLCollectionLayout()
/** 這個字典用來存儲每一列最大的Y值(每一列的高度) */
@property (nonatomic, strong) NSMutableDictionary *maxYDict;
/** 存放全部的佈局屬性 */
@property(nonatomic,strong)NSMutableArray *attributeArray;
@end
@implementation YLCollectionLayout
- (NSMutableDictionary *)maxYDict
{
if (!_maxYDict) {
self.maxYDict = [[NSMutableDictionary alloc] init];
}
return _maxYDict;
}
- (NSMutableArray *)attributeArray
{
if (!_attributeArray) {
self.attributeArray = [[NSMutableArray alloc] init];
}
return _attributeArray;
}
#pragma mark -初始化默認值
- (instancetype)init
{
if (self = [super init]) {
self.columnMargin = 15;
self.rowMargin = 10;
self.columnsCount = 3;
self.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
}
return self;
}
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
return YES;
}
// 佈局每個indexPath的位置
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
// 1.計算尺寸
CGFloat width = (self.collectionView.frame.size.width - self.sectionInset.left - self.sectionInset.right - (self.columnsCount - 1) * self.columnMargin) / self.columnsCount;
// 代理計算傳入高的值
CGFloat height = [self.delegate flowLayout:self heightForWidth:width atIndexPath:indexPath];
// 2.0假設最短的那一列的第0列
__block NSString *minColumn = @"0";
// 遍歷字典找出最短的那一列
[self.maxYDict enumerateKeysAndObjectsUsingBlock:^(NSString *column, NSNumber *maxY, BOOL *stop) {
if ([maxY floatValue] < [self.maxYDict[minColumn] floatValue]) {
minColumn = column;
}
}];
// 2.1計算位置
CGFloat x = self.sectionInset.left + (self.columnMargin + width) * [minColumn intValue];
CGFloat y = [self.maxYDict[minColumn] floatValue]+ _rowMargin;
self.maxYDict[minColumn] = @(y + height);
// 3.建立屬性
UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
attrs.frame = CGRectMake(x, y, width, height);
return attrs;
}
- (void)prepareLayout
{
[super prepareLayout];
// 1.清空最大的Y值
for (int i = 0; i<self.columnsCount; i++) {
NSString *column = [NSString stringWithFormat:@"%d", i];
self.maxYDict[column] = @(self.sectionInset.top);
}
[self.attributeArray removeAllObjects];
// 總 item 數
NSInteger count = [self.collectionView numberOfItemsInSection:0];
for (int i = 0; i <count; i++) {
UICollectionViewLayoutAttributes *attris = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
[self.attributeArray addObject:attris];
}
}
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
return self.attributeArray;
}
// 計算ContentSize
- (CGSize)collectionViewContentSize
{
// 默認最大Y值在第0列
__block NSString *maxColumn = @"0";
[self.maxYDict enumerateKeysAndObjectsUsingBlock:^(NSString *column, NSNumber *maxY, BOOL *stop) {
if ([maxY floatValue] > [self.maxYDict[maxColumn] floatValue]) {
maxColumn = column;
}
}];
return CGSizeMake(0, [self.maxYDict[maxColumn] floatValue] + self.sectionInset.bottom);
}
@end
效果
![](http://static.javashuo.com/static/loading.gif)
另外一個案例--圖片查看器---連接git