Swift 关键帧动画小计

///抖动动画
let anim = CAKeyframeAnimation(keyPath: "transform.rotation")
anim.values = [(-1 / 180 * Double.pi), (1 / 180 * Double.pi),(-1 / 180 * Double.pi)]
anim.repeatCount = Float(MAX_CANON)
anim.duration = 0.3
anim.autoreverses = true
shareButton.layer.add(anim, forKey: nil)


/// 位移动画
let postion = CAKeyframeAnimation(keyPath: "position")
let value1 = NSValue(cgPoint: CGPoint(x: imageView.x-20, y: imageView.y+20))
let value2 = NSValue(cgPoint: CGPoint(x: imageView.x, y: imageView.y))
postion.values = [value1,value2]


/// 旋转效果(创建子动画)
let rotate = CABasicAnimation(keyPath: "transform.rotation")
rotate.fromValue = .pi / 4.0
rotate.toValue = 0.0


/// 创建组动画
let group = CAAnimationGroup()

/// 消失动画
let alpha = CABasicAnimation(keyPath: "opacity")
alpha.fromValue = 0.0
alpha.toValue = 1.0
/// 位移动画
let postion = CAKeyframeAnimation(keyPath: "position")
let value1 = NSValue(cgPoint: CGPoint(x: imageView.x-20, y: imageView.y+20))
let value2 = NSValue(cgPoint: CGPoint(x: imageView.x, y: imageView.y))
postion.values = [value1,value2]
/// 缩放动画
let scaleDown = CABasicAnimation(keyPath: "transform.scale")
scaleDown.fromValue = 0.2
scaleDown.toValue = 1.2

/// 次数
group.repeatCount = Float(MAX_CANON)
/// 每次次动画时间
group.duration = 1.5
group.autoreverses = false
/// 添加进组动画
group.animations = [alpha,scaleDown,postion]
/// 添加进空间
imageView.layer.add(group, forKey: nil)

你可能感兴趣的:(Swift 关键帧动画小计)