Last active
May 5, 2018 21:29
-
-
Save victorBaro/523f1041e80e184b328b to your computer and use it in GitHub Desktop.
Custom GestureRecognizer for Force Touch
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.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