Skip to content

Instantly share code, notes, and snippets.

@mbehan
Created November 11, 2016 23:11
Show Gist options
  • Save mbehan/25e315f05686b87b28a376c6434f9c5e to your computer and use it in GitHub Desktop.
Save mbehan/25e315f05686b87b28a376c6434f9c5e to your computer and use it in GitHub Desktop.
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