![](/img/trans.png)
[英]UICollectionView with SwiftUI + Drag and drop reordering possible?
[英]UICollectionView support for drag/drop reordering AND context menus
我正在尝试使用实例方法moveItemAt
和contextMenuConfigurationForItemAt
在 UICollectionView 中实现重新排序和上下文菜单。 两种解决方案都可以自己正常工作,但是当我尝试在 UICollectionView 中实现它们时,上下文菜单优先,我无法使用拖放重新排序。
有没有办法在同一个集合视图中使用这两种方法? 或者,是否有人熟悉使用其他解决方案支持这些功能的方法? 我目前正在考虑实施拖放委托作为一种解决方法。
下面是我正在使用的代码示例。 该项目针对 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
}
}
将您对 moveItemAt API 的使用替换为新的拖放 API,您将免费获得此行为!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.