【问题标题】:UITableView Destructive Context Menu AnimationUITableView 破坏性上下文菜单动画
【发布时间】:2020-08-10 18:39:23
【问题描述】:

我正在将 iOS13 上下文菜单添加到我的表格视图中。菜单操作之一允许用户删除项目:

override func tableView(_ tableView: UITableView, contextMenuConfigurationForRowAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
    return UIContextMenuConfiguration(identifier: nil, previewProvider: nil) { suggestedActions in
       let deleteAction = UIAction(title: "Delete", image: UIImage(systemName: "trash.fill"), identifier: nil, discoverabilityTitle: "", attributes: UIMenuElement.Attributes.destructive) { action in
            self.data.remove(at: indexPath.row)

            //Remove from the table.
            self.tableView.deleteRows(at: [indexPath], with: .automatic)
        }

        return UIMenu(title: "", children: [deleteAction])
    }
}

我正在使用默认的预览视图控制器(所以它只显示单元格)。我目前看到一个奇怪的动画工件,其中显示上下文菜单预览,而被删除的行下方的项目动画起来,然后预览消失为白色(所以看起来列表中有一个空白行),然后该表重新绘制并显示被掩盖的项目。

这是使用默认单元格,但在使用包含更多信息的自定义单元格时看起来更糟。有没有办法让这个动作的动画效果更好?

【问题讨论】:

    标签: ios swift uikit


    【解决方案1】:

    我也遇到了这个问题。该错误可能是由于用于生成预览的原始单元格被删除、移动或更改所致。

    我找到的解决方案是实现委托方法tableView(_:previewForHighlightingContextMenuWithConfiguration:),将原始单元格作为视图传递给它,但自定义UIPreviewParameters以使用UIColor.clear

        override func tableView(_ tableView: UITableView, previewForHighlightingContextMenuWithConfiguration configuration: UIContextMenuConfiguration) -> UITargetedPreview? {
            guard let indexPath = configuration.identifier as? IndexPath, let cell = tableView.cellForRow(at: indexPath) else {
                return nil
            }
            
            let parameters = UIPreviewParameters()
            parameters.backgroundColor = .clear
            
            return UITargetedPreview(view: cell, parameters: parameters)
        }
    

    为了识别此委托方法中的原始单元格,您需要一种方法来识别它。一种方法是将indexPath 设置为UIContextMenuConfiguration 的标识符,例如:

        override func tableView(_ tableView: UITableView, contextMenuConfigurationForRowAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
            return UIContextMenuConfiguration(identifier: indexPath as NSIndexPath, previewProvider: nil) { _ in
                return UIMenu(title: "", children: [
                    UIAction(title: "Delete", image: UIImage(systemName: "trash"), attributes: .destructive) { action in
                        self.data.remove(at: indexPath.row)
                        tableView.deleteRows(at: [indexPath], with: .automatic)
                    }
                ])
            }
        }
    

    但是,如果您的数据可以在上下文菜单的显示和操作之间发生变化,那么您需要一种更可靠的方法来识别它。

    我不必实现 tableView(_:previewForDismissingContextMenuWithConfiguration:) 即可。

    【讨论】:

    • 不幸的是,这对我在 iOS 14.3 上不起作用。对于两者,previewForHighlightingContextMenuWithConfigurationUIContextMenuConfiguration.previewProvider 与自定义 VC 我得到与 OP 的 GIF 完全相同的行为。 :(
    • 我猜我的错。我不得不实现previewForDismissingContextMenuWithConfiguration,因为我使用的是自定义previewProvider。它现在完美运行!谢谢!
    • 面临同样的问题...我的 collectionView 监听套接字更改,因此它更新非常频繁...我收到奇怪的动画...SOS
    最近更新 更多