[英]UICollectionView support for drag/drop reordering AND context menus
I'm trying to implement reordering AND context menus in a UICollectionView using the instance methods moveItemAt
and contextMenuConfigurationForItemAt
.我正在尝试使用实例方法
moveItemAt
和contextMenuConfigurationForItemAt
在 UICollectionView 中实现重新排序和上下文菜单。 Both solutions work fine on their own but when I try to implement both of them in the UICollectionView the contextual menus take precedence and I'm not able to use drag and drop reordering.两种解决方案都可以自己正常工作,但是当我尝试在 UICollectionView 中实现它们时,上下文菜单优先,我无法使用拖放重新排序。
Is there a way to use both of these methods in the same collection view?有没有办法在同一个集合视图中使用这两种方法? Alternatively, is anyone familiar with a way to support these features using another solution?
或者,是否有人熟悉使用其他解决方案支持这些功能的方法? I'm currently looking into implementing Drag/Drop delegate as a workaround.
我目前正在考虑实施拖放委托作为一种解决方法。
Below is an example of the code I'm using.下面是我正在使用的代码示例。 This project is targeting iOS14.
该项目针对 iOS14。
extension myViewController {
override func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
let temp = items.remove(at: sourceIndexPath.item)
items.insert(temp, at: destinationIndexPath.item)
}
override func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
let index = indexPath.row
let config = UIContextMenuConfiguration(
identifier: nil,
previewProvider: nil){ [self] _ in
return UIMenu(
title: "",
image: nil,
identifier: nil,
children: [deleteAction(index: index),editAction(index: index)])
}
return config
}
}
Replace your use of the moveItemAt API with the new drag and drop APIs, and you'll get this behaviour for free!将您对 moveItemAt API 的使用替换为新的拖放 API,您将免费获得此行为!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.