gpt4 book ai didi

ios - 如何禁用 UICollectionView 部分之间的单元格拖动?

转载 作者:行者123 更新时间:2023-12-04 16:09:54 40 4
gpt4 key购买 nike

我的 UICollectionView 中有两个部分。我希望能够在每个部分内拖动单元格,但不能在它们之间拖动。

我正在使用长按手势识别器来制作单元格拖动运动的动画,这样我就可以检查放置位置是否不在不同的部分。有没有办法确定一个部分的框架?

或者有更简单的方法吗?

最佳答案

UICollectionViewDropDelegate 中,此协议(protocol) func collectionView(_ collectionView: UICollectionView, dropSessionDidUpdate session: UIDropSession, withDestinationIndexPath destinationIndexPath: IndexPath?) -> UICollectionViewDropProposal 可以提供帮助。

检查下面的示例,了解我如何防止将项目从一个部分拖到另一个部分:

UICollectionViewDragDelegate 中,我们使用 itemsForBeginning 函数来传递有关对象的信息。你可以看到,我将索引和项目传递给 localObject

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

UICollectionViewDropDelegate 中,我这样做了:

func collectionView(_ collectionView: UICollectionView, dropSessionDidUpdate session: UIDropSession, withDestinationIndexPath destinationIndexPath: IndexPath?) -> UICollectionViewDropProposal {
if let object = session.items.first?.localObject as? (Item, IndexPath), object.0.status, let destinationIndexPath = destinationIndexPath, object.1.section == destinationIndexPath.section {
let itemAtDestination = sectionViewModel[destinationIndexPath.section].items[destinationIndexPath.row]
if itemAtDestination.status {
return UICollectionViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
}
}
return UICollectionViewDropProposal(operation: .forbidden)
}

根据 Apple :

While the user is dragging content, the collection view calls this method repeatedly to determine how you would handle the drop if it occurred at the specified location. The collection view provides visual feedback to the user based on your proposal.In your implementation of this method, create a UICollectionViewDropProposal object and use it to convey your intentions. Because this method is called repeatedly while the user drags over the table view, your implementation should return as quickly as possible.

我做了什么:我有几个限制要涵盖:

  • 防止 item.status == true 转到同一部分中的项目
  • 防止项目转到其他部分

动图 Drag Sample

关于ios - 如何禁用 UICollectionView 部分之间的单元格拖动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44318401/

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