-
Notifications
You must be signed in to change notification settings - Fork 0
/
TGLStackedLayout.m
executable file
·327 lines (217 loc) · 10.6 KB
/
TGLStackedLayout.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
//
// TGLStackedLayout.m
// TGLStackedViewController
//
// Created by Tim Gleue on 07.04.14.
// Copyright (c) 2014 Tim Gleue ( http://gleue-interactive.com )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#import "TGLStackedLayout.h"
@interface TGLStackedLayout ()
@property (nonatomic, strong) NSDictionary *layoutAttributes;
// Set to YES when layout is currently arranging
// items so that they evenly fill entire height
//
@property (nonatomic, assign) BOOL filling;
@end
@implementation TGLStackedLayout
- (instancetype)init {
self = [super init];
if (self) [self initLayout];
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) [self initLayout];
return self;
}
- (void)initLayout {
self.layoutMargin = UIEdgeInsetsMake(20.0, 0.0, 0.0, 0.0);
self.topReveal = 120.0;
self.bounceFactor = 0.2;
}
#pragma mark - Accessors
- (void)setLayoutMargin:(UIEdgeInsets)margins {
if (!UIEdgeInsetsEqualToEdgeInsets(margins, self.layoutMargin)) {
_layoutMargin = margins;
[self invalidateLayout];
}
}
- (void)setTopReveal:(CGFloat)topReveal {
if (topReveal != self.topReveal) {
_topReveal = topReveal;
[self invalidateLayout];
}
}
- (void)setItemSize:(CGSize)itemSize {
if (!CGSizeEqualToSize(itemSize, self.itemSize)) {
_itemSize = itemSize;
[self invalidateLayout];
}
}
- (void)setBounceFactor:(CGFloat)bounceFactor {
if (bounceFactor != self.bounceFactor) {
_bounceFactor = bounceFactor;
[self invalidateLayout];
}
}
- (void)setFillHeight:(BOOL)fillHeight {
if (fillHeight != self.isFillingHeight) {
_fillHeight = fillHeight;
[self invalidateLayout];
}
}
- (void)setAlwaysBounce:(BOOL)alwaysBounce {
if (alwaysBounce != self.alwaysBounce) {
_alwaysBounce = alwaysBounce;
[self invalidateLayout];
}
}
#pragma mark - Layout computation
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
return YES;
}
- (CGSize)collectionViewContentSize {
CGSize contentSize = CGSizeMake(CGRectGetWidth(self.collectionView.bounds), self.layoutMargin.top + self.topReveal * [self.collectionView numberOfItemsInSection:0] + self.layoutMargin.bottom - self.collectionView.contentInset.bottom);
if (contentSize.height < CGRectGetHeight(self.collectionView.bounds)) {
contentSize.height = CGRectGetHeight(self.collectionView.bounds) - self.collectionView.contentInset.top - self.collectionView.contentInset.bottom;
// Adding an extra point of content height
// enables scrolling/bouncing
//
if (self.isAlwaysBouncing) contentSize.height += 1.0;
self.filling = self.isFillingHeight;
} else {
self.filling = NO;
}
return contentSize;
}
- (void)prepareLayout {
// Force update of property -filling
// used to decide whether to arrange
// items evenly in collection view's
// full height
//
[self collectionViewContentSize];
CGFloat itemReveal = self.topReveal;
if (self.filling) {
itemReveal = floor((CGRectGetHeight(self.collectionView.bounds) - self.layoutMargin.top - self.layoutMargin.bottom - self.collectionView.contentInset.top - self.collectionView.contentInset.bottom) / [self.collectionView numberOfItemsInSection:0]);
}
CGSize itemSize = self.itemSize;
if (CGSizeEqualToSize(itemSize, CGSizeZero)) {
itemSize = CGSizeMake(CGRectGetWidth(self.collectionView.bounds) - self.layoutMargin.left - self.layoutMargin.right, CGRectGetHeight(self.collectionView.bounds) - self.layoutMargin.top - self.layoutMargin.bottom - self.collectionView.contentInset.top - self.collectionView.contentInset.bottom);
}
// Honor overwritten contentOffset
// exactly once
//
CGPoint contentOffset = self.overwriteContentOffset ? self.contentOffset : self.collectionView.contentOffset;
self.overwriteContentOffset = NO;
NSMutableDictionary *layoutAttributes = [NSMutableDictionary dictionary];
UICollectionViewLayoutAttributes *previousTopOverlappingAttributes[2] = { nil, nil };
NSInteger itemCount = [self.collectionView numberOfItemsInSection:0];
static NSInteger firstCompressingItem = -1;
for (NSInteger item = 0; item < itemCount; item++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:item inSection:0];
UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
// Cards overlap each other
// via z depth
//
attributes.zIndex = item;
// The moving item is hidden
//
attributes.hidden = [attributes.indexPath isEqual:self.movingIndexPath];
// By default all items are layed
// out evenly with each revealing
// only top part ...
//
attributes.frame = CGRectMake(self.layoutMargin.left, self.layoutMargin.top + itemReveal * item, itemSize.width, itemSize.height);
if (contentOffset.y + self.collectionView.contentInset.top < 0.0) {
// Expand cells when reaching top
// and user scrolls further down,
// i.e. when bouncing
//
CGRect frame = attributes.frame;
frame.origin.y -= self.bounceFactor * (contentOffset.y + self.collectionView.contentInset.top) * item;
attributes.frame = frame;
} else if (CGRectGetMinY(attributes.frame) < contentOffset.y + self.layoutMargin.top) {
// Topmost cells overlap stack, but
// are placed directly above each
// other such that only one cell
// is visible
//
CGRect frame = attributes.frame;
frame.origin.y = contentOffset.y + self.layoutMargin.top;
attributes.frame = frame;
// Keep queue of last two items'
// attributes and hide any item
// below top overlapping item to
// improve performance
//
if (previousTopOverlappingAttributes[1]) previousTopOverlappingAttributes[1].hidden = YES;
previousTopOverlappingAttributes[1] = previousTopOverlappingAttributes[0];
previousTopOverlappingAttributes[0] = attributes;
} else if (self.collectionViewContentSize.height > CGRectGetHeight(self.collectionView.bounds) && contentOffset.y > self.collectionViewContentSize.height - CGRectGetHeight(self.collectionView.bounds)) {
// Compress cells when reaching bottom
// and user scrolls further up,
// i.e. when bouncing
//
if (firstCompressingItem < 0) {
firstCompressingItem = item;
} else {
CGRect frame = attributes.frame;
CGFloat delta = contentOffset.y + CGRectGetHeight(self.collectionView.bounds) - self.collectionViewContentSize.height;
frame.origin.y += self.bounceFactor * delta * (firstCompressingItem - item);
frame.origin.y = MAX(frame.origin.y, contentOffset.y + self.layoutMargin.top);
attributes.frame = frame;
}
} else {
firstCompressingItem = -1;
}
layoutAttributes[indexPath] = attributes;
}
self.layoutAttributes = layoutAttributes;
}
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
NSMutableArray *layoutAttributes = [NSMutableArray array];
[self.layoutAttributes enumerateKeysAndObjectsUsingBlock:^(NSIndexPath *indexPath, UICollectionViewLayoutAttributes *attributes, BOOL *stop) {
if (CGRectIntersectsRect(rect, attributes.frame)) {
[layoutAttributes addObject:attributes];
}
}];
return layoutAttributes;
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
return self.layoutAttributes[indexPath];
}
#pragma mark - Methods
- (void)invalidateLayoutIfNecessaryWithMovingLocation:(CGPoint)movingLocation targetBlock:(NSIndexPath* (^) (NSIndexPath *sourceIndexPath, NSIndexPath *proposedDestinationIndexPath))targetBlock updateBlock:(void (^) (NSIndexPath *fromIndexPath, NSIndexPath *toIndexPath))updateBlock {
NSIndexPath *oldMovingIndexPath = self.movingIndexPath;
NSIndexPath *newMovingIndexPath = [self.collectionView indexPathForItemAtPoint:movingLocation];
newMovingIndexPath = targetBlock(oldMovingIndexPath, newMovingIndexPath);
if (newMovingIndexPath != nil && ![newMovingIndexPath isEqual:oldMovingIndexPath]) {
__weak typeof(self) weakSelf = self;
[self.collectionView performBatchUpdates:^ (void) {
[weakSelf.collectionView deleteItemsAtIndexPaths:@[ oldMovingIndexPath ]];
self.movingIndexPath = newMovingIndexPath;
updateBlock(oldMovingIndexPath, newMovingIndexPath);
[weakSelf.collectionView insertItemsAtIndexPaths:@[ newMovingIndexPath ]];
}
completion:nil];
}
}
@end