Core Animation 深入解析:冻结动画、过渡效果与动作机制
1. 冻结动画
在动画操作中,我们可以在图层级别冻结动画,这与之前使用属性动画器的效果类似。CALayer 采用了 CAMediaTiming 协议,这意味着图层可以有一个速度属性,该属性会影响附加到它的任何动画。例如,速度为 2 的 CALayer 会在 5 秒内播放一个原本 10 秒的动画,而速度为 0 的 CALayer 则会有效地冻结附加到该图层的任何动画。
图层还可以有一个时间偏移(timeOffset),通过更改时间偏移,我们可以显示图层动画的任何单帧。
以下是一个冻结 CAShapeLayer 动画的示例代码:
let shape = CAShapeLayer()
shape.frame = v.bounds
v.layer.addSublayer(shape)
shape.fillColor = UIColor.clear.cgColor
shape.strokeColor = UIColor.red.cgColor
let path = CGPath(rect:shape.bounds, transform:nil)
shape.path = path
let path2 = CGPath(ellipseIn:shape.bounds, transform:nil)
let ba = CABasicAnimation(keyPath:#keyPath(CAShapeLayer.path))
ba.duration = 1
ba.fromValue = path
ba.toValue = path2
shape.sp