[英]UICollectionView: drag/move one cell onto another (no reordering)
In swift I would like to drag one cell inside the UICollectionView onto another cell.在 swift 中,我想将 UICollectionView 内的一个单元格拖到另一个单元格上。 I found several, good tutorials for reordering, but I would like to move only the touched element, other cells shall keep their positions.
我找到了几个很好的重新排序教程,但我只想移动触摸的元素,其他单元格应保持其位置。 Behavior would be same as on home screen to create folders by dragging one item onto another.
通过将一个项目拖到另一个项目上来创建文件夹的行为与在主屏幕上的行为相同。
Already setup view controller with UICollectionView and following events:已设置视图 controller 与 UICollectionView 和以下事件:
func collectionNode(_ collectionNode: ASCollectionNode, canMoveItemWith node: ASCellNode) -> Bool {}
func collectionNode(_ collectionNode: ASCollectionNode, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {}
@objc func handleLongGesture(gesture: UILongPressGestureRecognizer) {}
Finally figured out:终于想通了:
self.collectionView.view.updateInteractiveMovementTargetPosition(gesture.location(in: gesture.view!))
will update the view with reordering the items.将通过重新排序项目来更新视图。 Is there any alternative/option to keep the position of the other items?
是否有任何替代/选项可以保留其他项目的 position?
Okay, using wrong API functions.好的,使用了错误的 API 函数。 If you have a similar problem, please follow the tutorial of Ray: https://www.raywenderlich.com/3121851-drag-and-drop-tutorial-for-ios
如果你有类似的问题,请参考Ray的教程: https://www.raywenderlich.com/3121851-drag-and-drop-tutorial-for-ios
This is quite self explaining although it has its complexity.尽管它有其复杂性,但这是非常自我解释的。
Bottom line: the above discussed API functions only provide re-ordering of items, to put items onto each other, this is a drag'n drop instead, requiring nothing more than implementing three functions:底线:上面讨论的 API 函数仅提供项目的重新排序,将项目相互放置,这是一个拖放操作,只需要实现三个函数:
func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {}
func collectionView(_ collectionView: UICollectionView, performDropWith coordinator: UICollectionViewDropCoordinator) {}
func collectionView(_ collectionView: UICollectionView, dropSessionDidUpdate session: UIDropSession, withDestinationIndexPath destinationIndexPath: IndexPath?) -> UICollectionViewDropProposal {}
Make sure to use UICollectionViewDropProposal(operation: .move, intent: .insertIntoDestinationIndexPath) in the last function as return to allow moving above an object, otherwise Swift UI will again change position of existing items. Make sure to use UICollectionViewDropProposal(operation: .move, intent: .insertIntoDestinationIndexPath) in the last function as return to allow moving above an object, otherwise Swift UI will again change position of existing items.
Furthermore, make sure to enable dragging: view.dragInteractionEnabled = true此外,确保启用拖动:view.dragInteractionEnabled = true
view is then your collection view. view 是你的收藏视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.