2016-10-11 27 views
2

我目前需要一個圖層(CALayer),我可以在這些比例之間縮放來創建動畫。如何減慢CALayer.transform動畫?

private let invisibleScale = CATransform3DMakeScale(0.0,0.0,1.0) 
private let fullScale = CATransform3DMakeScale(2.5,2.5,1.0) 

通過簡單地調用我的層下面的功能層動畫就像我希望它(除了有點快)。

animationLayer.transform = invisibleScale 
animationLayer.transform = fullScale 

我試圖與轉化爲價值,但因爲它會回到原來的規模完成了動畫之後不起作用加CABasicAnimation。事情是這樣的:

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

所以,我想加入animationLayer.transform =滿量程末動畫後更新的狀態。

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

這導致看起來完全一樣,只是打電話動畫:

animationLayer.transform = fullScale 

我也嘗試這樣的東西:

UIView.animate(withDuration: 10) { 
     self.animationLayer.transform = fullScale 
} 

這也以相同的速度動畫只需鍵入animationLayer.transform = invisibleScale。

如何使這項工作的任何提示將不勝感激!

回答

5

我終於找到了一個使用CATransaction的解決方案!

CATransaction.begin() 
CATransaction.setAnimationDuration(0.5) 
animationLayer.transform = transform 
CATransaction.commit()