【问题标题】:How to add a slight delay before triggering didSelectRowAt action and deselecting the tabelview cell如何在触发 didSelectRowAt 操作和取消选择 tableview 单元格之前添加一点延迟
【发布时间】:2021-02-16 16:08:33
【问题描述】:

编辑: 我没有意识到是动画(在故事板中的 segue 中)和 pushViewController() 中的动画完成了这项工作。

当我们点击表格视图中的一行时,如何平滑过渡到下一个视图。我想实现与苹果的设置应用类似的平滑过渡效果。现在,只要触摸行,我的转换就会立即发生。

在 Apple 的设置应用程序中,当您点击设置菜单上的一行时,它首先会突出显示该行,然后平滑过渡到下一个视图。这就是我想要达到的目标。

我已搜索但找不到任何相关的解决方案。有什么帮助吗?

以下是我的行选择代码

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = tableView.cellForRow(at: indexPath)
    tableView.deselectRow(at: indexPath, animated: true)
    
    //.....
    self.navigationController?.pushViewController(destinationVC, animated: false)
}

【问题讨论】:

  • 你看过UINavigationController?pushViewController(_ :animated:)吗?
  • 您需要发布用于执行转换的代码。
  • 我已经用相关代码更新了我的问题。
  • @EmilioPelaez 我相信他想要UINavigationController 提供的动画。
  • @ParthTamane。好的,是 UINavigationController 提供了这个动画。如果我们不使用 UINavigationController 会怎样?在这种情况下也能达到同样的效果吗?

标签: ios swift uitableview uitableviewrowaction


【解决方案1】:

您需要更改演示样式。所以点击故事板中的segue。然后从右侧Attributes Inspector 的下拉列表中选择Kind 作为Push

另外,要以编程方式呈现它,您需要在Attributes Inspector 的第一个文本字段中设置一个标识符。然后在你的tableView(_:didSelectRowAt:) 中添加这个。

performSegue(withIdentifier: "SegueIdentifier", sender: nil)

如果您想要延迟操作,则可以使用asyncAfter,例如suggested here

DispatchQueue.main.asyncAfter(deadline: .now() + 0.33) {
   self.deselectRow(at: indexPath, animated: true)
   self.performSegue(withIdentifier: "SegueIdentifier", sender: nil)
}

【讨论】:

  • 只是想补充一点,动画也必须被选中。具有这种效果的是动画属性。我没有意识到。感谢您的帮助。
  • NP! :) “只是想添加动画也必须被选中”这是属性检查器中的另一个属性吗?
  • 另外,如果这对你有用,你能接受答案吗?谢谢:)
  • 它确实有效,但是它没有显示与设置应用程序相同的动画效果。设置应用程序在转换到下一个视图之前有一点延迟。你知道如何实现与“设置”应用相同的延迟水平吗?
  • 是的!我认为您需要手动启动segue。让我查一下。
猜你喜欢
  • 1970-01-01
  • 2011-11-28
  • 1970-01-01
  • 1970-01-01
  • 2020-07-04
  • 1970-01-01
  • 2019-03-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多