繁体   English   中英

集合视图 beginInteractiveMovementForItem 返回 false

[英]Collection view beginInteractiveMovementForItem returns false

我正在使用集合视图并希望在用户平移时移动单元格。 这是代码。

func longPressPhotoCollectionView(sender: UILongPressGestureRecognizer) {
    guard isEditing else {
        return
    }
    let point = sender.location(in: photoCollectionView)
    switch sender.state {
    case .began:
        guard let indexPath = photoCollectionView.indexPathForItem(at: point) else {
            return
        }
        let isS = photoCollectionView.beginInteractiveMovementForItem(at: indexPath)
        print(isS)
    case .changed:
        photoCollectionView.updateInteractiveMovementTargetPosition(point)
    case .ended:
        photoCollectionView.endInteractiveMovement()
    default:
        photoCollectionView.cancelInteractiveMovement()
    }
}

我怀疑自定义布局会导致这个问题。 我从 Apple 的 Doc 中找到了这个。

当您调用此方法时,集合视图会咨询其委托以确保可以移动项目。 如果数据源不支持项目的移动,则此方法返回 false。

我不知道如何处理动画,尤其是自定义布局。 请帮我! 谢谢!

在您的 UICollectionViewDataSource 中实现以下内容:

func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {

    print("MOVING!")
    // Switch the Items in the DataSource
}

当您调用此方法时,集合视图会咨询其委托以确保可以移动项目。 如果数据源不支持项目的移动,则此方法返回 false。

Apple 文档具有误导性。 这似乎暗示您应该覆盖UICollectionViewDelegate上的某些方法以使单元格可移动。 其实就是一个 UICollectionViewDataSource 方法:

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

https://developer.apple.com/documentation/uikit/uicollectionviewdatasource/1618015-collectionview

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM