以下是我在Android版中的示例:
我正在寻找iOS的解决方案。我是iOS的新手,所以我对iOS中的这种UI没有太多了解。
我可以使用FlexLayout构建静态布局。如何实现拖动和订购功能?
那么,有没有图书馆报道此事?感谢。
答案 0 :(得分:1)
在集合视图中重新排序项目
func collectionView(UICollectionView, canMoveItemAt: IndexPath)
询问您的数据源对象是否可以将指定的项目移动到集合视图中的其他位置。
func collectionView(UICollectionView, moveItemAt: IndexPath, to: IndexPath)
告诉您的数据源对象将指定的项目移动到新位置。
https://developer.apple.com/documentation/uikit/uicollectionviewdatasource
示例代码:
<强> UICollectionViewDelegate 强>
我们将从 UICollectionViewDelegate 协议中使用两个委托方法开始。
通过调用UICollectionView
委托方法,允许在canMoveItemAt
中使用移动项目。传递true将启用此功能。
func collectionView(_ collectionView: UICollectionView, canMoveItemAt indexPath: IndexPath) -> Bool {
return true
}
接下来,我们将实现moveItemAt
委托方法,您将拦截正在切换位置的两个项目的起始索引和结束索引。
func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
print(“Starting Index: \(sourceIndexPath.item)”)
print(“Ending Index: \(destinationIndexPath.item)”)
}
为了更好地控制UICollectionView中的手势,我们将实现一个UILongPressGestureRecognizer。
fileprivate var longPressGesture: UILongPressGestureRecognizer!
override func viewDidLoad() {
super.viewDidLoad()
longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(self.handleLongGesture(gesture:)))
reorderCollectionView.addGestureRecognizer(longPressGesture)
}
@objc func handleLongGesture(gesture: UILongPressGestureRecognizer) {
switch(gesture.state) {
case .began:
guard let selectedIndexPath = reorderCollectionView.indexPathForItem(at: gesture.location(in: reorderCollectionView)) else {
break
}
reorderCollectionView.beginInteractiveMovementForItem(at: selectedIndexPath)
case .changed:
reorderCollectionView.updateInteractiveMovementTargetPosition(gesture.location(in: gesture.view!))
case .ended:
reorderCollectionView.endInteractiveMovement()
default:
reorderCollectionView.cancelInteractiveMovement()
}
}
请注意,这些交互式功能适用于iOS 9及更高版本。
来源链接:https://theappspace.com/swift-reorder-cells-uicollectionview-using-drag-drop/