在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")
这样,视图就会以指定的路径进行关键帧动画了。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: iOS中mvvm怎么实现控制字号大小