Skip to content

Instantly share code, notes, and snippets.

@marty-suzuki
Last active July 17, 2016 19:22
Show Gist options
  • Save marty-suzuki/ee1967e1afbd8327a1fcf73e32cdbbad to your computer and use it in GitHub Desktop.
Save marty-suzuki/ee1967e1afbd8327a1fcf73e32cdbbad to your computer and use it in GitHub Desktop.
// based on this article. http://qiita.com/magickworx/items/9a8e6108db8134a8363a#comment-f7c2edf79151a38f4d9d
extension Double {
func toRadians() -> Double {
return self * Double(M_PI) / 180
}
}
extension UIView {
private struct Const {
static let VibrateAnimationKey = "VibrateAnimationKey"
static let TransformRotationKeyPath = "transform.rotation"
}
func vibrated(vibrated: Bool) {
guard vibrated else {
layer.removeAnimationForKey(Const.VibrateAnimationKey)
return
}
let animation = CABasicAnimation(keyPath: Const.TransformRotationKeyPath)
animation.duration = 0.25
animation.fromValue = 3.0.toRadians()
animation.toValue = -3.0.toRadians()
animation.repeatCount = .infinity
animation.autoreverses = true
layer.addAnimation(animation, forKey: Const.VibrateAnimationKey)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment