iOS利用view.layer实现简单的抖动效果

觉得还可以,拿来收藏.
代码如下:

func shakeAnimation(view:UIView) {
    let viewLayer = view.layer
    let position = viewLayer.position
    //移动的两个终点位置
    let x = CGPointMake(position.x + 5, position.y)
    let y = CGPointMake(position.x - 5, position.y)
    //设置动画
    let animation = CABasicAnimation(keyPath: "position")
    //设置运动形式
    animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionDefault)
    //设置开始结束位置
    animation.fromValue = NSValue.init(CGPoint: x)
    animation.toValue = NSValue.init(CGPoint: y)
    //设置自动反转
    animation.autoreverses = true
    animation.duration = 0.06
    animation.repeatCount = 3
    viewLayer.addAnimation(animation, forKey: nil)
}

你可能感兴趣的:(iOS利用view.layer实现简单的抖动效果)