简体   繁体   English

UICollectionView:将一个单元格拖动/移动到另一个单元格(无重新排序)

[英]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.

相关问题 UITableView 将一个单元格拖放到另一个单元格上? - UITableView drag and drop a cell onto another cell? 如何将UICollectionViewCell从一个UICollectionView拖到另一个UICollectionView? - How can I drag a UICollectionViewCell from one UICollectionView to another UICollectionView? UICollectionView 和 SwiftUI + 可以拖放重新排序吗? - UICollectionView with SwiftUI + Drag and drop reordering possible? UICollectionView 支持拖放重新排序和上下文菜单 - UICollectionView support for drag/drop reordering AND context menus UICollectionView 重新排序:交互式移动还是拖放? - UICollectionView reordering: interactive movement or drag and drop? UITableView:通过拖放将单元格移动到另一个位置 - UITableView: move cell to a position to another with drag & drop 如何将集合视图单元格从1拖放到另一个uicollectionview - How to drag and drop collection view cell from 1 to another uicollectionview UICollectionViewDropDelegate 打破了 UICollectionView 中单元格的重新排序机制 - UICollectionViewDropDelegate breaks reordering cell mechanics in UICollectionView UICollectionView移动特定单元格 - UICollectionView move specific cell UITableView单元重新排序拖动操作意外停止 - UITableView cell reordering drag action unexpectedly stops
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM