在iOS中,可以使用CAKeyframeAnimation实现关键帧动画。下面是使用CAKeyframeAnimation的步骤:
let animation = CAKeyframeAnimation(keyPath: "position")
let startPoint = CGPoint(x: 0, y: 0)
let endPoint = CGPoint(x: 100, y: 100)
animation.values = [NSValue(cgPoint: startPoint), NSValue(cgPoint: endPoint)]
animation.duration = 1.0
animation.repeatCount = Float.infinity
view.layer.add(animation, forKey: "positionAnimation")
完整的示例代码:
let animation = CAKeyframeAnimation(keyPath: "position")
let startPoint = CGPoint(x: 0, y: 0)
let endPoint = CGPoint(x: 100, y: 100)
animation.values = [NSValue(cgPoint: startPoint), NSValue(cgPoint: endPoint)]
animation.duration = 1.0
animation.repeatCount = Float.infinity
view.layer.add(animation, forKey: "positionAnimation")
这样,视图就会以指定的路径进行关键帧动画了。