【问题标题】:Weird animation when deleting item from UICollectionView while UIContextMenu is shown在显示 UIContextMenu 时从 UICollectionView 删除项目时出现奇怪的动画
【发布时间】:2020-01-19 15:52:08
【问题描述】:

我正在使用UIContextMenuInteraction 显示UICollectionView 的上下文菜单,如下所示:

func collectiovnView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
    return UIContextMenuConfiguration(identifier: nil, previewProvider: nil, actionProvider: { _ in
        let deleteAction = UIAction(title: "Delete", image: UIImage(systemName: "trash"), attributes: .destructive) { _ in
            self.deleteItem(at: indexPath)
        }
        return UIMenu(title: "Actions", children: [deleteAction])
    })
}

func deleteItem(at indexPath: IndexPath) {
    self.collectionView.performBatchUpdates({
        self.items.remove(at: indexPath.item)
        self.collectionView.deleteItems(at: [indexPath])
    })
}

一切正常,但是当我点击“删除”项目时,会出现一个奇怪的动画,其中删除的项目在其他项目移动时保持在原位,然后它立即消失。有时我什至会在新项目出现前的几分之一秒内看到空白区域或随机项目。

如果我在未显示上下文菜单的情况下调用collectionView.deleteItems(),删除动画将按预期工作。

【问题讨论】:

    标签: ios swift uicollectionview uicontextmenuinteraction


    【解决方案1】:

    看起来奇怪的动画是两个几乎同时运行的动画之间发生冲突的结果:

    1. 删除动画:点击“删除”项时,会调用collectionView.deleteItems(),并以动画方式删除指定的收藏项。
    2. 菜单关闭动画:点击菜单项后,上下文菜单也会关闭,并显示另一个动画,显示被删除的项目只有几分之一秒。

    这看起来像是一个应该由 Apple 修复的错误。但作为一种解决方法,我不得不延迟删除,直到关闭动画完成:

    func deleteItem(at indexPath: IndexPath) {
        let delay = 0.4 // Seconds
        DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
            self.collectionView.performBatchUpdates({
                self.items.remove(at: indexPath.item)
                self.collectionView.deleteItems(at: [indexPath])
            })
        }
    }
    

    0.4 秒是对我有用的最短延迟。

    【讨论】:

    • @robmathers 不幸的是,没有。
    • 0.7 延迟对我来说效果最好……很糟糕,我们必须做出这样的变通办法,但好吧,我猜是下一个
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-09
    • 2023-04-11
    • 2011-09-09
    • 2022-01-24
    相关资源
    最近更新 更多