Skip to content

Instantly share code, notes, and snippets.

@DejanEnspyra
Created August 1, 2017 15:44
Show Gist options
  • Select an option

  • Save DejanEnspyra/d3606cb334ea4fa1e62990606c2143ae to your computer and use it in GitHub Desktop.

Select an option

Save DejanEnspyra/d3606cb334ea4fa1e62990606c2143ae to your computer and use it in GitHub Desktop.
Reodering gesture used for controlling drag and drop feature in UICollectionView
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()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment