-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTGLStackedViewController.m
executable file
·536 lines (350 loc) · 18.7 KB
/
TGLStackedViewController.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
//
// TGLStackedViewController.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 "TGLStackedViewController.h"
#import "TGLStackedLayout.h"
#import "TGLExposedLayout.h"
#define MOVE_ZOOM 0.95
#define SCROLL_PER_FRAME 5.0
#define SCROLL_ZONE_TOP 100.0
#define SCROLL_ZONE_BOTTOM 100.0
typedef NS_ENUM(NSInteger, TGLStackedViewControllerScrollDirection) {
TGLStackedViewControllerScrollDirectionNone = 0,
TGLStackedViewControllerScrollDirectionDown,
TGLStackedViewControllerScrollDirectionUp
};
@interface TGLStackedViewController ()
@property (assign, nonatomic) CGPoint stackedContentOffset;
@property (strong, nonatomic) UIView *movingView;
@property (strong, nonatomic) NSIndexPath *movingIndexPath;
@property (strong, nonatomic) UILongPressGestureRecognizer *moveGestureRecognizer;
@property (assign, nonatomic) TGLStackedViewControllerScrollDirection scrollDirection;
@property (strong, nonatomic) CADisplayLink *scrollDisplayLink;
@end
@implementation TGLStackedViewController
@synthesize stackedLayout = _stackedLayout;
- (instancetype)init {
self = [super init];
if (self) [self initController];
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) [self initController];
return self;
}
- (instancetype)initWithCollectionViewLayout:(UICollectionViewLayout *)layout {
self = [super initWithCollectionViewLayout:layout];
if (self) [self initController];
return self;
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) [self initController];
return self;
}
- (void)initController {
_stackedLayout = [[TGLStackedLayout alloc] init];
_exposedLayoutMargin = UIEdgeInsetsMake(40.0, 0.0, 0.0, 0.0);
_exposedItemSize = CGSizeZero;
_exposedTopOverlap = 20.0;
_exposedBottomOverlap = 20.0;
_exposedBottomOverlapCount = 1;
}
#pragma mark - View life cycle
- (void)viewDidLoad {
[super viewDidLoad];
self.collectionView.collectionViewLayout = self.stackedLayout;
self.moveGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
self.moveGestureRecognizer.delegate = self;
[self.collectionView addGestureRecognizer:self.moveGestureRecognizer];
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
[self.collectionView.collectionViewLayout invalidateLayout];
}
#pragma mark - Accessors
- (void)setExposedItemIndexPath:(NSIndexPath *)exposedItemIndexPath {
if (![exposedItemIndexPath isEqual:_exposedItemIndexPath]) {
if (exposedItemIndexPath) {
// Select newly exposed item, possibly
// deslecting the previous selection,
// and animate to exposed layout
//
[self.collectionView selectItemAtIndexPath:exposedItemIndexPath animated:YES scrollPosition:UICollectionViewScrollPositionNone];
self.stackedContentOffset = self.collectionView.contentOffset;
TGLExposedLayout *exposedLayout = [[TGLExposedLayout alloc] initWithExposedItemIndex:exposedItemIndexPath.item];
exposedLayout.layoutMargin = self.exposedLayoutMargin;
exposedLayout.itemSize = self.exposedItemSize;
exposedLayout.topOverlap = self.exposedTopOverlap;
exposedLayout.bottomOverlap = self.exposedBottomOverlap;
exposedLayout.bottomOverlapCount = self.exposedBottomOverlapCount;
[self.collectionView setCollectionViewLayout:exposedLayout animated:YES];
} else {
// Deselect the currently exposed item
// and animate back to stacked layout
//
[self.collectionView deselectItemAtIndexPath:self.exposedItemIndexPath animated:YES];
self.stackedLayout.overwriteContentOffset = YES;
self.stackedLayout.contentOffset = self.stackedContentOffset;
// Issue #10: Collapsing on iOS 8
//
// NOTE: This solution produces a warning message
// "trying to load collection view layout
// data when layout is locked" but seems
// to work nevertheless.
//
[self.collectionView performBatchUpdates:^ {
[self.collectionView setContentOffset:self.stackedContentOffset animated:YES];
[self.collectionView setCollectionViewLayout:self.stackedLayout animated:YES];
} completion:nil];
}
_exposedItemIndexPath = exposedItemIndexPath;
}
}
#pragma mark - CollectionViewDataSource protocol
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
// Currently, only one single section is
// supported, therefore MUST NOT be != 1
//
return 1;
}
#pragma mark - CollectionViewDelegate protocol
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
// When selecting unexposed items is not allowed,
// prevent them from being highlighted and thus
// selected by the collection view
//
return self.unexposedItemsAreSelectable || self.exposedItemIndexPath == nil || [indexPath isEqual:self.exposedItemIndexPath];
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
if (!self.unexposedItemsAreSelectable && self.exposedItemIndexPath) {
// When selecting unexposed items is not allowed
// make sure the currently exposed item remains
// selected
//
[collectionView selectItemAtIndexPath:self.exposedItemIndexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
}
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
if ([indexPath isEqual:self.exposedItemIndexPath]) {
// Collapse currently exposed item
//
self.exposedItemIndexPath = nil;
} else if (self.unexposedItemsAreSelectable || self.exposedItemIndexPath == nil) {
// Expose new item, possibly collapsing
// the currently exposed item
//
self.exposedItemIndexPath = indexPath;
}
}
#pragma mark - GestureRecognizerDelegate protocol
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
// Long presses, i.e. moving items,
// only allowed when stacked
//
return (self.collectionView.collectionViewLayout == self.stackedLayout);
}
#pragma mark - Methods
- (BOOL)canMoveItemAtIndexPath:(NSIndexPath *)indexPath {
// Overload this method to prevent items
// from being dragged to another location
//
return YES;
}
- (NSIndexPath *)targetIndexPathForMoveFromItemAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath {
// Overload this method to modify an item's
// target location while being dragged to
// another proposed location
//
return proposedDestinationIndexPath;
}
- (void)moveItemAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
// Overload method to update collection
// view data source when item has been
// dragged to another location
}
#pragma mark - Actions
- (IBAction)handleLongPress:(UILongPressGestureRecognizer *)recognizer {
static CGPoint startCenter;
static CGPoint startLocation;
switch (recognizer.state) {
case UIGestureRecognizerStateBegan: {
startLocation = [recognizer locationInView:self.collectionView];
NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:startLocation];
if (indexPath && [self canMoveItemAtIndexPath:indexPath]) {
UICollectionViewCell *movingCell = [self.collectionView cellForItemAtIndexPath:indexPath];
self.movingView = [[UIView alloc] initWithFrame:movingCell.frame];
startCenter = self.movingView.center;
UIImageView *movingImageView = [[UIImageView alloc] initWithImage:[self screenshotImageOfItem:movingCell]];
movingImageView.alpha = 0.0f;
[self.movingView addSubview:movingImageView];
[self.collectionView addSubview:self.movingView];
self.movingIndexPath = indexPath;
__weak typeof(self) weakSelf = self;
[UIView animateWithDuration:0.3
delay:0.0
options:UIViewAnimationOptionBeginFromCurrentState
animations:^ (void) {
__strong typeof(self) strongSelf = weakSelf;
if (strongSelf) {
strongSelf.movingView.transform = CGAffineTransformMakeScale(MOVE_ZOOM, MOVE_ZOOM);
movingImageView.alpha = 1.0f;
}
}
completion:^ (BOOL finished) {
}];
self.stackedLayout.movingIndexPath = self.movingIndexPath;
[self.stackedLayout invalidateLayout];
}
break;
}
case UIGestureRecognizerStateChanged: {
if (self.movingIndexPath) {
CGPoint currentLocation = [recognizer locationInView:self.collectionView];
CGPoint currentCenter = startCenter;
currentCenter.y += (currentLocation.y - startLocation.y);
self.movingView.center = currentCenter;
if (currentLocation.y < CGRectGetMinY(self.collectionView.bounds) + SCROLL_ZONE_TOP && self.collectionView.contentOffset.y > SCROLL_ZONE_TOP) {
[self startScrollingUp];
} else if (currentLocation.y > CGRectGetMaxY(self.collectionView.bounds) - SCROLL_ZONE_BOTTOM && self.collectionView.contentOffset.y < self.collectionView.contentSize.height - CGRectGetHeight(self.collectionView.bounds) - SCROLL_ZONE_BOTTOM) {
[self startScrollingDown];
} else if (self.scrollDirection != TGLStackedViewControllerScrollDirectionNone) {
[self stopScrolling];
}
if (self.scrollDirection == TGLStackedViewControllerScrollDirectionNone) {
[self updateLayoutAtMovingLocation:currentLocation];
}
}
break;
}
case UIGestureRecognizerStateEnded:
case UIGestureRecognizerStateCancelled: {
if (self.movingIndexPath) {
[self stopScrolling];
UICollectionViewLayoutAttributes *layoutAttributes = [self.stackedLayout layoutAttributesForItemAtIndexPath:self.movingIndexPath];
self.movingIndexPath = nil;
__weak typeof(self) weakSelf = self;
[UIView animateWithDuration:0.3
delay:0.0
options:UIViewAnimationOptionBeginFromCurrentState
animations:^ (void) {
__strong typeof(self) strongSelf = weakSelf;
if (strongSelf) {
strongSelf.movingView.transform = CGAffineTransformMakeScale(1.0f, 1.0f);
strongSelf.movingView.frame = layoutAttributes.frame;
}
}
completion:^ (BOOL finished) {
__strong typeof(self) strongSelf = weakSelf;
if (strongSelf) {
[strongSelf.movingView removeFromSuperview];
strongSelf.movingView = nil;
self.stackedLayout.movingIndexPath = nil;
[strongSelf.stackedLayout invalidateLayout];
}
}];
}
break;
}
default:
break;
}
}
#pragma mark - Scrolling
- (void)startScrollingUp {
[self startScrollingInDirection:TGLStackedViewControllerScrollDirectionUp];
}
- (void)startScrollingDown {
[self startScrollingInDirection:TGLStackedViewControllerScrollDirectionDown];
}
- (void)startScrollingInDirection:(TGLStackedViewControllerScrollDirection)direction {
if (direction != TGLStackedViewControllerScrollDirectionNone && direction != self.scrollDirection) {
[self stopScrolling];
self.scrollDirection = direction;
self.scrollDisplayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(handleScrolling:)];
[self.scrollDisplayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
}
}
- (void)stopScrolling {
if (self.scrollDirection != TGLStackedViewControllerScrollDirectionNone) {
self.scrollDirection = TGLStackedViewControllerScrollDirectionNone;
[self.scrollDisplayLink invalidate];
self.scrollDisplayLink = nil;
}
}
- (void)handleScrolling:(CADisplayLink *)displayLink {
switch (self.scrollDirection) {
case TGLStackedViewControllerScrollDirectionUp: {
CGPoint offset = self.collectionView.contentOffset;
offset.y -= SCROLL_PER_FRAME;
if (offset.y > 0.0) {
self.collectionView.contentOffset = offset;
CGPoint center = self.movingView.center;
center.y -= SCROLL_PER_FRAME;
self.movingView.center = center;
} else {
[self stopScrolling];
CGPoint currentLocation = [self.moveGestureRecognizer locationInView:self.collectionView];
[self updateLayoutAtMovingLocation:currentLocation];
}
break;
}
case TGLStackedViewControllerScrollDirectionDown: {
CGPoint offset = self.collectionView.contentOffset;
offset.y += SCROLL_PER_FRAME;
if (offset.y < self.collectionView.contentSize.height - CGRectGetHeight(self.collectionView.bounds)) {
self.collectionView.contentOffset = offset;
CGPoint center = self.movingView.center;
center.y += SCROLL_PER_FRAME;
self.movingView.center = center;
} else {
[self stopScrolling];
CGPoint currentLocation = [self.moveGestureRecognizer locationInView:self.collectionView];
[self updateLayoutAtMovingLocation:currentLocation];
}
break;
}
default:
break;
}
}
#pragma mark - Helpers
- (UIImage *)screenshotImageOfItem:(UICollectionViewCell *)item {
UIGraphicsBeginImageContextWithOptions(item.bounds.size, item.isOpaque, 0.0f);
[item.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
- (void)updateLayoutAtMovingLocation:(CGPoint)movingLocation {
[self.stackedLayout invalidateLayoutIfNecessaryWithMovingLocation:movingLocation
targetBlock:^ (NSIndexPath *sourceIndexPath, NSIndexPath *proposedDestinationIndexPath) {
return [self targetIndexPathForMoveFromItemAtIndexPath:sourceIndexPath toProposedIndexPath:proposedDestinationIndexPath];
}
updateBlock:^ (NSIndexPath *fromIndexPath, NSIndexPath *toIndexPath){
[self moveItemAtIndexPath:fromIndexPath toIndexPath:toIndexPath];
self.movingIndexPath = toIndexPath;
}];
}
@end