如何解决处理两个 CollectionviewCell 之间的拖放
如何处理两个集合视图单元格之间的拖放。我们还有单元格重新排序功能(已实现)。
我们真正需要的是:将单元格的子视图(UILabel)拖放到另一个单元格
解决方法
您可以尝试以下操作:
- 向单元格标签添加长按手势。
- 点击开始,您可以隐藏源标签并创建标签的快照。
- 然后将其拖到目标单元格,从接触点的位置获取索引路径。
- 结束手势后,将标签文本添加到目标单元格标签,然后移除快照。
我认为这会奏效。我以前没有尝试过,但我认为这会奏效。
,第 1 步:首先您需要设置拖放委托,如下所示
collectionView1.dragDelegate = self
collectionView2.dropDelegate = self
第 2 步:您需要在 itemsForBeginning 委托方法中添加要拖动的项目
func collectionView(_ collectionView: UICollectionView,itemsForBeginning session: UIDragSession,at indexPath: IndexPath) -> [UIDragItem]
{
let item = imageArray[indexPath.row]
let itemProvider = NSItemProvider(object: item as NSString) //Here For your use case you can add label for drag
let dragItem = UIDragItem(itemProvider: itemProvider)
dragItem.localObject = item
return [dragItem]
}
第 3 步:您需要添加 performDropWith 委托方法来删除 collectionView2 的项目
func collectionView(_ collectionView: UICollectionView,performDropWith coordinator: UICollectionViewDropCoordinator)
{
let destinationIndexPath : IndexPath
if let indexPath = coordinator.destinationIndexPath
{
destinationIndexPath = indexPath
}
else
{
let row = collectionView.numberOfItems(inSection: 0)
destinationIndexPath = IndexPath(item: row - 1,section: 0)
}
if coordinator.proposal.operation == .copy
{
//Here you can add your copy code
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。