Last active
February 19, 2022 02:04
-
-
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
This file contains 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
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. |
This file contains 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 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 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 |
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
@skagedal fixed, thanks.
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)
}
}
}
@Fawxy thanks! We also maintain an up-to-date version here: https://github.com/Raizlabs/Swiftilities/blob/develop/Pod/Classes/Deselection/UIViewController%2BDeselection.swift
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line 8 in UIViewController+RZDeselection.m has double dots:
self..transitionCoordinator
.