如何减慢CALayer.transform动画的速度?

Ale*_*lex 3 animation duration transform ios

我目前需要一个图层(CALayer),我可以在这些比例之间缩放以创建动画.

private let invisibleScale = CATransform3DMakeScale(0.0,0.0,1.0)
private let fullScale = CATransform3DMakeScale(2.5,2.5,1.0)
Run Code Online (Sandbox Code Playgroud)

通过简单地在我的图层上调用下面的函数,图层就像我想要的那样动画(除了有点快).

animationLayer.transform = invisibleScale
animationLayer.transform = fullScale
Run Code Online (Sandbox Code Playgroud)

我试图添加CABasicAnimation与变换作为值,但这不起作用,因为它在完成动画后回到原始比例.像这样的东西:

let animation = CABasicAnimation(keyPath: "transform")
animation.toValue = NSValue(caTransform3D: invisibleScale)
animation.duration = animationDuration
animationLayer.add(animation, forKey: "transform")
Run Code Online (Sandbox Code Playgroud)

所以我尝试在结尾添加animationLayer.transform = fullScale来更新动画后的状态.

let animation = CABasicAnimation(keyPath: "transform")
animation.toValue = NSValue(caTransform3D: invisibleScale)
animation.duration = animationDuration
animationLayer.add(animation, forKey: "transform")
animationLayer.transform = fullScale
Run Code Online (Sandbox Code Playgroud)

这导致动画看起来与刚刚调用完全相同:

animationLayer.transform = fullScale
Run Code Online (Sandbox Code Playgroud)

我也试过像这样的东西:

UIView.animate(withDuration: 10) {
        self.animationLayer.transform = fullScale
}
Run Code Online (Sandbox Code Playgroud)

这也与只输入animationLayer.transform = invisibleScale的速度相同.

任何有关如何使这项工作的提示将不胜感激!

Ale*_*lex 9

我终于找到了使用CATransaction的解决方案!

CATransaction.begin()
CATransaction.setAnimationDuration(0.5)
animationLayer.transform = transform
CATransaction.commit()
Run Code Online (Sandbox Code Playgroud)