【问题标题】:Is there a method to check the UICollectionView item drag cancellation if the item wasn't moved?如果项目未移动,是否有方法检查 UICollectionView 项目拖动取消?
【发布时间】:2018-09-15 19:46:27
【问题描述】:

我使用拖放来重新排列collectionView。当我开始拖动时,我更改了 collectionView 可视化。要将它改回来,我需要一个在任何情况下都会执行的方法。现在,如果我立即开始拖动并释放触摸,则不会执行以下方法:

not this:
    func collectionView(_ collectionView: UICollectionView,  dragSessionDidEnd session: UIDragSession)

not this:
    func collectionView(_ collectionView: UICollectionView, dropSessionDidEnd session: UIDropSession)

not this:
    func collectionView(_ collectionView: UICollectionView, performDropWith coordinator: UICollectionViewDropCoordinator)

谢谢。

【问题讨论】:

  • 从 Apple 文档看来,dragSessionDidEnd 将在发生放置或会话中止时被调用 developer.apple.com/documentation/uikit/…
  • 哈。我刚刚意识到它不能结束,因为它只能在移动时开始——所以,如果你刚刚选择了一个项目,甚至它的预览已经生成,这并不意味着你开始了拖动。因此,问题是在获得预览之前我无法获得更新的单元格状态。

标签: swift drag-and-drop uicollectionview


【解决方案1】:

如果您使用标准 UICollectionViewDragDelegate 实现,您可以使用单元格类本身的func dragStateDidChange(_ dragState: UICollectionViewCellDragState)对单元格状态更改做出反应,例如:

override func dragStateDidChange(_ dragState: UICollectionViewCellDragState) {

    switch dragState {

    case .none:
        //
    case .lifting:
        //
    case .dragging:
        //
    }
}

【讨论】:

  • UICollectionView 上是否有类似的方法,当它的单元格被提升时,collectionView 可以响应?或者我们应该自己将这个事件从单元格传播到它的collectionView?
  • UICollectionView 符合UICollectionViewDragDelegate 协议时,您可以使用func collectionView(UICollectionView, dragSessionWillBegin: UIDragSession) 获取有关集合视图的拖动会话即将开始的通知。
  • func collectionView(UICollectionView, dragSessionWillBegin: UIDragSession) 不会因为“解除”状态而被调用
【解决方案2】:

从 WWDC“Introducing Drag and Drop”视频看来,处理电梯及其取消的正确位置是 dragInteraction(_:willAnimateLiftWith:session:) 方法。

【讨论】:

  • 这不适用于UICollectionViewDragDelegate
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-04
  • 2011-07-05
  • 2011-10-07
  • 2014-07-04
  • 2011-01-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多