forked from chiahsien/CHTCollectionViewWaterfallLayout
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUICollectionViewWaterfallLayout.m
More file actions
192 lines (163 loc) · 6.05 KB
/
UICollectionViewWaterfallLayout.m
File metadata and controls
192 lines (163 loc) · 6.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//
// UICollectionViewWaterfallLayout.m
//
// Created by Nelson on 12/11/19.
// Copyright (c) 2012 Nelson Tai. All rights reserved.
//
#import "UICollectionViewWaterfallLayout.h"
@interface UICollectionViewWaterfallLayout()
@property (nonatomic, assign) NSInteger itemCount;
@property (nonatomic, assign) CGFloat interitemSpacing;
@property (nonatomic, strong) NSMutableArray *columnHeights; // height for each column
@property (nonatomic, strong) NSMutableArray *itemAttributes; // attributes for each item
@property (nonatomic, assign) CGRect headerRect;
@end
@implementation UICollectionViewWaterfallLayout
#pragma mark - Accessors
- (void)setColumnCount:(NSUInteger)columnCount
{
if (_columnCount != columnCount) {
_columnCount = columnCount;
[self invalidateLayout];
}
}
- (void)setItemWidth:(CGFloat)itemWidth
{
if (_itemWidth != itemWidth) {
_itemWidth = itemWidth;
[self invalidateLayout];
}
}
- (void)setSectionInset:(UIEdgeInsets)sectionInset
{
if (!UIEdgeInsetsEqualToEdgeInsets(_sectionInset, sectionInset)) {
_sectionInset = sectionInset;
[self invalidateLayout];
}
}
#pragma mark - Init
- (void)commonInit
{
_columnCount = 2;
_itemWidth = 140.0f;
_sectionInset = UIEdgeInsetsZero;
}
- (id)init
{
self = [super init];
if (self) {
[self commonInit];
}
return self;
}
#pragma mark - Life cycle
- (void)dealloc
{
[_columnHeights removeAllObjects];
_columnHeights = nil;
[_itemAttributes removeAllObjects];
_itemAttributes = nil;
}
#pragma mark - Methods to Override
- (void)prepareLayout
{
[super prepareLayout];
_itemCount = [[self collectionView] numberOfItemsInSection:0];
NSAssert(_columnCount > 1, @"columnCount for UICollectionViewWaterfallLayout should be greater than 1.");
CGFloat width = self.collectionView.frame.size.width - _sectionInset.left - _sectionInset.right;
_interitemSpacing = floorf((width - _columnCount * _itemWidth) / (_columnCount - 1));
{
CGFloat headerHeight = [self.delegate heightOfHeaderCollectionView:self.collectionView layout:self];
self.headerRect = CGRectMake(0, 0, self.collectionView.frame.size.width, headerHeight);
}
_itemAttributes = [NSMutableArray arrayWithCapacity:_itemCount];
_columnHeights = [NSMutableArray arrayWithCapacity:_columnCount];
for (NSInteger idx = 0; idx < _columnCount; idx++) {
[_columnHeights addObject:@(_sectionInset.top + self.headerRect.size.height)];
}
// Item will be put into shortest column.
for (NSInteger idx = 0; idx < _itemCount; idx++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:idx inSection:0];
CGFloat itemHeight = [self.delegate collectionView:self.collectionView
layout:self
heightForItemAtIndexPath:indexPath];
NSUInteger columnIndex = [self shortestColumnIndex];
CGFloat xOffset = _sectionInset.left + (_itemWidth + _interitemSpacing) * columnIndex;
CGFloat yOffset = [(_columnHeights[columnIndex]) floatValue];
PSTCollectionViewLayoutAttributes *attributes =
[PSTCollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
attributes.frame = CGRectMake(xOffset, yOffset, self.itemWidth, itemHeight);
[_itemAttributes addObject:attributes];
_columnHeights[columnIndex] = @(yOffset + itemHeight + _interitemSpacing);
}
}
- (CGSize)collectionViewContentSize
{
if (self.itemCount == 0) {
return CGSizeZero;
}
CGSize contentSize = self.collectionView.frame.size;
NSUInteger columnIndex = [self longestColumnIndex];
CGFloat height = [self.columnHeights[columnIndex] floatValue];
contentSize.height = height - self.interitemSpacing + self.sectionInset.bottom;
return contentSize;
}
- (PSTCollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)path
{
return (self.itemAttributes)[path.item];
}
- (PSTCollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
PSTCollectionViewLayoutAttributes *headerAttributes = [PSTCollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:kind withIndexPath:indexPath];
headerAttributes.frame = self.headerRect;
return headerAttributes;
}
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
NSArray *elementArray;
NSArray *itemArray = [self.itemAttributes filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(PSTCollectionViewLayoutAttributes *evaluatedObject, NSDictionary *bindings) {
return CGRectIntersectsRect(rect, [evaluatedObject frame]);
}]];
if (YES==CGRectIntersectsRect(rect, self.headerRect)) {
NSMutableArray *tempArray = [NSMutableArray arrayWithObject:[self layoutAttributesForSupplementaryViewOfKind:nil atIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]]];
[tempArray addObjectsFromArray:itemArray];
elementArray = [NSArray arrayWithArray:tempArray];
} else {
elementArray = itemArray;
}
return elementArray;
}
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
return NO;
}
#pragma mark - Private Methods
// Find out shortest column.
- (NSUInteger)shortestColumnIndex
{
__block NSUInteger index = 0;
__block CGFloat shortestHeight = MAXFLOAT;
[self.columnHeights enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
CGFloat height = [obj floatValue];
if (height < shortestHeight) {
shortestHeight = height;
index = idx;
}
}];
return index;
}
// Find out longest column.
- (NSUInteger)longestColumnIndex
{
__block NSUInteger index = 0;
__block CGFloat longestHeight = 0;
[self.columnHeights enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
CGFloat height = [obj floatValue];
if (height > longestHeight) {
longestHeight = height;
index = idx;
}
}];
return index;
}
@end