UICollectionViewDropDelegate нарушает механику переупорядочения ячеек в UICollectionView - PullRequest
0 голосов
/ 23 июня 2019

Я пытаюсь реализовать два collectionViews с перетаскиванием ячеек между ними. Но я столкнулся со странным поведением с переупорядочением ячеек внутри collectionView. Вот минимальный код, который повторяет это поведение.

Предоставленный код работает, как и ожидалось, но затем я раскомментирую collectionView.dropDelegate = self, он больше не работает. Я пытался найти, какой метод UICollectionViewDropDelegate вызывается, но ни один из них не вызывается.

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDropDelegate {
    func collectionView(_ collectionView: UICollectionView, performDropWith coordinator: UICollectionViewDropCoordinator) {
        print("in drop")
    }

    @IBOutlet weak var collectionView: UICollectionView!
    var items = [UIColor.red, UIColor.green, UIColor.blue]
    override func viewDidLoad() {
        super.viewDidLoad()
//        collectionView.dropDelegate = self
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return items.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
        cell.contentView.layer.backgroundColor = items[indexPath.item].cgColor
        return cell
    }

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

    func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
        let temp = items.remove(at: sourceIndexPath.item)
        items.insert(temp, at: destinationIndexPath.item)
    }

    @IBAction func handleLongGesture(_ gesture: UILongPressGestureRecognizer) {
        switch gesture.state {
        case .began:
            guard let selectedIndexPath = self.collectionView.indexPathForItem(at: gesture.location(in: self.collectionView)) else {
                break
            }
            self.collectionView.beginInteractiveMovementForItem(at: selectedIndexPath)
        case .changed:
            self.collectionView.updateInteractiveMovementTargetPosition(gesture.location(in: gesture.view!))
        case .ended:
            self.collectionView.endInteractiveMovement()
        default:
            self.collectionView.cancelInteractiveMovement()
        }
    }
}

пример без dragDelegate

пример с dragDelegate

Так это происходит, и что я должен сделать, чтобы получить нормальное поведение с dragDelegate?

...