forked from TextureGroup/Texture
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMosaicCollectionViewLayout.m
More file actions
231 lines (196 loc) · 8.05 KB
/
Copy pathMosaicCollectionViewLayout.m
File metadata and controls
231 lines (196 loc) · 8.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
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
//
// MosaicCollectionViewLayout.m
// Texture
//
// Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
// Changes after 4/13/2017 are: Copyright (c) Pinterest, Inc. All rights reserved.
// Licensed under Apache 2.0: http://www.apache.org/licenses/LICENSE-2.0
//
#import "MosaicCollectionViewLayout.h"
@implementation MosaicCollectionViewLayout {
NSMutableArray *_columnHeights;
NSMutableArray *_itemAttributes;
NSMutableDictionary *_headerAttributes;
NSMutableArray *_allAttributes;
}
- (instancetype)init
{
self = [super init];
if (self != nil) {
self.numberOfColumns = 3;
self.columnSpacing = 10.0;
self.sectionInset = UIEdgeInsetsMake(10.0, 10.0, 10.0, 10.0);
self.interItemSpacing = UIEdgeInsetsMake(10.0, 0, 10.0, 0);
}
return self;
}
- (void)prepareLayout
{
_itemAttributes = [NSMutableArray array];
_columnHeights = [NSMutableArray array];
_allAttributes = [NSMutableArray array];
_headerAttributes = [NSMutableDictionary dictionary];
CGFloat top = 0;
NSInteger numberOfSections = [self.collectionView numberOfSections];
for (NSUInteger section = 0; section < numberOfSections; section++) {
NSInteger numberOfItems = [self.collectionView numberOfItemsInSection:section];
top += _sectionInset.top;
if (_headerHeight > 0) {
CGSize headerSize = [self _headerSizeForSection:section];
UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes
layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader
withIndexPath:[NSIndexPath indexPathForItem:0 inSection:section]];
attributes.frame = CGRectMake(_sectionInset.left, top, headerSize.width, headerSize.height);
_headerAttributes[@(section)] = attributes;
[_allAttributes addObject:attributes];
top = CGRectGetMaxY(attributes.frame);
}
[_columnHeights addObject:[NSMutableArray array]];
for (NSUInteger idx = 0; idx < self.numberOfColumns; idx++) {
[_columnHeights[section] addObject:@(top)];
}
CGFloat columnWidth = [self _columnWidthForSection:section];
[_itemAttributes addObject:[NSMutableArray array]];
for (NSUInteger idx = 0; idx < numberOfItems; idx++) {
NSUInteger columnIndex = [self _shortestColumnIndexInSection:section];
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:idx inSection:section];
CGSize itemSize = [self _itemSizeAtIndexPath:indexPath];
CGFloat xOffset = _sectionInset.left + (columnWidth + _columnSpacing) * columnIndex;
CGFloat yOffset = [_columnHeights[section][columnIndex] floatValue];
UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes
layoutAttributesForCellWithIndexPath:indexPath];
attributes.frame = CGRectMake(xOffset, yOffset, itemSize.width, itemSize.height);
_columnHeights[section][columnIndex] = @(CGRectGetMaxY(attributes.frame) + _interItemSpacing.bottom);
[_itemAttributes[section] addObject:attributes];
[_allAttributes addObject:attributes];
}
NSUInteger columnIndex = [self _tallestColumnIndexInSection:section];
top = [_columnHeights[section][columnIndex] floatValue] - _interItemSpacing.bottom + _sectionInset.bottom;
for (NSUInteger idx = 0; idx < [_columnHeights[section] count]; idx++) {
_columnHeights[section][idx] = @(top);
}
}
}
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
NSMutableArray *includedAttributes = [NSMutableArray array];
// Slow search for small batches
for (UICollectionViewLayoutAttributes *attributes in _allAttributes) {
if (CGRectIntersectsRect(attributes.frame, rect)) {
[includedAttributes addObject:attributes];
}
}
return includedAttributes;
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section >= _itemAttributes.count) {
return nil;
} else if (indexPath.item >= [_itemAttributes[indexPath.section] count]) {
return nil;
}
return _itemAttributes[indexPath.section][indexPath.item];
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath
{
if ([elementKind isEqualToString:UICollectionElementKindSectionHeader]) {
return _headerAttributes[@(indexPath.section)];
}
return nil;
}
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
if (!CGRectEqualToRect(self.collectionView.bounds, newBounds)) {
return YES;
}
return NO;
}
- (CGFloat)_widthForSection:(NSUInteger)section
{
return self.collectionView.bounds.size.width - _sectionInset.left - _sectionInset.right;
}
- (CGFloat)_columnWidthForSection:(NSUInteger)section
{
return ([self _widthForSection:section] - ((_numberOfColumns - 1) * _columnSpacing)) / _numberOfColumns;
}
- (CGSize)_itemSizeAtIndexPath:(NSIndexPath *)indexPath
{
CGSize size = CGSizeMake([self _columnWidthForSection:indexPath.section], 0);
CGSize originalSize = [[self _delegate] collectionView:self.collectionView layout:self originalItemSizeAtIndexPath:indexPath];
if (originalSize.height > 0 && originalSize.width > 0) {
size.height = originalSize.height / originalSize.width * size.width;
}
return size;
}
- (CGSize)_headerSizeForSection:(NSUInteger)section
{
return CGSizeMake([self _widthForSection:section], _headerHeight);
}
- (CGSize)collectionViewContentSize
{
CGFloat height = [[[_columnHeights lastObject] firstObject] floatValue];
return CGSizeMake(self.collectionView.bounds.size.width, height);
}
- (NSUInteger)_tallestColumnIndexInSection:(NSUInteger)section
{
__block NSUInteger index = 0;
__block CGFloat tallestHeight = 0;
[_columnHeights[section] enumerateObjectsUsingBlock:^(NSNumber *height, NSUInteger idx, BOOL *stop) {
if (height.floatValue > tallestHeight) {
index = idx;
tallestHeight = height.floatValue;
}
}];
return index;
}
- (NSUInteger)_shortestColumnIndexInSection:(NSUInteger)section
{
__block NSUInteger index = 0;
__block CGFloat shortestHeight = CGFLOAT_MAX;
[_columnHeights[section] enumerateObjectsUsingBlock:^(NSNumber *height, NSUInteger idx, BOOL *stop) {
if (height.floatValue < shortestHeight) {
index = idx;
shortestHeight = height.floatValue;
}
}];
return index;
}
- (id<MosaicCollectionViewLayoutDelegate>)_delegate
{
return (id<MosaicCollectionViewLayoutDelegate>)self.collectionView.delegate;
}
@end
@implementation MosaicCollectionViewLayoutInspector
- (ASSizeRange)collectionView:(ASCollectionView *)collectionView constrainedSizeForNodeAtIndexPath:(NSIndexPath *)indexPath
{
MosaicCollectionViewLayout *layout = (MosaicCollectionViewLayout *)[collectionView collectionViewLayout];
return ASSizeRangeMake(CGSizeZero, [layout _itemSizeAtIndexPath:indexPath]);
}
- (ASSizeRange)collectionView:(ASCollectionView *)collectionView constrainedSizeForSupplementaryNodeOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
MosaicCollectionViewLayout *layout = (MosaicCollectionViewLayout *)[collectionView collectionViewLayout];
return ASSizeRangeMake(CGSizeZero, [layout _headerSizeForSection:indexPath.section]);
}
/**
* Asks the inspector for the number of supplementary sections in the collection view for the given kind.
*/
- (NSUInteger)collectionView:(ASCollectionView *)collectionView numberOfSectionsForSupplementaryNodeOfKind:(NSString *)kind
{
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
return [[collectionView asyncDataSource] numberOfSectionsInCollectionView:collectionView];
} else {
return 0;
}
}
/**
* Asks the inspector for the number of supplementary views for the given kind in the specified section.
*/
- (NSUInteger)collectionView:(ASCollectionView *)collectionView supplementaryNodesOfKind:(NSString *)kind inSection:(NSUInteger)section
{
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
return 1;
} else {
return 0;
}
}
@end