/ViewController+LongPress.swift Secret
Created
January 16, 2020 14:27
This file contains 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
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