gpt4 book ai didi

ios - UICollectionViewDropDelegate 破坏了 UICollectionView 中的重新排序单元机制

转载 作者:行者123 更新时间:2023-11-29 05:39:14 27 4
gpt4 key购买 nike

我正在尝试实现两个 collectionView,并在它们之间拖放单元格。但我在对 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()
}
}
}

example without dragDelegate

example with dragDelegate

所以这种情况发生了,我应该怎么做才能使用 dragDelegate 获得正常行为?

最佳答案

这有点难看,但我遇到了这个问题,我能找到的最简单的方法就是在调用手势识别器时禁用 dropDelegate。

@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.dropDelegate = nil // Drop delegate behaves VERY badly with beginInteractiveMovementForItem
self.collectionView.beginInteractiveMovementForItem(at: selectedIndexPath)

case .changed:
self.collectionView.updateInteractiveMovementTargetPosition(gesture.location(in: gesture.view!))
case .ended:
self.collectionView.endInteractiveMovement()
self.collectionView.dropDelegate = self
default:
self.collectionView.cancelInteractiveMovement()
self.collectionView.dropDelegate = self
}
}

关于ios - UICollectionViewDropDelegate 破坏了 UICollectionView 中的重新排序单元机制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56725610/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com