Swift 如何删除';鬼';UICollectionView拖放过程中的单元格,并使移动单元格不透明?

Swift 如何删除';鬼';UICollectionView拖放过程中的单元格,并使移动单元格不透明?,swift,uicollectionview,drag-and-drop,uikit,Swift,Uicollectionview,Drag And Drop,Uikit,我试图在UICollectionView中实现拖放机制,这与在快捷方式应用程序中重新排列快捷方式的组件非常相似 到目前为止,当您开始拖动时,会留下一个单元格的透明视图,而另一个单元格的透明视图会随着手指移动 我想摆脱“幽灵”细胞,使移动细胞完全不透明 以下是我的拖放行为(主要取自) 不确定问题是否仍然存在,但如果是这样: 以下是如何处理单元格拖动状态: 只需通过DispatchQueue.main.async{}将其隐藏起来即可 并使在上再次可见。无状态。对我有用:)但这并不能解释如何隐藏鬼细

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

到目前为止,当您开始拖动时,会留下一个单元格的透明视图,而另一个单元格的透明视图会随着手指移动

我想摆脱“幽灵”细胞,使移动细胞完全不透明

以下是我的拖放行为(主要取自)


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

只需通过DispatchQueue.main.async{}将其隐藏起来即可


并使在上再次可见。无状态。对我有用:)

但这并不能解释如何隐藏鬼细胞。
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)
    }
}