首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在UICollectionView拖放过程中移除“重影”单元格,并使移动的单元格不透明?

如何在UICollectionView拖放过程中移除“重影”单元格,并使移动的单元格不透明?
EN

Stack Overflow用户
提问于 2019-05-08 03:16:46
回答 1查看 1.2K关注 0票数 6

我试图在我的UICollectionView中实现拖放机制,非常类似于在快捷方式应用程序中对快捷方式的组件进行重新排序。

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

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

下面是我的拖放行为(大部分摘自this post)

代码语言:javascript
复制
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)
    }
}

EN

回答 1

Stack Overflow用户

发布于 2019-09-26 22:59:56

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

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

只需隐藏拖动,通过DispatchQueue.main.async {}

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56029160

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档