我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()
在未显示上下文菜单时调用,则删除动画按预期工作。