以下结果导致视图在两秒内从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
注意到它是可动画的。
答案 0 :(得分:3)
您无法通过sublayerTransform
为UIView.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