gpt4 book ai didi

ios - 使用向后滑动手势以交互方式取消选择选定的单元格

转载 作者:技术小花猫 更新时间:2023-10-29 11:01:05 34 4
gpt4 key购买 nike

在“设置”等应用中,当您点击一个插入屏幕的单元格,然后从屏幕左侧向后滑动时,您可以看到取消选择所选单元格的背景颜色逐渐消失,并且它是完全交互的 - 如果您滑动一半,然后向后滑动,所选背景 View 将恢复为完全不透明。

在我的应用程序中,我没有更改任何默认行为,当我从左侧滑动返回时,所选单元格背景颜色保持完全不透明,直到滑动手势完成,然后它迅速淡化为取消选择它。

如何通过滑动返回手势实现单元格的交互式取消选择?

最佳答案

要在 iOS 11 和更高版本中启用交互式取消选择,您可以使用 UITableViewController 因为它会为您实现它,或者您可以通过在过渡协调器旁边设置取消选择动画来实现它,如下所示:

- (void)viewWillAppear:(BOOL)animated { 
[super viewWillAppear:animated];

NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
if (selectedIndexPath) {
if (self.transitionCoordinator) {
[self.transitionCoordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
[self.tableView deselectRowAtIndexPath:selectedIndexPath animated:YES];
} completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
if (context.cancelled) {
[self.tableView selectRowAtIndexPath:selectedIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
}];
} else {
[self.tableView deselectRowAtIndexPath:selectedIndexPath animated:animated];
}
}
}

在 Swift 中:

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

if let selectedIndexPath = tableView.indexPathForSelectedRow {
if let coordinator = transitionCoordinator {
coordinator.animate(alongsideTransition: { context in
self.tableView.deselectRow(at: selectedIndexPath, animated: true)
}) { context in
if context.isCancelled {
self.tableView.selectRow(at: selectedIndexPath, animated: false, scrollPosition: .none)
}
}
} else {
self.tableView.deselectRow(at: selectedIndexPath, animated: animated)
}
}
}

类似的实现适用于 UICollectionView:

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

if let indexPath = collectionView.indexPathsForSelectedItems?.first {
if let coordinator = transitionCoordinator {
coordinator.animate(alongsideTransition: { _ in
self.collectionView.deselectItem(at: indexPath, animated: true)
}, completion: { context in
if context.isCancelled {
self.collectionView.selectItem(at: indexPath, animated: false, scrollPosition: [])
}
})
} else {
collectionView.deselectItem(at: indexPath, animated: animated)
}
}
}

关于ios - 使用向后滑动手势以交互方式取消选择选定的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23894319/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com