gpt4 book ai didi

swift - 如何在 UICollectionView 拖/放过程中删除 'ghost' 单元格,并使移动单元格不透明?

转载 作者:搜寻专家 更新时间:2023-11-01 05:32:08 35 4
gpt4 key购买 nike

我正在尝试在我的 UICollectionView 中实现拖放机制,这与在快捷方式应用程序中重新排序快捷方式的组件非常相似。

截至目前,行为是当您开始拖动时,会留下一个透明的单元格 View ,而另一个透明的单元格 View 会随手指移动。

我想摆脱“幽灵”单元格,并使移动的单元格完全不透明。

这是我的拖放行为(主要取自 this post )

func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
let item = itemData[indexPath.item]
let itemProvider = NSItemProvider(object: item.title as NSString)
let dragItem = UIDragItem(itemProvider: itemProvider)
dragItem.localObject = item


return [dragItem]
}

func collectionView(_ collectionView: UICollectionView, dropSessionDidUpdate session: UIDropSession, withDestinationIndexPath destinationIndexPath: IndexPath?) -> UICollectionViewDropProposal {

if collectionView.hasActiveDrag {
return UICollectionViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
}

return UICollectionViewDropProposal(operation: .forbidden)
}

func collectionView(_ collectionView: UICollectionView, performDropWith coordinator: UICollectionViewDropCoordinator) {

var destinationIndexPath: IndexPath
if let indexPath = coordinator.destinationIndexPath {
destinationIndexPath = indexPath
}
else {
let row = collectionView.numberOfItems(inSection: 0)
destinationIndexPath = IndexPath(row: row - 1, section: 0)
}

reorderItems(coordinator: coordinator, destinationIndexPath: destinationIndexPath, collectionView: collectionView)

}

fileprivate func reorderItems(coordinator: UICollectionViewDropCoordinator,
destinationIndexPath: IndexPath,
collectionView: UICollectionView) {

if let item = coordinator.items.first,
let sourceIndexPath = item.sourceIndexPath {

collectionView.performBatchUpdates({
self.itemData.remove(at: sourceIndexPath.item)
self.itemData.insert(item.dragItem.localObject as! PlanItem, at: destinationIndexPath.item)

collectionView.deleteItems(at: [sourceIndexPath])
collectionView.insertItems(at: [destinationIndexPath])
}, completion: nil)

coordinator.drop(item.dragItem, toItemAt: destinationIndexPath)
}
}

Here's an image describing what I want

最佳答案

不确定问题是否仍然存在,但如果是:以下是处理单元格拖动状态的方法:

Is there a method to check the UICollectionView item drag cancellation if the item wasn't moved?

只需通过 DispatchQueue.main.async 在拖动时隐藏它{}

并在 .none 状态下再次可见。对我有用:)

关于swift - 如何在 UICollectionView 拖/放过程中删除 'ghost' 单元格,并使移动单元格不透明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56029160/

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