-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSStickyHeaderFlowLayout.m
More file actions
337 lines (259 loc) · 12.9 KB
/
CSStickyHeaderFlowLayout.m
File metadata and controls
337 lines (259 loc) · 12.9 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
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
/*
* This file is part of the CSStickyHeaderFlowLayout package.
* (c) James Tang <j@jamztang.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
#import "CSStickyHeaderFlowLayout.h"
#import "CSStickyHeaderFlowLayoutAttributes.h"
NSString *const CSStickyHeaderParallaxHeader = @"CSStickyHeaderParallexHeader";
static const NSInteger kHeaderZIndex = 1024;
@interface CSStickyHeaderFlowLayout (Debug)
- (void)debugLayoutAttributes:(NSArray *)layoutAttributes;
@end
@implementation CSStickyHeaderFlowLayout
- (void)prepareLayout {
[super prepareLayout];
}
- (UICollectionViewLayoutAttributes *)initialLayoutAttributesForAppearingSupplementaryElementOfKind:(NSString *)elementKind
atIndexPath:(NSIndexPath *)elementIndexPath {
UICollectionViewLayoutAttributes *attributes = [super initialLayoutAttributesForAppearingSupplementaryElementOfKind:elementKind atIndexPath:elementIndexPath];
if ([elementKind isEqualToString:CSStickyHeaderParallaxHeader]) {
// sticky header do not need to offset
return nil;
} else {
// offset others
CGRect frame = attributes.frame;
frame.origin.y += self.parallaxHeaderReferenceSize.height;
attributes.frame = frame;
}
return attributes;
}
- (UICollectionViewLayoutAttributes *)finalLayoutAttributesForDisappearingSupplementaryElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)elementIndexPath {
if ([elementKind isEqualToString:CSStickyHeaderParallaxHeader]) {
CSStickyHeaderFlowLayoutAttributes *attribute = (CSStickyHeaderFlowLayoutAttributes *)[self layoutAttributesForSupplementaryViewOfKind:elementKind atIndexPath:elementIndexPath];
[self updateParallaxHeaderAttribute:attribute];
return attribute;
} else {
return [super finalLayoutAttributesForDisappearingSupplementaryElementOfKind:elementKind atIndexPath:elementIndexPath];
}
return nil;
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewLayoutAttributes *attributes = [super layoutAttributesForSupplementaryViewOfKind:kind atIndexPath:indexPath];
if (!attributes && [kind isEqualToString:CSStickyHeaderParallaxHeader]) {
attributes = [CSStickyHeaderFlowLayoutAttributes layoutAttributesForSupplementaryViewOfKind:kind withIndexPath:indexPath];
}
return attributes;
}
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
if (self.collectionView.dataSource != nil) {
// The rect should compensate the header size
CGRect adjustedRect = rect;
adjustedRect.origin.y -= self.parallaxHeaderReferenceSize.height;
NSMutableArray *allItems = [NSMutableArray array];
NSArray *originalAttributes = [super layoutAttributesForElementsInRect:adjustedRect];
//Perform a deep copy of the attributes returned from super
for (UICollectionViewLayoutAttributes *originalAttribute in originalAttributes) {
[allItems addObject:[originalAttribute copy]];
}
NSMutableDictionary *headers = [[NSMutableDictionary alloc] init];
NSMutableDictionary *lastCells = [[NSMutableDictionary alloc] init];
__block BOOL visibleParallexHeader;
[allItems enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
UICollectionViewLayoutAttributes *attributes = obj;
CGRect frame = attributes.frame;
frame.origin.y += self.parallaxHeaderReferenceSize.height;
attributes.frame = frame;
NSIndexPath *indexPath = [(UICollectionViewLayoutAttributes *)obj indexPath];
BOOL isHeader = [[obj representedElementKind] isEqualToString:UICollectionElementKindSectionHeader];
BOOL isFooter = [[obj representedElementKind] isEqualToString:UICollectionElementKindSectionFooter];
if (isHeader) {
[headers setObject:obj forKey:@(indexPath.section)];
} else if (isFooter) {
// Not implemeneted
} else {
UICollectionViewLayoutAttributes *currentAttribute = [lastCells objectForKey:@(indexPath.section)];
// Get the bottom most cell of that section
if ( ! currentAttribute || indexPath.row > currentAttribute.indexPath.row) {
[lastCells setObject:obj forKey:@(indexPath.section)];
}
if ([indexPath item] == 0 && [indexPath section] == 0) {
visibleParallexHeader = YES;
}
}
if (isHeader) {
attributes.zIndex = kHeaderZIndex;
} else {
// For iOS 7.0, the cell zIndex should be above sticky section header
attributes.zIndex = 1;
}
}];
// when the visible rect is at top of the screen, make sure we see
// the parallex header
if (CGRectGetMinY(rect) <= 0) {
visibleParallexHeader = YES;
}
if (self.parallaxHeaderAlwaysOnTop == YES) {
visibleParallexHeader = YES;
}
// This method may not be explicitly defined, default to 1
// https://developer.apple.com/library/ios/documentation/uikit/reference/UICollectionViewDataSource_protocol/Reference/Reference.html#jumpTo_6
// NSUInteger numberOfSections = [self.collectionView.dataSource
// respondsToSelector:@selector(numberOfSectionsInCollectionView:)]
// ? [self.collectionView.dataSource numberOfSectionsInCollectionView:self.collectionView]
// : 1;
// Create the attributes for the Parallex header
if (visibleParallexHeader && ! CGSizeEqualToSize(CGSizeZero, self.parallaxHeaderReferenceSize)) {
CSStickyHeaderFlowLayoutAttributes *currentAttribute = [CSStickyHeaderFlowLayoutAttributes layoutAttributesForSupplementaryViewOfKind:CSStickyHeaderParallaxHeader withIndexPath:[NSIndexPath indexPathWithIndex:0]];
[self updateParallaxHeaderAttribute:currentAttribute];
[allItems addObject:currentAttribute];
}
if ( ! self.disableStickyHeaders) {
[lastCells enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSIndexPath *indexPath = [obj indexPath];
NSNumber *indexPathKey = @(indexPath.section);
UICollectionViewLayoutAttributes *header = headers[indexPathKey];
// CollectionView automatically removes headers not in bounds
if ( ! header) {
header = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader
atIndexPath:[NSIndexPath indexPathForItem:0 inSection:indexPath.section]];
if (!CGSizeEqualToSize(CGSizeZero, header.frame.size)) {
[allItems addObject:header];
}
}
if (!CGSizeEqualToSize(CGSizeZero, header.frame.size)) {
[self updateHeaderAttributes:header lastCellAttributes:lastCells[indexPathKey]];
}
}];
}
// For debugging purpose
// [self debugLayoutAttributes:allItems];
return allItems;
} else {
return nil;
}
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewLayoutAttributes *attributes = [super layoutAttributesForItemAtIndexPath:indexPath].copy;
CGRect frame = attributes.frame;
frame.origin.y += self.parallaxHeaderReferenceSize.height;
attributes.frame = frame;
return attributes;
}
- (CGSize)collectionViewContentSize {
// If not part of view hierarchy then return CGSizeZero (as in docs).
// Call [super collectionViewContentSize] can cause EXC_BAD_ACCESS when collectionView has no superview.
if (!self.collectionView.superview) {
return CGSizeZero;
}
CGSize size = [super collectionViewContentSize];
size.height += self.parallaxHeaderReferenceSize.height;
return size;
}
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
return YES;
}
#pragma mark Overrides
+ (Class)layoutAttributesClass {
return [CSStickyHeaderFlowLayoutAttributes class];
}
- (void)setParallaxHeaderReferenceSize:(CGSize)parallaxHeaderReferenceSize {
_parallaxHeaderReferenceSize = parallaxHeaderReferenceSize;
// Make sure we update the layout
[self invalidateLayout];
}
#pragma mark Helper
- (void)updateHeaderAttributes:(UICollectionViewLayoutAttributes *)attributes lastCellAttributes:(UICollectionViewLayoutAttributes *)lastCellAttributes
{
CGRect currentBounds = self.collectionView.bounds;
attributes.zIndex = kHeaderZIndex;
attributes.hidden = NO;
CGPoint origin = attributes.frame.origin;
CGFloat sectionMaxY = CGRectGetMaxY(lastCellAttributes.frame) - attributes.frame.size.height;
CGFloat y = CGRectGetMaxY(currentBounds) - currentBounds.size.height + self.collectionView.contentInset.top;
if (self.parallaxHeaderAlwaysOnTop) {
y += self.parallaxHeaderMinimumReferenceSize.height;
}
CGFloat maxY = MIN(MAX(y, attributes.frame.origin.y), sectionMaxY);
// NSLog(@"%.2f, %.2f, %.2f", y, maxY, sectionMaxY);
origin.y = maxY;
attributes.frame = (CGRect){
origin,
attributes.frame.size
};
}
- (void)updateParallaxHeaderAttribute:(CSStickyHeaderFlowLayoutAttributes *)currentAttribute {
CGRect frame = currentAttribute.frame;
frame.size.width = self.parallaxHeaderReferenceSize.width;
frame.size.height = self.parallaxHeaderReferenceSize.height;
CGRect bounds = self.collectionView.bounds;
CGFloat maxY = CGRectGetMaxY(frame);
// make sure the frame won't be negative values
CGFloat y = MIN(maxY - self.parallaxHeaderMinimumReferenceSize.height, bounds.origin.y + self.collectionView.contentInset.top);
CGFloat height = MAX(0, -y + maxY);
CGFloat maxHeight = self.parallaxHeaderReferenceSize.height;
CGFloat minHeight = self.parallaxHeaderMinimumReferenceSize.height;
CGFloat progressiveness = (height - minHeight)/(maxHeight - minHeight);
currentAttribute.progressiveness = progressiveness;
// if zIndex < 0 would prevents tap from recognized right under navigation bar
currentAttribute.zIndex = 0;
// When parallaxHeaderAlwaysOnTop is enabled, we will check when we should update the y position
if (self.parallaxHeaderAlwaysOnTop && height <= self.parallaxHeaderMinimumReferenceSize.height) {
CGFloat insetTop = self.collectionView.contentInset.top;
// Always stick to top but under the nav bar
y = self.collectionView.contentOffset.y + insetTop;
currentAttribute.zIndex = 2000;
}
currentAttribute.frame = (CGRect){
frame.origin.x,
y,
frame.size.width,
self.disableStretching && height > maxHeight ? maxHeight : height,
};
}
@end
#pragma mark - Debugging
@implementation CSStickyHeaderFlowLayoutAttributes (Debug)
- (NSString *)description {
NSString *indexPathString = [NSString stringWithFormat:@"{%ld, %ld}", (long)self.indexPath.section, (long)self.indexPath.item];
NSString *desc = [NSString stringWithFormat:@"<CSStickyHeaderFlowLayout: %p> indexPath: %@ zIndex: %ld valid: %@ kind: %@", self, indexPathString, (long)self.zIndex, [self isValid] ? @"YES" : @"NO", self.representedElementKind ?: @"cell"];
return desc;
}
- (BOOL)isValid {
switch (self.representedElementCategory) {
case UICollectionElementCategoryCell:
if (self.zIndex != 1) {
return NO;
}
return YES;
case UICollectionElementCategorySupplementaryView:
if ([self.representedElementKind isEqualToString:CSStickyHeaderParallaxHeader]) {
return YES;
} else if (self.zIndex < 1024) {
return NO;
}
return YES;
default:
return YES;
}
}
@end
@implementation CSStickyHeaderFlowLayout (Debug)
- (void)debugLayoutAttributes:(NSArray *)layoutAttributes {
__block BOOL hasInvalid = NO;
[layoutAttributes enumerateObjectsUsingBlock:^(CSStickyHeaderFlowLayoutAttributes *attr, NSUInteger idx, BOOL *stop) {
hasInvalid = ![attr isValid];
if (hasInvalid) {
*stop = YES;
}
}];
if (hasInvalid) {
NSLog(@"CSStickyHeaderFlowLayout: %@", layoutAttributes);
}
}
@end