/
NVBnbCollectionViewLayout.m
410 lines (333 loc) · 16.4 KB
/
NVBnbCollectionViewLayout.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
//
// UICollectionViewBnbLayout.m
// NVBnbCollectionView
//
// Created by Nguyen Vinh on 8/7/15.
//
//
#import "NVBnbCollectionViewLayout.h"
#import "NVBnbCollectionView.h"
#define GRID_CELL_SIZE CGSizeMake(200, 100)
#define GRID_CELL_SPACING CGSizeMake(10, 10)
#define GRID_PADDING 20
#define PARALLAX_CELL_SIZE CGSizeMake(400, 200)
#define HEADER_SIZE CGSizeMake(200, 200)
#define MORE_LOADER_SIZE CGSizeMake(50, 50)
#define NUMBER_OF_ITEMS_IN_GROUP 10
#define SECTION 0
#define MAX_PARALLAX_OFFSET 50
// For fixed layout, group 10 items to one group
// Each group has the following layout:
// x x
// x x
// ---
// x x
// x x
// x x
// ---
//
// Annotation:
// x: one cell
// ---: parallax effect
//
// Item at index 3 and 9 have parallax effect
// The rest separates into 2 grids:
// 1. 1 2
// 3 2
// 2. 1 2
// 3 3
// 4 5
//
// Notes:
// - Cell having the same number that is represent for item occuping multiple cells
NSString *NVBnbCollectionElementKindHeader = @"Header";
NSString *NVBnbCollectionElementKindMoreLoader = @"MoreLoader";
@implementation NVBnbCollectionViewLayout {
CGSize _contentSize;
CGSize _groupSize;
CGSize _internalGridCellSize;
CGSize _internalParallaxCellSize;
CGSize _previousBoundsSize;
NSMutableDictionary *_cellAttributes;
UICollectionViewLayoutAttributes *_headerAttributes;
UICollectionViewLayoutAttributes *_moreLoaderAttributes;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
[self setDefaultValues];
}
return self;
}
- (instancetype)init {
if (self = [super init]) {
[self setDefaultValues];
}
return self;
}
- (void)prepareLayout {
NSLog(@"prepareLayout");
_internalGridCellSize = self.gridCellSize;
_internalParallaxCellSize = self.parallaxCellSize;
// Calculate content height
[self calculateContentSize];
// Calculate cell size
[self calculateCellSize];
// Calculate cell attributes
[self calculateCellAttributes];
// Calculate header attributes
[self calculateHeaderAttributes];
// Calculate more loader attributes
[self calculateMoreLoaderAttributes];
}
- (CGSize)collectionViewContentSize {
// NSLog(@"collectionViewContentSize");
return _contentSize;
}
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
// NSLog(@"layoutAttributesForElementsInRect");
NSMutableArray *result = [[NSMutableArray alloc] initWithCapacity:10];
NSInteger numberOfItems = [self.collectionView numberOfItemsInSection:0];
for (NSInteger itemCount = 0; itemCount < numberOfItems; itemCount++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:itemCount inSection:SECTION];
UICollectionViewLayoutAttributes *attributes = [_cellAttributes objectForKey:indexPath];
if (CGRectIntersectsRect(rect, attributes.frame)) {
[result addObject:attributes];
}
}
// Add header attributes to array if it is in rect
if (_headerAttributes && CGRectIntersectsRect(rect, _headerAttributes.frame)) {
[result addObject:_headerAttributes];
}
// Add more loader attributes to array if it is in rect
if (_moreLoaderAttributes && CGRectIntersectsRect(rect, _moreLoaderAttributes.frame)) {
[result addObject:_moreLoaderAttributes];
}
return result;
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewLayoutAttributes *attributes = [_cellAttributes objectForKey:indexPath];
NSLog(@"layoutAttributesForItemAtIndexPath");
return attributes;
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath {
return _headerAttributes;
}
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
NSLog(@"newBounds: %@", NSStringFromCGRect(newBounds));
if (CGSizeEqualToSize(_previousBoundsSize, CGSizeZero) && !CGSizeEqualToSize(_previousBoundsSize, newBounds.size)) {
_previousBoundsSize = newBounds.size;
return true;
}
return false;
}
#pragma mark - Calculation methods
- (void)calculateContentSize {
NSInteger numberOfItems = [self.collectionView numberOfItemsInSection:SECTION];
if (UIInterfaceOrientationIsPortrait(self.currentOrientation)) {
_groupSize.width = self.collectionView.bounds.size.width;
_groupSize.height = _internalGridCellSize.height * 6 + self.gridCellSpacing.height * 4 + _internalParallaxCellSize.height * 2 + self.gridPadding * 4;
_contentSize.width = self.collectionView.bounds.size.width;
_contentSize.height = _groupSize.height * (numberOfItems / 10);
} else {
_groupSize.width = _internalGridCellSize.width * 6 + self.gridCellSpacing.width * 4 + _internalParallaxCellSize.width * 2 + self.gridPadding * 4;
_groupSize.height = self.collectionView.bounds.size.height;
_contentSize.width = _groupSize.width * (numberOfItems / 10);
_contentSize.height = self.collectionView.bounds.size.height;
}
NSInteger numberOfItemsInLastGroup = numberOfItems % 10;
BOOL enableLoadMore = ((NVBnbCollectionView *) self.collectionView).enableLoadMore;
if (UIInterfaceOrientationIsPortrait(self.currentOrientation)) {
if (numberOfItemsInLastGroup > 0) {
_contentSize.height += _internalGridCellSize.height + self.gridPadding;
}
if (numberOfItemsInLastGroup > 1) {
_contentSize.height += _internalGridCellSize.height + self.gridCellSpacing.height;
}
if (numberOfItemsInLastGroup > 3) {
_contentSize.height += _internalParallaxCellSize.height + self.gridPadding;
}
if (numberOfItemsInLastGroup > 4) {
_contentSize.height += _internalGridCellSize.height + self.gridPadding;
}
if (numberOfItemsInLastGroup > 6) {
_contentSize.height += _internalGridCellSize.height * 2 + self.gridCellSpacing.height * 2;
}
if (numberOfItemsInLastGroup > 7) {
_contentSize.height += _internalGridCellSize.height + self.gridCellSpacing.height;
}
_contentSize.height += self.headerSize.height;
_contentSize.height += (enableLoadMore) ? self.moreLoaderSize.height : 0;
} else {
if (numberOfItemsInLastGroup > 0) {
_contentSize.width += _internalGridCellSize.width + self.gridPadding;
}
if (numberOfItemsInLastGroup > 1) {
_contentSize.width += _internalGridCellSize.width + self.gridCellSpacing.width;
}
if (numberOfItemsInLastGroup > 3) {
_contentSize.width += _internalParallaxCellSize.width + self.gridPadding;
}
if (numberOfItemsInLastGroup > 4) {
_contentSize.width += _internalGridCellSize.width + self.gridPadding;
}
if (numberOfItemsInLastGroup > 5) {
_contentSize.width += _internalGridCellSize.width * 2 + self.gridCellSpacing.width * 2;
}
if (numberOfItemsInLastGroup > 7) {
_contentSize.width += _internalGridCellSize.width + self.gridCellSpacing.width;
}
_contentSize.width += self.headerSize.width;
_contentSize.width += (enableLoadMore) ? self.moreLoaderSize.width : 0;
}
}
- (void)calculateCellSize {
if (UIInterfaceOrientationIsPortrait(self.currentOrientation)) {
_internalGridCellSize.width = (self.collectionView.frame.size.width - self.gridCellSpacing.width - self.gridPadding * 2) / 2;
_internalParallaxCellSize.width = self.collectionView.frame.size.width;
} else {
_internalGridCellSize.height = (self.collectionView.frame.size.height - self.gridCellSpacing.height - self.gridPadding * 2) / 2;
_internalParallaxCellSize.height = self.collectionView.frame.size.height;
}
}
- (void)calculateCellAttributes {
NSInteger numberOfItems = [self.collectionView numberOfItemsInSection:SECTION];
_cellAttributes = [[NSMutableDictionary alloc] initWithCapacity:numberOfItems];
for (NSInteger itemCount = 0; itemCount < numberOfItems; itemCount++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:itemCount inSection:SECTION];
UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
[_cellAttributes setObject:attributes forKey:indexPath];
}
CGFloat x = self.gridPadding;
CGFloat y = self.gridPadding;
// Give space for header
if (UIInterfaceOrientationIsPortrait(self.currentOrientation)) {
y += self.headerSize.height;
} else {
x += self.headerSize.width;
}
for (NSInteger itemCount = 0; itemCount < numberOfItems; itemCount++) {
NSInteger indexInGroup = itemCount % NUMBER_OF_ITEMS_IN_GROUP;
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:itemCount inSection:SECTION];
UICollectionViewLayoutAttributes *attributes = [_cellAttributes objectForKey:indexPath];
CGRect frame = CGRectZero;
if (UIInterfaceOrientationIsPortrait(self.currentOrientation)) {
switch (indexInGroup) {
case 0:
frame = CGRectMake(x, y, _internalGridCellSize.width, _internalGridCellSize.height);
break;
case 1:
frame = CGRectMake(x + _internalGridCellSize.width + self.gridCellSpacing.width, y, _internalGridCellSize.width, _internalGridCellSize.height * 2 + self.gridCellSpacing.height);
y += frame.size.height + self.gridPadding;
break;
case 2:
frame = CGRectMake(x, y - _internalGridCellSize.height - self.gridPadding, _internalGridCellSize.width, _internalGridCellSize.height);
break;
case 3:
frame = CGRectMake(0, y, _internalParallaxCellSize.width, _internalParallaxCellSize.height);
y += frame.size.height + self.gridPadding;
break;
case 4:
frame = CGRectMake(x, y, _internalGridCellSize.width, _internalGridCellSize.height);
break;
case 5:
frame = CGRectMake(x + _internalGridCellSize.width + self.gridCellSpacing.width, y, _internalGridCellSize.width, _internalGridCellSize.height);
y += frame.size.height + self.gridCellSpacing.height;
break;
case 6:
frame = CGRectMake(x, y, _internalGridCellSize.width * 2 + self.gridCellSpacing.width, _internalGridCellSize.height * 2 + self.gridCellSpacing.height);
y += frame.size.height + self.gridCellSpacing.height;
break;
case 7:
frame = CGRectMake(x, y, _internalGridCellSize.width, _internalGridCellSize.height);
break;
case 8:
frame = CGRectMake(x + _internalGridCellSize.width + self.gridCellSpacing.width, y, _internalGridCellSize.width, _internalGridCellSize.height);
y += frame.size.height + self.gridPadding;
break;
case 9:
frame = CGRectMake(0, y, _internalParallaxCellSize.width, _internalParallaxCellSize.height);
y += frame.size.height + self.gridPadding;
break;
default:
break;
}
} else {
switch (indexInGroup) {
case 0:
frame = CGRectMake(x, y, _internalGridCellSize.width, _internalGridCellSize.height);
break;
case 1:
frame = CGRectMake(x + _internalGridCellSize.width + self.gridCellSpacing.width, y, _internalGridCellSize.width, _internalGridCellSize.height * 2 + self.gridCellSpacing.height);
break;
case 2:
frame = CGRectMake(x, y + _internalGridCellSize.height + self.gridCellSpacing.height, _internalGridCellSize.width, _internalGridCellSize.height);
x += _internalGridCellSize.width * 2 + self.gridCellSpacing.width + self.gridPadding;
break;
case 3:
frame = CGRectMake(x, 0, _internalParallaxCellSize.width, _internalParallaxCellSize.height);
x += frame.size.width + self.gridPadding;
break;
case 4:
frame = CGRectMake(x, y, _internalGridCellSize.width, _internalGridCellSize.height);
break;
case 5:
frame = CGRectMake(x + _internalGridCellSize.width + self.gridCellSpacing.width, y, _internalGridCellSize.width * 2 + self.gridCellSpacing.width, _internalGridCellSize.height * 2 + self.gridCellSpacing.height);
break;
case 6:
frame = CGRectMake(x, y + _internalGridCellSize.height + self.gridCellSpacing.height, _internalGridCellSize.width, _internalGridCellSize.height);
x += _internalGridCellSize.width * 3 + self.gridCellSpacing.width * 3;
break;
case 7:
frame = CGRectMake(x, y, _internalGridCellSize.width, _internalGridCellSize.height);
break;
case 8:
frame = CGRectMake(x, y + _internalGridCellSize.height + self.gridCellSpacing.height, _internalGridCellSize.width, _internalGridCellSize.height);
x += frame.size.width + self.gridPadding;
break;
case 9:
frame = CGRectMake(x, 0, _internalParallaxCellSize.width, _internalParallaxCellSize.height);
x += frame.size.width + self.gridPadding;
break;
default:
break;
}
}
attributes.frame = frame;
}
}
- (void)calculateHeaderAttributes {
if (self.headerSize.width == 0 || self.headerSize.height == 0) {
return;
}
_headerAttributes = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:NVBnbCollectionElementKindHeader withIndexPath:[NSIndexPath indexPathForRow:0 inSection:SECTION]];
if (UIInterfaceOrientationIsPortrait(self.currentOrientation)) {
_headerAttributes.frame = CGRectMake(0, 0, self.collectionView.frame.size.width, self.headerSize.height);
} else {
_headerAttributes.frame = CGRectMake(0, 0, self.headerSize.width, self.collectionView.frame.size.height);
}
}
- (void)calculateMoreLoaderAttributes {
if (!((NVBnbCollectionView *) self.collectionView).enableLoadMore) {
_moreLoaderAttributes = nil;
return;
}
NSInteger numberOfItems = [self.collectionView numberOfItemsInSection:SECTION];
_moreLoaderAttributes = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:NVBnbCollectionElementKindMoreLoader withIndexPath:[NSIndexPath indexPathForRow:numberOfItems - 1 inSection:SECTION]];
if (UIInterfaceOrientationIsPortrait(self.currentOrientation)) {
_moreLoaderAttributes.frame = CGRectMake(0, _contentSize.height - self.moreLoaderSize.height, self.collectionView.frame.size.width, self.moreLoaderSize.height);
} else {
_moreLoaderAttributes.frame = CGRectMake(_contentSize.width - self.moreLoaderSize.width, 0, self.moreLoaderSize.width, self.collectionView.frame.size.height);
}
}
#pragma mark - Private methods
- (void)setDefaultValues {
_previousBoundsSize = CGSizeZero;
self.gridCellSize = GRID_CELL_SIZE;
self.parallaxCellSize = PARALLAX_CELL_SIZE;
self.gridCellSpacing = GRID_CELL_SPACING;
self.headerSize = HEADER_SIZE;
self.moreLoaderSize = MORE_LOADER_SIZE;
self.gridPadding = GRID_PADDING;
self.maxParallaxOffset = MAX_PARALLAX_OFFSET;
}
@end