daily pastebin goal
28%
SHARE
TWEET

Untitled

a guest Oct 19th, 2018 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class GTForceTouchGestureRecognizer: UIGestureRecognizer {
  2.    
  3.     internal var deepPressedAt: TimeInterval = 0
  4.     internal var gestureParameters: (target: AnyObject?, action: Selector)
  5.     internal var hardTriggerMinTime: TimeInterval
  6.     internal let feedbackGenerator = UIImpactFeedbackGenerator.init(style: .medium)
  7.     internal let threshold: CGFloat
  8.    
  9.     internal var deepPressed: Bool = false {
  10.         didSet {
  11.             guard deepPressed, !oldValue else {
  12.                 return
  13.             }
  14.             deepPressedAt = NSDate.timeIntervalSinceReferenceDate
  15.         }
  16.     }
  17.    
  18.     public override var state: UIGestureRecognizer.State {
  19.         didSet {
  20.             guard oldValue != state else {
  21.                 return
  22.             }
  23.             switch state {
  24.             case .began:
  25.                 feedbackGenerator.prepare()
  26.             case .ended:
  27.                 feedbackGenerator.impactOccurred()
  28.                 _ = gestureParameters.target?.perform(gestureParameters.action, with: self)
  29.             default:
  30.                 return
  31.             }
  32.         }
  33.     }
  34.    
  35.     required public init(target: AnyObject?, action: Selector, threshold: CGFloat = 0.75, hardTriggerMinTime: TimeInterval = 0.5) {
  36.         self.gestureParameters = (target, action)
  37.         self.threshold = threshold
  38.         self.hardTriggerMinTime = hardTriggerMinTime
  39.         super.init(target: nil, action: nil)
  40.     }
  41.    
  42.     override public func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
  43.         handleTouch(touches.first)
  44.     }
  45.    
  46.     override public func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) {
  47.         handleTouch(touches.first)
  48.     }
  49.    
  50.     override public func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent) {
  51.         state = deepPressed ? UIGestureRecognizer.State.ended : UIGestureRecognizer.State.failed
  52.         deepPressed = false
  53.         super.touchesEnded(touches, with: event)
  54.     }
  55.  
  56.     internal func handleTouch(_ touch: UITouch?) {
  57.         guard view != nil, let touch = touch, touch.force != 0 && touch.maximumPossibleForce != 0 else {
  58.             return
  59.         }
  60.         let forcePercentage = touch.force / touch.maximumPossibleForce
  61.         if deepPressed && forcePercentage <= 0 {
  62.             state = UIGestureRecognizer.State.ended
  63.             return
  64.         }
  65.         handleForceTouch(with: forcePercentage)
  66.     }
  67.    
  68.     private func handleForceTouch(with forcePercentage: CGFloat) {
  69.         let currentTime = NSDate.timeIntervalSinceReferenceDate
  70.         if !deepPressed && forcePercentage >= threshold {
  71.             deepPressed = true
  72.             state = UIGestureRecognizer.State.began
  73.             return
  74.         }
  75.         if deepPressed && currentTime - deepPressedAt > hardTriggerMinTime && forcePercentage == 1.0 {
  76.             state = UIGestureRecognizer.State.ended
  77.         }
  78.     }
  79. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top