-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSStickyHeaderFlowLayout.m
More file actions
208 lines (160 loc) · 8.05 KB
/
CSStickyHeaderFlowLayout.m
File metadata and controls
208 lines (160 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
/*
* 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";
@implementation CSStickyHeaderFlowLayout
- (void)prepareLayout {
[super prepareLayout];
}
- (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
{
// The rect should compensate the header size
CGRect adjustedRect = rect;
adjustedRect.origin.y -= self.parallaxHeaderReferenceSize.height;
NSMutableArray *allItems = [[super layoutAttributesForElementsInRect:adjustedRect] mutableCopy];
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];
if ([[obj representedElementKind] isEqualToString:UICollectionElementKindSectionHeader]) {
[headers setObject:obj forKey:@(indexPath.section)];
} else if ([[obj representedElementKind] isEqualToString:UICollectionElementKindSectionFooter]) {
// Not implemeneted
} else {
NSIndexPath *indexPath = [(UICollectionViewLayoutAttributes *)obj indexPath];
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;
}
}
// 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) && numberOfSections > 0) {
CSStickyHeaderFlowLayoutAttributes *currentAttribute = [CSStickyHeaderFlowLayoutAttributes layoutAttributesForSupplementaryViewOfKind:CSStickyHeaderParallaxHeader withIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
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(1, -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,
height,
};
[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 (header) {
[allItems addObject:header];
}
}
[self updateHeaderAttributes:header lastCellAttributes:lastCells[indexPathKey]];
}];
}
return allItems;
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewLayoutAttributes *attributes = [super layoutAttributesForItemAtIndexPath:indexPath];
CGRect frame = attributes.frame;
frame.origin.y += self.parallaxHeaderReferenceSize.height;
attributes.frame = frame;
return attributes;
}
- (CGSize)collectionViewContentSize {
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];
}
#pragma mark Helper
- (void)updateHeaderAttributes:(UICollectionViewLayoutAttributes *)attributes lastCellAttributes:(UICollectionViewLayoutAttributes *)lastCellAttributes
{
CGRect currentBounds = self.collectionView.bounds;
attributes.zIndex = 1024;
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
};
}
@end