UICollectionView不调用moveItemAt:To:数据源方法以响应endInteractiveMovement()

时间:2017-11-02 21:11:30

标签: ios uicollectionview

我很难将交互式重新排序工作在嵌入式UIViewController中的canMoveItemAt中。没有自定义布局对象。

视图控制器被设置为集合视图的委托和数据源,并且集合视图被赋予了按下herehere概述的长按手势识别器。我还实现了数据源方法moveItemAt:To:和func collectionView(_ collectionView: UICollectionView, canMoveItemAt indexPath: IndexPath) -> Bool { if collectionView == cardCollectionView { return true } else { return false } }

我在视图控制器中有另一个不应重新排序的集合视图,VC是两者的委托/数据源。因此:

UIGestureRecognizerState.ended

我可以确认正在调用此数据源方法。

长按手势的处理程序触发适当的步骤,并且细胞在按下期间开始重新排列行为。除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)") } 数据源方法,但永远不会调用它。

app.locals.getFile = function()
{
    return `
        <%- include("someFile.ejs") %>
        <%- include("someOneFile.ejs") %>
        <%- include("someTwoFile.ejs") %>
        all is well
    `
}

相反,当长按结束时,单元格全部返回到它们的初始位置,我从未有机会实现数据的重新排序。如何完成此重新排序流程需要做什么? 此外,当长按开始时,细胞在重新排序动画中反弹,但我没有看到按下的“提升”细胞。这是一个单独的问题,还是相关的? (代码在 Swift 4.0

1 个答案:

答案 0 :(得分:1)

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

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

        self.cardCollectionView.dropDelegate = self

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