-
-
Save skagedal/0babbfb6caf9d35e9ef31b70e24824d5 to your computer and use it in GitHub Desktop.
Smoothly deselect table and collection view cells on dismissal, including interactive dismiss and interactively-partially-dismiss-then-cancel-then-dismiss-again
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension UIViewController { | |
func rz_smoothlyDeselectRows(tableView tableView: UITableView?) { | |
let selectedIndexPaths = tableView?.indexPathsForSelectedRows ?? [] | |
if let coordinator = transitionCoordinator() { | |
coordinator.animateAlongsideTransitionInView(parentViewController?.view, animation: { context in | |
selectedIndexPaths.forEach { | |
tableView?.deselectRowAtIndexPath($0, animated: context.isAnimated()) | |
} | |
}, completion: { context in | |
if context.isCancelled() { | |
selectedIndexPaths.forEach { | |
tableView?.selectRowAtIndexPath($0, animated: false, scrollPosition: .None) | |
} | |
} | |
}) | |
} | |
else { | |
selectedIndexPaths.forEach { | |
tableView?.deselectRowAtIndexPath($0, animated: false) | |
} | |
} | |
} | |
func rz_smoothlyDeselectItems(collectionView collectionView: UICollectionView?) { | |
let selectedIndexPaths = collectionView?.indexPathsForSelectedItems() ?? [] | |
if let coordinator = transitionCoordinator() { | |
coordinator.animateAlongsideTransitionInView(parentViewController?.view, animation: { context in | |
selectedIndexPaths.forEach { | |
collectionView?.deselectItemAtIndexPath($0, animated: context.isAnimated()) | |
} | |
}, completion: { context in | |
if context.isCancelled() { | |
selectedIndexPaths.forEach { | |
collectionView?.selectItemAtIndexPath($0, animated: false, scrollPosition: .None) | |
} | |
} | |
}) | |
} | |
else { | |
selectedIndexPaths.forEach { | |
collectionView?.deselectItemAtIndexPath($0, animated: false) | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@import UIKit; | |
@interface UIViewController (RZDeselection) | |
- (void)rz_smoothlyDeselectRowsInTableView:(UITableView *)tableView; | |
- (void)rz_smoothlyDeselectItemsInCollectionView:(UICollectionView *)collectionView; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "UIViewController+RZDeselection.h" | |
@implementation UIViewController (RZDeselection) | |
- (void)rz_smoothlyDeselectRowsInTableView:(UITableView *)tableView | |
{ | |
NSArray<NSIndexPath *> *selectedIndexPaths = [tableView indexPathsForSelectedRows]; | |
if (self.transitionCoordinator) { | |
[self.transitionCoordinator animateAlongsideTransitionInView:self.parentViewController.view animation:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) { | |
for (NSIndexPath *indexPath in selectedIndexPaths) { | |
[tableView deselectRowAtIndexPath:indexPath animated:context.isAnimated]; | |
} | |
} completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) { | |
if (context.isCancelled) { | |
for (NSIndexPath *indexPath in selectedIndexPaths) { | |
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone]; | |
} | |
} | |
}]; | |
} | |
else { | |
for (NSIndexPath *indexPath in selectedIndexPaths) { | |
[tableView deselectRowAtIndexPath:indexPath animated:NO]; | |
} | |
} | |
} | |
- (void)rz_smoothlyDeselectItemsInCollectionView:(UICollectionView *)collectionView | |
{ | |
NSArray<NSIndexPath *> *selectedIndexPaths = [collectionView indexPathsForSelectedItems]; | |
if (self.transitionCoordinator) { | |
[self.transitionCoordinator animateAlongsideTransitionInView:self.parentViewController.view animation:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) { | |
for (NSIndexPath *indexPath in selectedIndexPaths) { | |
[collectionView deselectItemAtIndexPath:indexPath animated:context.isAnimated]; | |
} | |
} completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) { | |
if (context.isCancelled) { | |
for (NSIndexPath *indexPath in selectedIndexPaths) { | |
[collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone]; | |
} | |
} | |
}]; | |
} | |
else { | |
for (NSIndexPath *indexPath in selectedIndexPaths) { | |
[collectionView deselectItemAtIndexPath:indexPath animated:NO]; | |
} | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment