【问题标题】:UICollectionView support for drag/drop reordering AND context menusUICollectionView 支持拖放重新排序和上下文菜单
【发布时间】:2021-03-19 20:20:24
【问题描述】:

我正在尝试使用实例方法 moveItemAtcontextMenuConfigurationForItemAt 在 UICollectionView 中实现重新排序 AND 上下文菜单。两种解决方案都可以自己正常工作,但是当我尝试在 UICollectionView 中实现它们时,上下文菜单优先,我无法使用拖放重新排序。

有没有办法在同一个集合视图中使用这两种方法? 或者,是否有人熟悉使用其他解决方案支持这些功能的方法?我目前正在考虑将 Drag/Drop 委托作为一种解决方法。

下面是我正在使用的代码示例。本项目面向 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
    }
}

【问题讨论】:

  • 我也对此感兴趣。有消息吗?

标签: ios uicollectionview uikit


【解决方案1】:

将您对 moveItemAt API 的使用替换为新的拖放 API,您将免费获得此行为!

【讨论】:

  • 我已经按照你说的做了——使用 UICollectionViewDrag/DropDelegate,但不幸的是上下文菜单仍然优先,即使我移动了我的手指,我也无法进入拖动会话。有什么想法吗?
猜你喜欢
  • 2016-05-27
  • 1970-01-01
  • 2020-02-22
  • 2019-11-26
  • 2014-01-18
  • 1970-01-01
  • 2012-06-19
  • 2016-12-29
  • 2011-01-08
相关资源
最近更新 更多