通过新的自定义属性在CALayer中自定义动画的两个属性
我可以通过使用CABasicAnimation更改属性来使CALayer对象动画,如下所示:
let animationOfPropertyOpacity = CABasicAnimation(keyPath: "opacity")
animationOfPropertyOpacity.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)
animationOfPropertyOpacity.fromValue = 0
animationOfPropertyOpacity.toValue = 1
myLayer.addAnimation(animationOfPropertyOpacity, forKey: "fade")
在这种情况下,动画将创建0到1之间的线性插值。我可以对其他属性(如比例,位置或旋转)进行相同操作。
如果我想从点A(0,3)移动到点B(3,0),但是使点C(0,0)变为圆?
我想继续使用fromValue toValue,但将我制作的自定义属性设置为rotation。 旋转将根据点C为中心计算每个插值,并从该中心开始一个半径。
我需要把计算每个步骤的函数放在哪里?
我已经尝试继承CALayer并添加一个名为rotation的自定义属性。 然后将代码添加到needsDisplayForKey以重绘该图层:
@NSManaged var rotation: CGFloat
override class func needsDisplayForKey(key: String) -> Bool {
if (key == "rotation") {
return true
}
return super.needsDisplayForKey(key)
}
这将调用drawInContext并使整个CALayer变黑。 我在drawInContext中有一个打印,并且在更改其他属性(如不透明度)时不会调用它,似乎没有必要重新绘制图层。
在CALayer中应该有一种方法被称为动画的每一帧,我可以在应用这些改变之前实现一个函数。
我也无法找到合适的位置来实现我创建的自定义属性的功能,即使没有动画也能实际反映更改。 让我们说一个属性customOpacity
,它只是反转不透明度的效果并相应地更新图层。 我只能通过函数updateRotation(rotation: CGFloat)
来做到这一点。
上一篇: Custom animation of two properties in CALayer by a new custom property
下一篇: How to synchronize CALayer and UIView animations up and down a complex hierarchy