Skip to content

Instantly share code, notes, and snippets.

@skagedal
Forked from ZevEisenberg/LICENSE
Last active June 9, 2016 12:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skagedal/0babbfb6caf9d35e9ef31b70e24824d5 to your computer and use it in GitHub Desktop.
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
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)
}
}
}
}
@import UIKit;
@interface UIViewController (RZDeselection)
- (void)rz_smoothlyDeselectRowsInTableView:(UITableView *)tableView;
- (void)rz_smoothlyDeselectItemsInCollectionView:(UICollectionView *)collectionView;
@end
#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