Created
May 6, 2016 15:33
-
-
Save wess/ec685b7acadb5afdfe4cce289d0bb3eb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// | |
// ForceTouchGestureRecognizer.swift | |
// Payment | |
// | |
// Created by Wesley Cope on 5/5/16. | |
// Copyright © 2016 Nudge, llc. All rights reserved. | |
// | |
import Foundation | |
import UIKit | |
import UIKit.UIGestureRecognizerSubclass | |
import AudioToolbox | |
class ForceTouchGestureRecognizer : UIGestureRecognizer { | |
var threshold:CGFloat = 1 | |
private var max:CGFloat = 1 | |
private var force:CGFloat = 0 | |
private var normalized:CGFloat { | |
return force / max | |
} | |
override func touchesBegan(touches:Set<UITouch>, withEvent event:UIEvent) { | |
super.touchesBegan(touches, withEvent: event) | |
state = .Began | |
} | |
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent) { | |
guard let touch = touches.first else { return } | |
if touch.force != force { | |
force = touch.force | |
max = touch.maximumPossibleForce | |
state = .Changed | |
} | |
if normalized >= threshold { | |
state = .Ended | |
} | |
else if normalized > 0.75 { | |
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate) | |
} | |
} | |
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent) { | |
super.touchesEnded(touches, withEvent: event) | |
state = .Changed | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment