Skip to content

Instantly share code, notes, and snippets.

@SAllen0400
Created February 16, 2017 04:47
Show Gist options
  • Save SAllen0400/18b7799f8195fbf91a11a844d368cbc9 to your computer and use it in GitHub Desktop.
Save SAllen0400/18b7799f8195fbf91a11a844d368cbc9 to your computer and use it in GitHub Desktop.
Get length of Long Press Gesture
// Swift 3
// If you'd like to use the gestureDuration outside of the scope of the button press, create some variables.
var gestureStartTime: TimeInterval!
var gestureDuration: TimeInterval!
// IBAction for UILongPressGestureRecognizer
@IBAction func recordButtonHeld(_ sender: UILongPressGestureRecognizer) {
switch sender.state {
case .began:
// By default, .began triggers after the button is held for 0.5 seconds.
// This can be adjusted by changing the minimumPressDuration property
gestureStartTime = Date.timeIntervalSinceReferenceDate
case .ended:
gestureDuration = Date.timeIntervalSinceReferenceDate - gestureStartTime
default:
break
}
}
// Now you have the gestureDuration value in a variable and can use it however you like in your code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment