簡體   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