forked from murraysagal/SSCollectionViewExchangeController
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSSCollectionViewExchangeController.m
More file actions
685 lines (473 loc) · 27.5 KB
/
SSCollectionViewExchangeController.m
File metadata and controls
685 lines (473 loc) · 27.5 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
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
//
// SSCollectionViewExchangeController.m
//
// Created by Murray Sagal on 1/9/2014.
// Copyright (c) 2014 Signature Software and Murray Sagal
// SSCollectionViewExchangeController: https://github.com/murraysagal/SSCollectionViewExchangeController
//
// 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 "SSCollectionViewExchangeController.h"
#import "SSCollectionViewExchangeLayout.h"
#import "UIView+SSCollectionViewExchangeControllerAdditions.h"
typedef NS_ENUM(NSInteger, ExchangeEventType) {
ExchangeEventTypeDraggedFromStartingItem,
ExchangeEventTypeDraggedToOtherItem,
ExchangeEventTypeDraggedToStartingItem,
ExchangeEventTypeNothingToExchange,
ExchangeEventTypeCannotDisplaceItem
};
@interface SSCollectionViewExchangeController () <SSCollectionViewExchangeLayoutDelegate>
@property (weak, nonatomic) id<SSCollectionViewExchangeControllerDelegate> delegate; // the delegate, which must conform to the SSCollectionViewExchangeControllerDelegate protocol
@property (weak, nonatomic) UICollectionView *collectionView; // the collection view that this exchange controller responds to
@property (weak, nonatomic, readwrite) UILongPressGestureRecognizer *longPressGestureRecognizer; // exposed as readonly in the header to allow configuration if required
@property (nonatomic) CGPoint locationInCollectionView; // the last known location of the long press
@property (strong, nonatomic) UIView *snapshot; // this is the view that follows the user's finger during the long press
@property (nonatomic) CGPoint offsetToCenterOfSnapshot; // for the snapshot, this is the offset from the location of the long press to its center
@property (strong, nonatomic) NSIndexPath *originalIndexPathForDraggedItem; // the index path where the exchange transaction began
@property (strong, nonatomic) NSIndexPath *originalIndexPathForDisplacedItem; // the original index path for the item most recently displaced item
@property (strong, nonatomic) NSIndexPath *currentIndexPath; // the index path for the item under locationInCollectionView
@property (nonatomic, assign) BOOL mustUndoPriorExchange; // for each exchange, tells the exchange controller if a prior exchange must be undone first
@property (nonatomic, assign) BOOL longPressWasManuallyCancelled; // in some cases the exchange controller needs to cancel the gesture recognizer, this flag distinguishes those cases from cases where the system cancels the recognizer
@property (nonatomic, assign, readwrite) BOOL exchangeTransactionInProgress; // exposed as readonly in the header to allow the delegate to determine if an exchange transaction is in progress
@property (nonatomic, copy) PostReleaseCompletionBlock postReleaseCompletionBlock; // refer to the comments in the header file
@property (strong, nonatomic) NSIndexPath *indexPathForItemLastChecked; // refer to the comments in delegateAllowsDisplacingItemAtIndexPath:withItemFromIndexPath:
@property (nonatomic, assign) BOOL resultForItemLastChecked; // ditto
// At the end of the exchange transation the snapshot is animated to the center of the cell
// that is hidden. But, as an optimization, the collection view does not create the view for cells
// that are hidden. So the center can't be determined at that time. So this property is set at
// the end of each exchange event just before the layout hides the item.
@property (nonatomic) CGPoint centerOfHiddenCell;
@end
@implementation SSCollectionViewExchangeController
- (id)initWithDelegate:(id<SSCollectionViewExchangeControllerDelegate>)delegate
collectionView:(UICollectionView *)collectionView
{
self = [super init];
if (self) {
// defaults...
_alphaForDisplacedItem = 0.60;
_animationDuration = 0.20;
_blinkToScaleForCatch = 1.20;
_blinkToScaleForRelease = 1.05;
_animationBacklogDelay = 0.50;
_snapshotAlpha = 0.80;
_snapshotBackgroundColor = [UIColor darkGrayColor];
_longPressWasManuallyCancelled = NO;
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress)];
longPress.minimumPressDuration = 0.15;
longPress.delaysTouchesBegan = YES;
[collectionView addGestureRecognizer:longPress];
collectionView.collectionViewLayout = [[SSCollectionViewExchangeLayout alloc] initWithDelegate:self];
_collectionView = collectionView;
_longPressGestureRecognizer = longPress;
_delegate = delegate;
}
return self;
}
//-------------------------
#pragma mark - Accessors...
- (UICollectionViewFlowLayout *)layout {
return (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
}
//-------------------------------------------------------------------------------
#pragma mark - UILongPressGestureRecognizer action method and exchange methods...
- (void)longPress {
switch (self.longPressGestureRecognizer.state) {
case UIGestureRecognizerStateBegan:
[self beginExchangeTransaction];
break;
case UIGestureRecognizerStateChanged:
[self updateSnapshotLocation];
[self performExchangeEventType];
break;
case UIGestureRecognizerStateEnded:
[self finishExchangeTransaction];
break;
case UIGestureRecognizerStateCancelled:
[self cancelExchangeTransaction];
break;
case UIGestureRecognizerStatePossible:
NSLog(@"UIGestureRecognizerStatePossible");
break;
case UIGestureRecognizerStateFailed:
NSLog(@"UIGestureRecognizerStateFailed");
break;
}
}
- (void)beginExchangeTransaction {
CGPoint locationInCollectionView = [self.longPressGestureRecognizer locationInView:self.collectionView];
NSIndexPath *startingIndexPath = [self.collectionView indexPathForItemAtPoint:locationInCollectionView];
if ([self cannotBeginExchangeTransactionWithItemAtIndexPath:startingIndexPath]) {
[self cancelLongPressRecognizer];
return;
}
self.exchangeTransactionInProgress = YES;
self.indexPathForItemLastChecked = nil;
UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:startingIndexPath];
UIView *snapshot = [self snapshotForCell:cell];
[self.collectionView addSubview:snapshot];
[self animateCatch:snapshot];
CGPoint locationInCell = [self.longPressGestureRecognizer locationInView:cell];
self.offsetToCenterOfSnapshot = [cell offsetToCenterFromPoint:locationInCell];
self.snapshot = snapshot;
self.centerOfHiddenCell = cell.center;
self.originalIndexPathForDraggedItem = startingIndexPath;
self.originalIndexPathForDisplacedItem = startingIndexPath;
self.mustUndoPriorExchange = NO;
// InvalidateLayout kicks off the process of redrawing the layout.
// SSCollectionViewExchangeLayout intervenes in that process by overriding
// layoutAttributesForElementsInRect: and layoutAttributesForItemAtIndexPath:
// to hide and dim collection view items as required.
[self.collectionView.collectionViewLayout invalidateLayout];
}
- (void)updateSnapshotLocation {
self.locationInCollectionView = [self.longPressGestureRecognizer locationInView:self.collectionView];
CGPoint offsetLocationInCollectionView = CGPointMake(self.locationInCollectionView.x - self.offsetToCenterOfSnapshot.x, self.locationInCollectionView.y - self.offsetToCenterOfSnapshot.y);
self.snapshot.center = offsetLocationInCollectionView;
}
- (ExchangeEventType)exchangeEventType {
// The user is still dragging in the long press. Determine the exchange event type.
self.currentIndexPath = [self.collectionView indexPathForItemAtPoint:self.locationInCollectionView];
if ([self isOverSameItemAtIndexPath:self.currentIndexPath] || self.currentIndexPath == nil) {
return ExchangeEventTypeNothingToExchange;
}
if ([self delegateAllowsDisplacingItemAtIndexPath:self.currentIndexPath
withItemFromIndexPath:self.originalIndexPathForDraggedItem] == NO) {
return ExchangeEventTypeCannotDisplaceItem;
}
// Otherwise there is an exchange event to perform. What kind?
if (self.mustUndoPriorExchange) {
return ([self isBackToStartingItemAtIndexPath:self.currentIndexPath])? ExchangeEventTypeDraggedToStartingItem : ExchangeEventTypeDraggedToOtherItem;
} else {
return ExchangeEventTypeDraggedFromStartingItem;
}
}
- (void)performExchangeEventType {
switch ([self exchangeEventType]) {
case ExchangeEventTypeNothingToExchange:
break;
case ExchangeEventTypeCannotDisplaceItem:
break;
case ExchangeEventTypeDraggedFromStartingItem:
[self performExchangeEventTypeDraggedFromStartingItem];
break;
case ExchangeEventTypeDraggedToOtherItem:
[self performExchangeEventTypeDraggedToOtherItem];
break;
case ExchangeEventTypeDraggedToStartingItem:
[self performExchangeEventTypeDraggedToStartingItem];
break;
}
}
- (void)performExchangeEventTypeDraggedFromStartingItem {
[self.collectionView performBatchUpdates:^{
// Model...
[self.delegate exchangeController:self
didExchangeItemAtIndexPath1:self.currentIndexPath
withItemAtIndexPath2:self.originalIndexPathForDraggedItem];
[self.delegate exchangeControllerDidFinishExchangeEvent:self];
// View...
[self ensureItemsToMoveAreFrontMost];
[self.collectionView moveItemAtIndexPath:self.originalIndexPathForDraggedItem toIndexPath:self.currentIndexPath];
[self.collectionView moveItemAtIndexPath:self.currentIndexPath toIndexPath:self.originalIndexPathForDraggedItem];
[self setPostExchangeEventStateWithIndexPathForDisplacedItem:self.currentIndexPath undoFlag:YES];
} completion:nil];
}
- (void)performExchangeEventTypeDraggedToOtherItem {
[self.collectionView performBatchUpdates:^{
// Model...
[self.delegate exchangeController:self
didExchangeItemAtIndexPath1:self.originalIndexPathForDraggedItem
withItemAtIndexPath2:self.originalIndexPathForDisplacedItem];
[self.delegate exchangeController:self
didExchangeItemAtIndexPath1:self.currentIndexPath
withItemAtIndexPath2:self.originalIndexPathForDraggedItem];
[self.delegate exchangeControllerDidFinishExchangeEvent:self];
// View...
[self ensureItemsToMoveAreFrontMost];
[self.collectionView moveItemAtIndexPath:self.originalIndexPathForDraggedItem toIndexPath:self.originalIndexPathForDisplacedItem];
[self.collectionView moveItemAtIndexPath:self.originalIndexPathForDisplacedItem toIndexPath:self.currentIndexPath];
[self.collectionView moveItemAtIndexPath:self.currentIndexPath toIndexPath:self.originalIndexPathForDraggedItem];
[self setPostExchangeEventStateWithIndexPathForDisplacedItem:self.currentIndexPath undoFlag:YES];
} completion:nil];
}
- (void)performExchangeEventTypeDraggedToStartingItem {
[self.collectionView performBatchUpdates:^{
// Model...
[self.delegate exchangeController:self
didExchangeItemAtIndexPath1:self.originalIndexPathForDraggedItem
withItemAtIndexPath2:self.originalIndexPathForDisplacedItem];
[self.delegate exchangeControllerDidFinishExchangeEvent:self];
// View...
[self ensureItemsToMoveAreFrontMost];
[self.collectionView moveItemAtIndexPath:self.originalIndexPathForDraggedItem toIndexPath:self.originalIndexPathForDisplacedItem];
[self.collectionView moveItemAtIndexPath:self.originalIndexPathForDisplacedItem toIndexPath:self.originalIndexPathForDraggedItem];
[self setPostExchangeEventStateWithIndexPathForDisplacedItem:self.self.originalIndexPathForDraggedItem undoFlag:NO];
} completion:nil];
}
- (void)finishExchangeTransaction {
[self.delegate exchangeControllerDidFinishExchangeTransaction:self
withIndexPath1:self.originalIndexPathForDisplacedItem
indexPath2:self.originalIndexPathForDraggedItem];
[self animateRelease];
self.exchangeTransactionInProgress = NO;
}
- (void)cancelExchangeTransaction {
// In certain circumstances the exchange controller can manually cancel the gesture recognizer.
// This needs to be distinguished from system events, like an incoming phone call, that can
// also cancel the gesture recognizer.
if (self.longPressWasManuallyCancelled) {
self.longPressWasManuallyCancelled = NO;
} else {
// Control reaches here, for example, if the user gets a phone call in the middle of the long press.
// So the delegate can undo the last exchange in its model and thus
// return it to its pre-exchange transaction state...
[self.delegate exchangeController:self
didExchangeItemAtIndexPath1:self.originalIndexPathForDisplacedItem
withItemAtIndexPath2:self.originalIndexPathForDraggedItem];
// So the delegate has an opportunity to update its view...
[self.delegate exchangeControllerDidCancelExchangeTransaction:self];
self.originalIndexPathForDisplacedItem = nil;
self.originalIndexPathForDraggedItem = nil;
[self.snapshot removeFromSuperview];
self.exchangeTransactionInProgress = NO;
// Why delay? Refer to the comments for the animationBacklogDelay property in the .h file.
[self performBlock:^{ [self.collectionView reloadData]; }
afterDelay:self.animationBacklogDelay];
}
}
//---------------------------------------
#pragma mark - Exchange helper methods...
- (void)cancelLongPressRecognizer {
self.longPressWasManuallyCancelled = YES;
self.longPressGestureRecognizer.enabled = NO;
self.longPressGestureRecognizer.enabled = YES;
}
- (BOOL)canBeginExchangeTransactionWithItemAtIndexPath:(NSIndexPath *)indexPath {
// There are several conditions that must be true to begin a transaction...
// 1. indexPath must not be nil
// 2. the location (of the user's finger) must be in the catch rectangle
// 3. the delegate allows the exchange to begin
if (indexPath == nil) return NO;
if ([self locationIsInCatchRectangleForItemAtIndexPath:indexPath] == NO) return NO;
if ([self delegateAllowsExchangeToBeginWithItemAtIndexPath:indexPath] == NO) return NO;
// Otherwise...
return YES;
}
- (BOOL)cannotBeginExchangeTransactionWithItemAtIndexPath:(NSIndexPath *)indexPath {
return ![self canBeginExchangeTransactionWithItemAtIndexPath:indexPath];
}
- (BOOL)delegateAllowsExchangeToBeginWithItemAtIndexPath:(NSIndexPath *)indexPath {
BOOL delegateAllowsExchangeToBeginWithItemAtIndexPath = YES;
if ([self.delegate respondsToSelector:@selector(exchangeControllerCanBeginExchangeTransaction:withItemAtIndexPath:)]) {
delegateAllowsExchangeToBeginWithItemAtIndexPath = [self.delegate exchangeControllerCanBeginExchangeTransaction:self
withItemAtIndexPath:indexPath];
}
return delegateAllowsExchangeToBeginWithItemAtIndexPath;
}
- (BOOL)delegateAllowsDisplacingItemAtIndexPath:(NSIndexPath *)indexPathForItemToDisplace
withItemFromIndexPath:(NSIndexPath *)indexPathOfItemBeingDragged {
// This if() is here to prevent repeated calls to the delegate. With this if() the delegate
// is asked only once until indexPathForItemToDisplace changes.
if ([indexPathForItemToDisplace isEqual:self.indexPathForItemLastChecked]) {
return self.resultForItemLastChecked;
} else {
self.indexPathForItemLastChecked = indexPathForItemToDisplace;
BOOL delegateAllowsDisplacingItemAtIndexPath = YES;
if ([self.delegate respondsToSelector:@selector(exchangeController:canDisplaceItemAtIndexPath:withItemBeingDraggedFromIndexPath:)]) {
delegateAllowsDisplacingItemAtIndexPath = [self.delegate exchangeController:self
canDisplaceItemAtIndexPath:indexPathForItemToDisplace
withItemBeingDraggedFromIndexPath:indexPathOfItemBeingDragged];
}
self.resultForItemLastChecked = delegateAllowsDisplacingItemAtIndexPath;
return delegateAllowsDisplacingItemAtIndexPath;
}
}
- (BOOL)locationIsInCatchRectangleForItemAtIndexPath:(NSIndexPath *)indexPath {
BOOL locationIsInCatchRectangle;
UIView *viewForCatchRectangle;
if ([self.delegate respondsToSelector:@selector(exchangeController:viewForCatchRectangleForItemAtIndexPath:)]) {
viewForCatchRectangle = [self.delegate exchangeController:self
viewForCatchRectangleForItemAtIndexPath:indexPath];
}
if (viewForCatchRectangle) {
CGPoint locationInCatchRectangle = [self.longPressGestureRecognizer locationInView:viewForCatchRectangle];
locationIsInCatchRectangle = CGRectContainsPoint(viewForCatchRectangle.bounds, locationInCatchRectangle);
} else {
locationIsInCatchRectangle = YES;
}
return locationIsInCatchRectangle;
}
- (void)ensureItemsToMoveAreFrontMost {
// It can happen that the cells being exchanged are not the frontmost in the view. In that case
// the move animations are obscured behind other collection view items.
UICollectionViewCell *cell;
cell = [self.collectionView cellForItemAtIndexPath:self.originalIndexPathForDraggedItem];
[self.collectionView bringSubviewToFront:cell];
cell = [self.collectionView cellForItemAtIndexPath:self.currentIndexPath];
[self.collectionView bringSubviewToFront:cell];
[self.collectionView bringSubviewToFront:self.snapshot];
}
- (void)setPostExchangeEventStateWithIndexPathForDisplacedItem:(NSIndexPath *)indexPathForDisplacedItem
undoFlag:(BOOL)undoFlag {
self.originalIndexPathForDisplacedItem = indexPathForDisplacedItem;
self.mustUndoPriorExchange = undoFlag;
self.centerOfHiddenCell = [self.collectionView cellForItemAtIndexPath:self.currentIndexPath].center;
}
- (UIView *)snapshotForCell:(UICollectionViewCell *)cell {
UIView *snapshot;
if ([self.delegate respondsToSelector:@selector(exchangeController:snapshotForCell:)]) {
return [self.delegate exchangeController:self snapshotForCell:cell];
} else {
BOOL shouldApplyBackgroundColor = self.snapshotBackgroundColor != nil;
UIColor *originalBackgroundColor;
if (shouldApplyBackgroundColor) {
originalBackgroundColor = cell.backgroundColor;
cell.backgroundColor = self.snapshotBackgroundColor;
}
float originalAlpha = cell.alpha;
cell.alpha = self.snapshotAlpha;
// Normally, it would be appropriate to use UIView's snapshotViewAfterScreenUpdates:.
// Like this...
//
// snapshot = [cell snapshotViewAfterScreenUpdates:YES];
//
// But when that runs on an iPad (device or simulator) there is an annoying screen
// flash. The flash doesn't happen if NO is passed to snapshotViewAfterScreenUpdates:
// but then the default background colour and alpha aren't applied. So for now
// I've gone back to the UIGraphics method for generating the snapshot.
//////////////////////////////////////////////////////
UIGraphicsBeginImageContextWithOptions(cell.bounds.size, cell.opaque, 0.0f);
[cell.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *cellImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
snapshot = [[UIImageView alloc] initWithImage:cellImage];
//////////////////////////////////////////////////////
// snapshot = [cell snapshotViewAfterScreenUpdates:YES];
// This can replace the UIGraphics code above if it starts to work on the iPad.
snapshot.frame = cell.frame;
if (shouldApplyBackgroundColor) {
cell.backgroundColor = originalBackgroundColor;
}
cell.alpha = originalAlpha;
return snapshot;
}
}
- (void)animateCatch:(UIView *)snapshot {
if ([self.delegate respondsToSelector:@selector(animateCatchForExchangeController:withSnapshot:)]) {
[self.delegate animateCatchForExchangeController:self withSnapshot:snapshot];
} else {
NSTimeInterval duration = self.animationDuration;
CGFloat blinkToScale = self.blinkToScaleForCatch;
CGFloat finalScale = 1.0;
[UIView animateWithDuration:duration animations:^ {
snapshot.transform = CGAffineTransformMakeScale(blinkToScale, blinkToScale);
} completion:^(BOOL finished) {
[UIView animateWithDuration:duration animations:^ {
snapshot.transform = CGAffineTransformMakeScale(finalScale, finalScale);
}];
}];
}
}
- (void)animateRelease {
self.longPressGestureRecognizer.enabled = NO;
UICollectionViewCell *cellForOriginalLocation = [self.collectionView cellForItemAtIndexPath:self.originalIndexPathForDraggedItem];
if ([self.delegate respondsToSelector:@selector(animateReleaseForExchangeController:withSnapshot:toPoint:originalIndexPathForDraggedItem:completionBlock:)]) {
[self.delegate animateReleaseForExchangeController:self
withSnapshot:self.snapshot
toPoint:self.centerOfHiddenCell
originalIndexPathForDraggedItem:self.originalIndexPathForDraggedItem
completionBlock:self.postReleaseCompletionBlock];
} else {
NSTimeInterval duration = self.animationDuration;
CGFloat blinkToScale = self.blinkToScaleForRelease;
CGFloat finalScale = 1.0;
[UIView animateWithDuration:duration animations:^ {
self.snapshot.center = self.centerOfHiddenCell;
cellForOriginalLocation.alpha = 1.0;
} completion:^(BOOL finished) {
[UIView animateWithDuration:duration animations:^ {
self.snapshot.transform = CGAffineTransformMakeScale(blinkToScale, blinkToScale);
} completion:^(BOOL finished) {
[UIView animateWithDuration:duration animations:^ {
self.snapshot.transform = CGAffineTransformMakeScale(finalScale, finalScale);
} completion:^(BOOL finished) {
self.postReleaseCompletionBlock(duration);
}];
}];
}];
}
}
- (BOOL)isOverSameItemAtIndexPath:(NSIndexPath *)indexPath {
return [indexPath isEqual:self.originalIndexPathForDisplacedItem];
}
- (BOOL)isBackToStartingItemAtIndexPath:(NSIndexPath *)indexPath {
return [indexPath isEqual:self.originalIndexPathForDraggedItem];
}
- (UIView *)snapshotForView:(UIView *)view
withBackgroundColor:(UIColor *)backgroundColor
alpha:(float)alpha {
UIColor *originalBackgroundColor = view.backgroundColor;
float originialAlpha = view.alpha;
view.backgroundColor = backgroundColor;
view.alpha = alpha;
UIView *snapshot = [view snapshotViewAfterScreenUpdates:YES];
view.backgroundColor = originalBackgroundColor;
view.alpha = originialAlpha;
return snapshot;
}
- (void)performBlock:(void (^) ())block afterDelay:(double)delay {
double delayInSeconds = delay;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
block();
});
}
- (PostReleaseCompletionBlock)postReleaseCompletionBlock {
__weak SSCollectionViewExchangeController *weakSelf = self;
return ^ void (NSTimeInterval duration) {
weakSelf.originalIndexPathForDisplacedItem = nil;
weakSelf.originalIndexPathForDraggedItem = nil;
[weakSelf.collectionView.collectionViewLayout invalidateLayout];
[UIView animateWithDuration:duration animations:^ {
weakSelf.snapshot.alpha = 0.0;
} completion:^(BOOL finished) {
[weakSelf.snapshot removeFromSuperview];
weakSelf.snapshot = nil;
self.longPressGestureRecognizer.enabled = YES;
}];
};
}
//--------------------------------------------------------------
#pragma mark - SSCollectionViewExchangeLayoutDelegate methods...
- (NSIndexPath *)indexPathForItemToHide {
return self.originalIndexPathForDisplacedItem;
// Return nil if you don't want to hide.
// This can be useful during testing to ensure that the item
// you're dragging around is properly following.
}
- (NSIndexPath *)indexPathForItemToDim {
return self.originalIndexPathForDraggedItem;
// As above return nil if you don't want to dim.
}
- (CGFloat)alphaForItemToDim {
return self.alphaForDisplacedItem;
}
@end