Skip to content

Instantly share code, notes, and snippets.

@matteodanelli
Created November 4, 2017 14:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matteodanelli/f6f6d21b41a7a91086c1373a5f2987e6 to your computer and use it in GitHub Desktop.
Save matteodanelli/f6f6d21b41a7a91086c1373a5f2987e6 to your computer and use it in GitHub Desktop.
Add wiggle animation to UIButton
import UIKit
extension UIButton {
// Animate a button, adding effect of "something went wrong". Useful for login button for example.
func wiggle() {
let wiggleAnim = CABasicAnimation(keyPath: "position")
wiggleAnim.duration = 0.05
wiggleAnim.repeatCount = 5
wiggleAnim.autoreverses = true
wiggleAnim.fromValue = CGPoint(x: self.center.x - 4.0, y: self.center.y)
wiggleAnim.toValue = CGPoint(: self.center.x + 4.0, y: self.center.y)
layer.add(wiggleAnim, forKey: "position")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment