Skip to content

Instantly share code, notes, and snippets.

@pstasiak
Last active October 26, 2020 10:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pstasiak/521f8959c6831a6cde537cc25e4e9685 to your computer and use it in GitHub Desktop.
Save pstasiak/521f8959c6831a6cde537cc25e4e9685 to your computer and use it in GitHub Desktop.
Setting custom drag gesture initiation time. Important: Should be called after each update of `dragDelegate`.
import UIKit
extension UICollectionView {
func setupDragInitiationTime(_ time: TimeInterval) {
let dragInitiatingRecognizerName = "dragInitiation"
let longPressRecognizers = gestureRecognizers?.compactMap {
$0 as? UILongPressGestureRecognizer
} ?? []
let dragInitiatingRecognizers = longPressRecognizers.filter {
$0.name == dragInitiatingRecognizerName
}
assert(
!dragInitiatingRecognizers.isEmpty,
"""
Unable to setup drag initiation time - no gesture recognizer named "\(dragInitiatingRecognizerName)".
Existing UILongPressGestureRecognizer subclasses:
\(longPressRecognizers)
"""
)
dragInitiatingRecognizers.forEach { $0.minimumPressDuration = time }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment