Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A UIPanGestureRecognizer subclass to get the force of the gesture's touch (assumes a single touch)
import UIKit.UIGestureRecognizerSubclass
class ForcePanGestureRecognizer : UIPanGestureRecognizer {
private(set) var force = CGFloat(0) {
didSet {
if force > maxForce {
maxForce = force
}
}
}
private(set) var maxForce = CGFloat(0)
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
maxForce = CGFloat(0)
force = touches.first!.force
super.touchesBegan(touches, with: event)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) {
force = touches.first!.force
super.touchesMoved(touches, with: event)
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent) {
force = touches.first!.force
super.touchesEnded(touches, with: event)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.