Last active
June 30, 2019 11:59
-
-
Save sukov/2dc741f3368cb3799c82e30ba2630ccb to your computer and use it in GitHub Desktop.
DeselectRowsAlongsideTransition Swift
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 UITableView { | |
func reloadDataAnimated() { | |
UIView.transition(with: self, | |
duration: 0.35, | |
options: .transitionCrossDissolve, | |
animations: { | |
self.reloadData() | |
}) | |
} | |
/** | |
Animates cell deselection alongside transition. Must be called on `viewWillAppear` | |
- Parameters: | |
- viewController: The UIViewController where the table view is located | |
*/ | |
func deselectRowsAlongsideTransition(from viewController: UIViewController) { | |
guard let coordinator = viewController.transitionCoordinator else { | |
indexPathsForSelectedRows?.forEach { deselectRow(at: $0, animated: false) } | |
return | |
} | |
coordinator.animateAlongsideTransition(in: self, | |
animation: { [weak self] context in | |
self?.indexPathsForSelectedRows?.forEach { | |
self?.deselectRow(at: $0, animated: context.isAnimated) | |
} | |
}, completion: { [weak self] context in | |
if context.isCancelled { | |
self?.indexPathsForSelectedRows?.forEach { | |
self?.selectRow(at: $0, animated: false, scrollPosition: .none) | |
} | |
} | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment