Skip to content

Instantly share code, notes, and snippets.

@hidez
Created January 16, 2020 14:27
import UIKit
extension ViewController {
/// セルの長押しのリスナーの登録
func addLongPressGestureListner() {
let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(handleLongGesture(gesture:)))
collectionView.addGestureRecognizer(longPressGesture)
}
// MARK: - PRIVATE METHODS
/// セルの長押しジェスチャーのアクション
///
/// - Parameters: gesture
@objc
private func handleLongGesture(gesture: UILongPressGestureRecognizer) {
switch gesture.state {
case .began:
guard let selectedIndexPath = collectionView.indexPathForItem(at: gesture.location(in: collectionView)) else {
break
}
collectionView.beginInteractiveMovementForItem(at: selectedIndexPath)
case .changed:
collectionView.updateInteractiveMovementTargetPosition(gesture.location(in: gesture.view))
case .ended:
collectionView.endInteractiveMovement()
default:
collectionView.cancelInteractiveMovement()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment