ios - UICollectionView 未调用 moveItemAt :To: Data Source method in response to endInteractiveMovement()

标签 ios uicollectionview

我很难在子类 UIViewController 中嵌入的 UICollectionView 中进行交互式重新排序。没有自定义布局对象。

View Controller 被设置为 Collection View 的委托(delegate)和数据源,并且 Collection View 被赋予长按手势识别器,如概述herehere 。我还实现了数据源方法 canMoveItemAt: 和 moveItemAt:To:

我在 View Controller 中有另一个 Collection View ,它不应该重新排序,并且 VC 是两者的委托(delegate)/数据源。因此:

func collectionView(_ collectionView: UICollectionView, canMoveItemAt indexPath: IndexPath) -> Bool {

    if collectionView == cardCollectionView {

        return true
    }
    else {
        return false
    }
}

我可以确认这个数据源方法被正确调用。

长按手势的处理程序会触发适当的步骤,并且单元格在按下期间开始重新排列行为。除了对 UIGestureRecognizerState.ished: 的响应,它会触发 cardCollectionView.endInteractiveMovement()。这应该会导致 moveItemAt:To 数据源方法,但它永远不会被调用。

@objc func handleLongGesture(gesture: UILongPressGestureRecognizer) {

        //print(cardCollectionView.delegate)
        switch(gesture.state) {

        case UIGestureRecognizerState.began:
        guard let selectedIndexPath = self.cardCollectionView.indexPathForItem(at: gesture.location(in: self.cardCollectionView)) else {
        break
        }
        cardCollectionView.beginInteractiveMovementForItem(at: selectedIndexPath)
        case UIGestureRecognizerState.changed:
        cardCollectionView.updateInteractiveMovementTargetPosition(gesture.location(in: gesture.view!))
        case UIGestureRecognizerState.ended:
            print("ending")
        cardCollectionView.endInteractiveMovement()
        default:
            print("canceling")
        cardCollectionView.cancelInteractiveMovement()
        }
}  


func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
    print("Starting Index: \(sourceIndexPath.item)")
}

相反,当长按结束时,单元格全部返回到其初始位置,并且我永远没有机会影响数据的重新排序。我需要做什么才能完成此重新排序过程? 另外,当长按开始时,单元格在重新排序动画中反弹,但我没有看到按下时“抬起”的单元格。这是一个单独的问题,还是相关的? (代码位于 Swift 4.0 中)

最佳答案

UICollectionViewDropDelegate 分配给 UICollectionView 会通过长按手势处理程序方法干扰交互式重新排序(iOS 9 样式)。

拆掉 UIViewController 并一次重建一行后,我隔离了交互式重新排序失败的地方。罪魁祸首是 viewDidLoad 中的那个(由于其他原因)我将 ViewController 指定为 CollectionView 的 dropDelegate:

        self.cardCollectionView.dropDelegate = self

解决方法是通过新的 iOS 11 拖放 API 简单地实现重新排序( WWDC 2017 session 223 ,大约 32:00)

关于ios - UICollectionView 未调用 moveItemAt :To: Data Source method in response to endInteractiveMovement(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47084733/

相关文章:

ios - 获取 UITapGestureRecognizer 目标

ios - MKMapRect 和显示跨越第 180 个子午线的 map 叠加层

ios - 更改rootViewController时从正确的动画滑动

ios - 错误 : Static member copyWithZone cannot be used on instance of type UIImageView

ios - UICollectionView 中的单元格重叠

ios - UICollectionView | UICollectionView |电池可重复使用

ios - 仍未针对 iPhone 6 和 iPhone 6 Plus 进行优化

ios - Swift inout 没有设置

ios - JSQMessagesView 动画行高变化的问题

ios - 为什么我收到错误 "cannot assign value of type (class) to type UICollectionViewDelegate, UICollectionViewDataSource?"