UIView shake animation

后端 未结 16 1301
梦毁少年i
梦毁少年i 2020-11-28 18:07

i\'m trying to make a UIView shake when a button is pressed.

I am adapting the code I found on http://www.cimgf.com/2008/02/27/core-animation-tutorial-window-shake-e

16条回答
  •  情话喂你
    2020-11-28 18:47

    Based on @bandejapaisa answer, UIView extension for Swift 3

    extension UIView {
        func shake() {
            let animation = CAKeyframeAnimation(keyPath: "transform.translation.x")
            animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
            animation.duration = 0.6
            animation.values = [-20, 20, -20, 20, -10, 10, -5, 5, 0]
            layer.addAnimation(animation, forKey: "shake")
        }
    }
    

提交回复
热议问题