Skip to content

Instantly share code, notes, and snippets.

@victorBaro
Last active May 5, 2018 21:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save victorBaro/523f1041e80e184b328b to your computer and use it in GitHub Desktop.
Save victorBaro/523f1041e80e184b328b to your computer and use it in GitHub Desktop.
Custom GestureRecognizer for Force Touch
import UIKit.UIGestureRecognizerSubclass
class ForceGestureRecognizer: UIGestureRecognizer {
var forceValue: CGFloat = 0
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent) {
super.touchesBegan(touches, withEvent: event)
state = .Began
handleForceWithTouches(touches)
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent) {
super.touchesMoved(touches, withEvent: event)
state = .Changed
handleForceWithTouches(touches)
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent) {
super.touchesEnded(touches, withEvent: event)
state = .Ended
handleForceWithTouches(touches)
}
func handleForceWithTouches(touches: Set<UITouch>) {
if touches.count != 1 {
state = .Failed
return
}
guard let touch = touches.first else {
state = .Failed
return
}
forceValue = touch.force
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment