1
votes

I am creating a screen in iOS Xcode 8.3.2 Swift 3.1 that has a horizontally scrolling UICollectionView on it with a single row of images. The problem is, I had thought the "automatic re-ordering" capability was part of the UICollectionView, when it actually is only implemented in the UICollectionViewController. As I just want a portion of the screen to scroll, I don't believe I can use the collection view controller -- the collection view is always full screen in that case, right? With the controller, I discovered installsStandardGestureForInteractiveMovement, which automatically implements a gesture recognizer.

I would like this same capability in my collection view, which is on a UIViewController. However, I have no idea which gestures I need to capture to implement cell re-ordering. Is any of the drag re-ordering "magic" built into the collection view itself? Is there a way I can use the controller, instead, and not have the collection view full screen?

I have implemented the following method to do the actual move of the data, but I'm not sure which gesture recognizer(s) to setup.

func collectionView(_ collectionView: UICollectionView,
                        moveItemAt sourceIndexPath: IndexPath,
                        to destinationIndexPath: IndexPath) {

    let cellToMove = promoPages.remove(at: (sourceIndexPath as NSIndexPath).row)
    promoPages.insert(cellToMove, at: (destinationIndexPath as NSIndexPath).row)
}
1
Thanks, @user2215977. With a little tweaking for Swift 3.1, I got it working. I think I found that article, too, but saw the reference to "...controller" and stopped reading.Lastmboy

1 Answers

0
votes

After reading this guide, and converting it to Swift3:

For UIViewController class:

override func viewDidLoad() {
    super.viewDidLoad()

    let g = UILongPressGestureRecognizer(target: self,
                                         action: #selector(handleLongGesture(_:)))
    clcImages?.addGestureRecognizer(g)
}

func handleLongGesture(_ gesture: UILongPressGestureRecognizer)
{
    switch(gesture.state) {
    case UIGestureRecognizerState.began:
        guard let selectedIndexPath = clcImages?.indexPathForItem(at: gesture.location(in: clcImages)) else {
            break
        }
        clcImages?.beginInteractiveMovementForItem(at: selectedIndexPath)
    case UIGestureRecognizerState.changed:
        clcImages?.updateInteractiveMovementTargetPosition(gesture.location(in: gesture.view!))
    case UIGestureRecognizerState.ended:
        clcImages?.endInteractiveMovement()
    default:
        clcImages?.cancelInteractiveMovement()
    }
}