简体   繁体   English

集合视图 beginInteractiveMovementForItem 返回 false

[英]Collection view beginInteractiveMovementForItem returns false

I'm using collection view and want to move cells when user paned.我正在使用集合视图并希望在用户平移时移动单元格。 This is the code.这是代码。

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()
    }
}

I doubt custom layout will cause this problem.我怀疑自定义布局会导致这个问题。 And I found this from Apple's Doc.我从 Apple 的 Doc 中找到了这个。

When you call this method, the collection view consults its delegate to make sure the item can be moved.当您调用此方法时,集合视图会咨询其委托以确保可以移动项目。 If the data source does not support the movement of the item, this method returns false.如果数据源不支持项目的移动,则此方法返回 false。

I don't know how to handle animations, especially with custom layout.我不知道如何处理动画,尤其是自定义布局。 Please help me!请帮我! Thanks!谢谢!

Implement the following in your UICollectionViewDataSource:在您的 UICollectionViewDataSource 中实现以下内容:

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

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

When you call this method, the collection view consults its delegate to make sure the item can be moved.当您调用此方法时,集合视图会咨询其委托以确保可以移动项目。 If the data source does not support the movement of the item, this method returns false.如果数据源不支持项目的移动,则此方法返回 false。

The Apple documentation is misleading. Apple 文档具有误导性。 It seems to imply that you should override some method on UICollectionViewDelegate to make a cell movable.这似乎暗示您应该覆盖UICollectionViewDelegate上的某些方法以使单元格可移动。 In fact, it's a UICollectionViewDataSource method:其实就是一个 UICollectionViewDataSource 方法:

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

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

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

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