Es gibt nur wenige einfache Möglichkeiten für UICollectionViewController, die leicht von canMoveItemAt Delegatmethode gehandhabt werden kann wenn Sie einen view-Controller haben Sie folgende Art und Weise versuchen:
import UIKit
class LoveProfileEditViewViewController: BaseViewController {
fileprivate var longPressGesture:UILongPressGestureRecognizer!
@IBOutlet var theCollectionView:UICollectionView!
public var items:[String]!
override func viewDidLoad() {
super.viewDidLoad()
let horizontalLayout = UICollectionViewFlowLayout()
horizontalLayout.scrollDirection = .horizontal
self.theCollectionView.collectionViewLayout = horizontalLayout
self.theCollectionView.register(UINib(nibName: "SomeNibName", bundle: nil), forCellWithReuseIdentifier: "Some Identifier")
self.longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(self.handleLongGesture(gesture:)))
self.theCollectionView.addGestureRecognizer(longPressGesture)
}
fileprivate func moveDataItem(_ sIndex: Int, _ dIndex: Int) {
let item = self.items.remove(at: sIndex)
self.items.insert(item, at:dIndex)
self.theCollectionView.reloadData()
}
@objc private func handleLongGesture(gesture: UILongPressGestureRecognizer) {
switch(gesture.state) {
case UIGestureRecognizerState.began:
guard let selectedIndexPath = self.theCollectionView.indexPathForItem(at: gesture.location(in: self.theCollectionView)) else {
break
}
self.theCollectionView.beginInteractiveMovementForItem(at: selectedIndexPath)
case UIGestureRecognizerState.changed:
guard let selectedIndexPath = self.theCollectionView.indexPathForItem(at: gesture.location(in: self.theCollectionView)) else {
break
}
self.theCollectionView?.updateInteractiveMovementTargetPosition(gesture.location(in: gesture.view!))
case UIGestureRecognizerState.ended:
self.theCollectionView.endInteractiveMovement()
default:
self.theCollectionView.cancelInteractiveMovement()
}
}
}
// MARK: - UICollectionViewDelegateFlowLayout
extension LoveProfileEditViewViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 88.0, height: collectionView.bounds.size.height)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsetsMake(0.0, 5.0, 0.0, 5.0)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 10.0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 10.0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return .zero
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
return .zero
}
}
// MARK: - UICollectionViewDataSource
extension LoveProfileEditViewViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.items.count
}
func collectionView(_ collectionView: UICollectionView, canMoveItemAt indexPath: IndexPath) -> Bool {
//set boolean as per your need
return true
}
func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
if sourceIndexPath.row > 0 && destinationIndexPath.row > 0 {
self.moveDataItem(sourceIndexPath.row, destinationIndexPath.row)
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Some Identifier", for: indexPath) as! SomeNibName
cell.textLabel.text = self.items[indexPath.row]
return cell
}
}
https://github.com/bvogelzang/BVReorderTableView zum Neuordnen der Zelle UITableView –