Created
August 1, 2017 15:44
-
-
Save DejanEnspyra/d3606cb334ea4fa1e62990606c2143ae to your computer and use it in GitHub Desktop.
Reodering gesture used for controlling drag and drop feature in UICollectionView
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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