iOS 13 上下文菜单:如何通过点击预览来简单地关闭菜单?

iOS 13 context menu: how to simply dismiss menu by tapping on preview?

按照 Kyle Bashour 在这里发布的优秀指南,我能够在 UIViewController 中的 UITableView 上实现新的上下文菜单:

https://kylebashour.com/posts/context-menu-guide

我不想在用户点击单元格的预览时执行任何操作。相反,我只想关闭菜单并 return 到 table 视图。

这是默认行为,但是相应的默认动画非常跳跃(从预览扩展到覆盖整个屏幕),如 运行 上述指南的示例应用所示:

有没有办法实现关闭只是调用上下文菜单的反向动画?

我刚刚设法找到解决方案。

实现 UITableViewDelegate willPerformPreviewActionForMenuWith 函数以将动画师的 preferredCommitStyle 设置为 .dismiss 就可以了。

func tableView(_ tableView: UITableView, willPerformPreviewActionForMenuWith configuration: UIContextMenuConfiguration, animator: UIContextMenuInteractionCommitAnimating) {
    animator.preferredCommitStyle = .dismiss
}