Skip to content

Instantly share code, notes, and snippets.

@ZevEisenberg
Last active February 19, 2022 02:04
Show Gist options
  • Star 29 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save ZevEisenberg/8951478e8c6907bd5e5a to your computer and use it in GitHub Desktop.
Save ZevEisenberg/8951478e8c6907bd5e5a 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
The MIT License (MIT)
Copyright (c) 2016 Zev Eisenberg
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.
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
@ZevEisenberg
Copy link
Author

Thanks to @bsmith11 for helping me figure this out.

@ZevEisenberg
Copy link
Author

I guess I should mention how to use these: you call them in [UIViewController viewWillAppear:].

@skagedal
Copy link

skagedal commented Jun 9, 2016

Line 8 in UIViewController+RZDeselection.m has double dots: self..transitionCoordinator.

@skagedal
Copy link

skagedal commented Jun 9, 2016

Thought I'd make a pull request but apparently you can't do that for gists. Anyway, here's a fixed repo. https://gist.github.com/skagedal/0babbfb6caf9d35e9ef31b70e24824d5

@ZevEisenberg
Copy link
Author

@skagedal fixed, thanks.

@Fawxy
Copy link

Fawxy commented Apr 11, 2017

Swift 3-ified:

func rz_smoothlyDeselectRowsIn(table tableView: UITableView?) {
    // Get the initially selected index paths, if any
    let selectedIndexPaths = tableView?.indexPathsForSelectedRows ?? []

    // Grab the transition coordinator responsible for the current transition
    if let coordinator = transitionCoordinator {
        coordinator.animate(alongsideTransition: { context in
            // Deselect the cells, with animations enabled if this is an animated transition
            selectedIndexPaths.forEach {
                tableView?.deselectRow(at: $0, animated: context.isAnimated)
            }
        }, completion: { context in
            // If the transition was cancel, reselect the rows that were selected before,
            // so they are still selected the next time the same animation is triggered
            if context.isCancelled {
                selectedIndexPaths.forEach {
                    tableView?.selectRow(at: $0, animated: false, scrollPosition: .none)
                }
            }
        })
    } else { // If this isn't a transition coordinator, just deselect the rows without animating
        selectedIndexPaths.forEach {
            tableView?.deselectRow(at: $0, animated: false)
        }
    }
}

@ZevEisenberg
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment