sublayerTransform忽略动画持续时间

时间:2017-02-08 02:30:53

标签: ios swift core-animation

以下结果导致视图在两秒内从1/2大小放大;

myView.layer.transform = CATransform3DMakeAffineTransform(
    CGAffineTransform.identity.scaledBy(x: 0.5, y: 0.5))
UIView.animate(withDuration: 2, animations: {
    myView.layer.transform = CATransform3DIdentity
})

然而,以下情况会导致视图立即放大 - 忽略持续时间;

myView.layer.sublayerTransform = CATransform3DMakeAffineTransform(
    CGAffineTransform.identity.scaledBy(x: 0.5, y: 0.5))
UIView.animate(withDuration: 2, animations: {
    myView.layer.sublayerTransform = CATransform3DIdentity
})

为什么sublayerTransform不会在持续时间内制作动画? CALayer注意到它是可动画的。

1 个答案:

答案 0 :(得分:3)

您无法通过sublayerTransformUIView.animate制作动画。相反,请使用CABasicAnimation

let animation = CABasicAnimation(keyPath: "sublayerTransform")
animation.fromValue = CATransform3DMakeAffineTransform(
    CGAffineTransform.identity.scaledBy(x: 0.5, y: 0.5))
animation.duration = 2
myView.layer.add(animation, forKey: "sublayerTransform")
myView.layer.sublayerTransform = CATransform3DIdentity