Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tamassengel/d0f1079698e9d26cc63f714e88b9fc0b to your computer and use it in GitHub Desktop.
Save tamassengel/d0f1079698e9d26cc63f714e88b9fc0b to your computer and use it in GitHub Desktop.
A UIGestureRecognizer that fires either on long-press or on a force-touch (3D Touch ™️)
import UIKit.UIGestureRecognizerSubclass
final class MNTLongPressGestureRecognizer: UILongPressGestureRecognizer {
var triggerWithForceTouch = true
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
super.touchesBegan(touches, with: event)
handleTouches(touches, with: event)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) {
super.touchesMoved(touches, with: event)
handleTouches(touches, with: event)
}
fileprivate func handleTouches(_ touches: Set<UITouch>, with event: UIEvent) {
guard triggerWithForceTouch,
state == .possible,
let traitCollection = view?.traitCollection,
traitCollection.forceTouchCapability == .available,
touches.count == 1 else {
return
}
let touch = touches.first!
let force = touch.force / touch.maximumPossibleForce
if force >= 1.0 {
state = .began
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment