forked from chiahsien/CHTCollectionViewWaterfallLayout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CHTCollectionViewWaterfallLayout.m
528 lines (444 loc) · 18 KB
/
CHTCollectionViewWaterfallLayout.m
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
//
// UICollectionViewWaterfallLayout.m
//
// Created by Nelson on 12/11/19.
// Copyright (c) 2012 Nelson Tai. All rights reserved.
//
#import "CHTCollectionViewWaterfallLayout.h"
#import "tgmath.h"
NSString *const CHTCollectionElementKindSectionHeader = @"CHTCollectionElementKindSectionHeader";
NSString *const CHTCollectionElementKindSectionFooter = @"CHTCollectionElementKindSectionFooter";
@interface CHTCollectionViewWaterfallLayout ()
/// The delegate will point to collection view's delegate automatically.
@property (nonatomic, weak) id <CHTCollectionViewDelegateWaterfallLayout> delegate;
/// Array to store height for each column
@property (nonatomic, strong) NSMutableArray *columnHeights;
/// Array of arrays. Each array stores item attributes for each section
@property (nonatomic, strong) NSMutableArray *sectionItemAttributes;
/// Array to store attributes for all items includes headers, cells, and footers
@property (nonatomic, strong) NSMutableArray *allItemAttributes;
/// Dictionary to store section headers' attribute
@property (nonatomic, strong) NSMutableDictionary *headersAttribute;
/// Dictionary to store section footers' attribute
@property (nonatomic, strong) NSMutableDictionary *footersAttribute;
/// Array to store union rectangles
@property (nonatomic, strong) NSMutableArray *unionRects;
@end
@implementation CHTCollectionViewWaterfallLayout
/// How many items to be union into a single rectangle
static const NSInteger unionSize = 20;
static CGFloat CHTFloorCGFloat(CGFloat value) {
CGFloat scale = [UIScreen mainScreen].scale;
return floor(value * scale) / scale;
}
#pragma mark - Public Accessors
- (void)setColumnCount:(NSInteger)columnCount {
if (_columnCount != columnCount) {
_columnCount = columnCount;
[self invalidateLayout];
}
}
- (void)setMinimumColumnSpacing:(CGFloat)minimumColumnSpacing {
if (_minimumColumnSpacing != minimumColumnSpacing) {
_minimumColumnSpacing = minimumColumnSpacing;
[self invalidateLayout];
}
}
- (void)setMinimumInteritemSpacing:(CGFloat)minimumInteritemSpacing {
if (_minimumInteritemSpacing != minimumInteritemSpacing) {
_minimumInteritemSpacing = minimumInteritemSpacing;
[self invalidateLayout];
}
}
- (void)setHeaderHeight:(CGFloat)headerHeight {
if (_headerHeight != headerHeight) {
_headerHeight = headerHeight;
[self invalidateLayout];
}
}
- (void)setFooterHeight:(CGFloat)footerHeight {
if (_footerHeight != footerHeight) {
_footerHeight = footerHeight;
[self invalidateLayout];
}
}
- (void)setHeaderInset:(UIEdgeInsets)headerInset {
if (!UIEdgeInsetsEqualToEdgeInsets(_headerInset, headerInset)) {
_headerInset = headerInset;
[self invalidateLayout];
}
}
- (void)setFooterInset:(UIEdgeInsets)footerInset {
if (!UIEdgeInsetsEqualToEdgeInsets(_footerInset, footerInset)) {
_footerInset = footerInset;
[self invalidateLayout];
}
}
- (void)setSectionInset:(UIEdgeInsets)sectionInset {
if (!UIEdgeInsetsEqualToEdgeInsets(_sectionInset, sectionInset)) {
_sectionInset = sectionInset;
[self invalidateLayout];
}
}
- (void)setItemRenderDirection:(CHTCollectionViewWaterfallLayoutItemRenderDirection)itemRenderDirection {
if (_itemRenderDirection != itemRenderDirection) {
_itemRenderDirection = itemRenderDirection;
[self invalidateLayout];
}
}
- (NSInteger)columnCountForSection:(NSInteger)section {
if ([self.delegate respondsToSelector:@selector(collectionView:layout:columnCountForSection:)]) {
return [self.delegate collectionView:self.collectionView layout:self columnCountForSection:section];
} else {
return self.columnCount;
}
}
- (CGFloat)itemWidthInSectionAtIndex:(NSInteger)section {
UIEdgeInsets sectionInset;
if ([self.delegate respondsToSelector:@selector(collectionView:layout:insetForSectionAtIndex:)]) {
sectionInset = [self.delegate collectionView:self.collectionView layout:self insetForSectionAtIndex:section];
} else {
sectionInset = self.sectionInset;
}
CGFloat width = self.collectionView.bounds.size.width - sectionInset.left - sectionInset.right;
NSInteger columnCount = [self columnCountForSection:section];
CGFloat columnSpacing = self.minimumColumnSpacing;
if ([self.delegate respondsToSelector:@selector(collectionView:layout:minimumColumnSpacingForSectionAtIndex:)]) {
columnSpacing = [self.delegate collectionView:self.collectionView layout:self minimumColumnSpacingForSectionAtIndex:section];
}
return CHTFloorCGFloat((width - (columnCount - 1) * columnSpacing) / columnCount);
}
#pragma mark - Private Accessors
- (NSMutableDictionary *)headersAttribute {
if (!_headersAttribute) {
_headersAttribute = [NSMutableDictionary dictionary];
}
return _headersAttribute;
}
- (NSMutableDictionary *)footersAttribute {
if (!_footersAttribute) {
_footersAttribute = [NSMutableDictionary dictionary];
}
return _footersAttribute;
}
- (NSMutableArray *)unionRects {
if (!_unionRects) {
_unionRects = [NSMutableArray array];
}
return _unionRects;
}
- (NSMutableArray *)columnHeights {
if (!_columnHeights) {
_columnHeights = [NSMutableArray array];
}
return _columnHeights;
}
- (NSMutableArray *)allItemAttributes {
if (!_allItemAttributes) {
_allItemAttributes = [NSMutableArray array];
}
return _allItemAttributes;
}
- (NSMutableArray *)sectionItemAttributes {
if (!_sectionItemAttributes) {
_sectionItemAttributes = [NSMutableArray array];
}
return _sectionItemAttributes;
}
- (id <CHTCollectionViewDelegateWaterfallLayout> )delegate {
return (id <CHTCollectionViewDelegateWaterfallLayout> )self.collectionView.delegate;
}
#pragma mark - Init
- (void)commonInit {
_columnCount = 2;
_minimumColumnSpacing = 10;
_minimumInteritemSpacing = 10;
_headerHeight = 0;
_footerHeight = 0;
_sectionInset = UIEdgeInsetsZero;
_headerInset = UIEdgeInsetsZero;
_footerInset = UIEdgeInsetsZero;
_itemRenderDirection = CHTCollectionViewWaterfallLayoutItemRenderDirectionShortestFirst;
}
- (id)init {
if (self = [super init]) {
[self commonInit];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
[self commonInit];
}
return self;
}
#pragma mark - Methods to Override
- (void)prepareLayout {
[super prepareLayout];
[self.headersAttribute removeAllObjects];
[self.footersAttribute removeAllObjects];
[self.unionRects removeAllObjects];
[self.columnHeights removeAllObjects];
[self.allItemAttributes removeAllObjects];
[self.sectionItemAttributes removeAllObjects];
NSInteger numberOfSections = [self.collectionView numberOfSections];
if (numberOfSections == 0) {
return;
}
NSAssert([self.delegate conformsToProtocol:@protocol(CHTCollectionViewDelegateWaterfallLayout)], @"UICollectionView's delegate should conform to CHTCollectionViewDelegateWaterfallLayout protocol");
NSAssert(self.columnCount > 0 || [self.delegate respondsToSelector:@selector(collectionView:layout:columnCountForSection:)], @"UICollectionViewWaterfallLayout's columnCount should be greater than 0, or delegate must implement columnCountForSection:");
// Initialize variables
NSInteger idx = 0;
for (NSInteger section = 0; section < numberOfSections; section++) {
NSInteger columnCount = [self columnCountForSection:section];
NSMutableArray *sectionColumnHeights = [NSMutableArray arrayWithCapacity:columnCount];
for (idx = 0; idx < columnCount; idx++) {
[sectionColumnHeights addObject:@(0)];
}
[self.columnHeights addObject:sectionColumnHeights];
}
// Create attributes
CGFloat top = 0;
UICollectionViewLayoutAttributes *attributes;
for (NSInteger section = 0; section < numberOfSections; ++section) {
/*
* 1. Get section-specific metrics (minimumInteritemSpacing, sectionInset)
*/
CGFloat minimumInteritemSpacing;
if ([self.delegate respondsToSelector:@selector(collectionView:layout:minimumInteritemSpacingForSectionAtIndex:)]) {
minimumInteritemSpacing = [self.delegate collectionView:self.collectionView layout:self minimumInteritemSpacingForSectionAtIndex:section];
} else {
minimumInteritemSpacing = self.minimumInteritemSpacing;
}
CGFloat columnSpacing = self.minimumColumnSpacing;
if ([self.delegate respondsToSelector:@selector(collectionView:layout:minimumColumnSpacingForSectionAtIndex:)]) {
columnSpacing = [self.delegate collectionView:self.collectionView layout:self minimumColumnSpacingForSectionAtIndex:section];
}
UIEdgeInsets sectionInset;
if ([self.delegate respondsToSelector:@selector(collectionView:layout:insetForSectionAtIndex:)]) {
sectionInset = [self.delegate collectionView:self.collectionView layout:self insetForSectionAtIndex:section];
} else {
sectionInset = self.sectionInset;
}
CGFloat width = self.collectionView.bounds.size.width - sectionInset.left - sectionInset.right;
NSInteger columnCount = [self columnCountForSection:section];
CGFloat itemWidth = CHTFloorCGFloat((width - (columnCount - 1) * columnSpacing) / columnCount);
/*
* 2. Section header
*/
CGFloat headerHeight;
if ([self.delegate respondsToSelector:@selector(collectionView:layout:heightForHeaderInSection:)]) {
headerHeight = [self.delegate collectionView:self.collectionView layout:self heightForHeaderInSection:section];
} else {
headerHeight = self.headerHeight;
}
UIEdgeInsets headerInset;
if ([self.delegate respondsToSelector:@selector(collectionView:layout:insetForHeaderInSection:)]) {
headerInset = [self.delegate collectionView:self.collectionView layout:self insetForHeaderInSection:section];
} else {
headerInset = self.headerInset;
}
top += headerInset.top;
if (headerHeight > 0) {
attributes = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:CHTCollectionElementKindSectionHeader withIndexPath:[NSIndexPath indexPathForItem:0 inSection:section]];
attributes.frame = CGRectMake(headerInset.left,
top,
self.collectionView.bounds.size.width - (headerInset.left + headerInset.right),
headerHeight);
self.headersAttribute[@(section)] = attributes;
[self.allItemAttributes addObject:attributes];
top = CGRectGetMaxY(attributes.frame) + headerInset.bottom;
}
top += sectionInset.top;
for (idx = 0; idx < columnCount; idx++) {
self.columnHeights[section][idx] = @(top);
}
/*
* 3. Section items
*/
NSInteger itemCount = [self.collectionView numberOfItemsInSection:section];
NSMutableArray *itemAttributes = [NSMutableArray arrayWithCapacity:itemCount];
// Item will be put into shortest column.
for (idx = 0; idx < itemCount; idx++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:idx inSection:section];
NSUInteger columnIndex = [self nextColumnIndexForItem:idx inSection:section];
CGFloat xOffset = sectionInset.left + (itemWidth + columnSpacing) * columnIndex;
CGFloat yOffset = [self.columnHeights[section][columnIndex] floatValue];
CGSize itemSize = [self.delegate collectionView:self.collectionView layout:self sizeForItemAtIndexPath:indexPath];
CGFloat itemHeight = 0;
if (itemSize.height > 0 && itemSize.width > 0) {
itemHeight = CHTFloorCGFloat(itemSize.height * itemWidth / itemSize.width);
}
attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
attributes.frame = CGRectMake(xOffset, yOffset, itemWidth, itemHeight);
[itemAttributes addObject:attributes];
[self.allItemAttributes addObject:attributes];
self.columnHeights[section][columnIndex] = @(CGRectGetMaxY(attributes.frame) + minimumInteritemSpacing);
}
[self.sectionItemAttributes addObject:itemAttributes];
/*
* 4. Section footer
*/
CGFloat footerHeight;
NSUInteger columnIndex = [self longestColumnIndexInSection:section];
top = [self.columnHeights[section][columnIndex] floatValue] - minimumInteritemSpacing + sectionInset.bottom;
if ([self.delegate respondsToSelector:@selector(collectionView:layout:heightForFooterInSection:)]) {
footerHeight = [self.delegate collectionView:self.collectionView layout:self heightForFooterInSection:section];
} else {
footerHeight = self.footerHeight;
}
UIEdgeInsets footerInset;
if ([self.delegate respondsToSelector:@selector(collectionView:layout:insetForFooterInSection:)]) {
footerInset = [self.delegate collectionView:self.collectionView layout:self insetForFooterInSection:section];
} else {
footerInset = self.footerInset;
}
top += footerInset.top;
if (footerHeight > 0) {
attributes = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:CHTCollectionElementKindSectionFooter withIndexPath:[NSIndexPath indexPathForItem:0 inSection:section]];
attributes.frame = CGRectMake(footerInset.left,
top,
self.collectionView.bounds.size.width - (footerInset.left + footerInset.right),
footerHeight);
self.footersAttribute[@(section)] = attributes;
[self.allItemAttributes addObject:attributes];
top = CGRectGetMaxY(attributes.frame) + footerInset.bottom;
}
for (idx = 0; idx < columnCount; idx++) {
self.columnHeights[section][idx] = @(top);
}
} // end of for (NSInteger section = 0; section < numberOfSections; ++section)
// Build union rects
idx = 0;
NSInteger itemCounts = [self.allItemAttributes count];
while (idx < itemCounts) {
CGRect unionRect = ((UICollectionViewLayoutAttributes *)self.allItemAttributes[idx]).frame;
NSInteger rectEndIndex = MIN(idx + unionSize, itemCounts);
for (NSInteger i = idx + 1; i < rectEndIndex; i++) {
unionRect = CGRectUnion(unionRect, ((UICollectionViewLayoutAttributes *)self.allItemAttributes[i]).frame);
}
idx = rectEndIndex;
[self.unionRects addObject:[NSValue valueWithCGRect:unionRect]];
}
}
- (CGSize)collectionViewContentSize {
NSInteger numberOfSections = [self.collectionView numberOfSections];
if (numberOfSections == 0) {
return CGSizeZero;
}
CGSize contentSize = self.collectionView.bounds.size;
contentSize.height = [[[self.columnHeights lastObject] firstObject] floatValue];
if (contentSize.height < self.minimumContentHeight) {
contentSize.height = self.minimumContentHeight;
}
return contentSize;
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)path {
if (path.section >= [self.sectionItemAttributes count]) {
return nil;
}
if (path.item >= [self.sectionItemAttributes[path.section] count]) {
return nil;
}
return (self.sectionItemAttributes[path.section])[path.item];
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
UICollectionViewLayoutAttributes *attribute = nil;
if ([kind isEqualToString:CHTCollectionElementKindSectionHeader]) {
attribute = self.headersAttribute[@(indexPath.section)];
} else if ([kind isEqualToString:CHTCollectionElementKindSectionFooter]) {
attribute = self.footersAttribute[@(indexPath.section)];
}
return attribute;
}
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
NSInteger i;
NSInteger begin = 0, end = self.unionRects.count;
NSMutableArray *attrs = [NSMutableArray array];
for (i = 0; i < self.unionRects.count; i++) {
if (CGRectIntersectsRect(rect, [self.unionRects[i] CGRectValue])) {
begin = i * unionSize;
break;
}
}
for (i = self.unionRects.count - 1; i >= 0; i--) {
if (CGRectIntersectsRect(rect, [self.unionRects[i] CGRectValue])) {
end = MIN((i + 1) * unionSize, self.allItemAttributes.count);
break;
}
}
for (i = begin; i < end; i++) {
UICollectionViewLayoutAttributes *attr = self.allItemAttributes[i];
if (CGRectIntersectsRect(rect, attr.frame)) {
[attrs addObject:attr];
}
}
return [NSArray arrayWithArray:attrs];
}
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
CGRect oldBounds = self.collectionView.bounds;
if (CGRectGetWidth(newBounds) != CGRectGetWidth(oldBounds)) {
return YES;
}
return NO;
}
#pragma mark - Private Methods
/**
* Find the shortest column.
*
* @return index for the shortest column
*/
- (NSUInteger)shortestColumnIndexInSection:(NSInteger)section {
__block NSUInteger index = 0;
__block CGFloat shortestHeight = MAXFLOAT;
[self.columnHeights[section] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
CGFloat height = [obj floatValue];
if (height < shortestHeight) {
shortestHeight = height;
index = idx;
}
}];
return index;
}
/**
* Find the longest column.
*
* @return index for the longest column
*/
- (NSUInteger)longestColumnIndexInSection:(NSInteger)section {
__block NSUInteger index = 0;
__block CGFloat longestHeight = 0;
[self.columnHeights[section] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
CGFloat height = [obj floatValue];
if (height > longestHeight) {
longestHeight = height;
index = idx;
}
}];
return index;
}
/**
* Find the index for the next column.
*
* @return index for the next column
*/
- (NSUInteger)nextColumnIndexForItem:(NSInteger)item inSection:(NSInteger)section {
NSUInteger index = 0;
NSInteger columnCount = [self columnCountForSection:section];
switch (self.itemRenderDirection) {
case CHTCollectionViewWaterfallLayoutItemRenderDirectionShortestFirst:
index = [self shortestColumnIndexInSection:section];
break;
case CHTCollectionViewWaterfallLayoutItemRenderDirectionLeftToRight:
index = (item % columnCount);
break;
case CHTCollectionViewWaterfallLayoutItemRenderDirectionRightToLeft:
index = (columnCount - 1) - (item % columnCount);
break;
default:
index = [self shortestColumnIndexInSection:section];
break;
}
return index;
}
@end