2017-02-21 88 views
2

我試圖動畫這樣的CAShapeLayerpath性質的變化:動畫CAShapeLayer通路改變

​​

這是animatePathChange功能代碼:

func animatePathChange(for layer: CAShapeLayer, toPath: CGPath) { 
    let animation = CABasicAnimation(keyPath: "path") 
    animation.duration = 1.0 
    animation.fromValue = layer.path 
    animation.toValue = toPath 
    animation.timingFunction = CAMediaTimingFunction(name: "easeInEaseOut") 
    layer.add(animation, forKey: "path") 
} 

但最終的結果是不是有什麼我想要。我如何實現將圖層的path從舊值更改爲新的動畫?

這是它的外觀,現在上面的代碼:

Result

+0

你可以動畫層的'strokeEnd'而不是改變的路徑? – dan

+0

這將啓動從開始繪製的路徑。我想動畫只是沿着這條路走新舊路徑的區別。 –

回答

7

不要動畫路徑。配置整個路徑,然後設置strokeEnd - 假設它是0,所以用戶什麼都看不到。現在,每次您要進行更改時,都會將當前的strokeEnd更改爲新的strokeEnd。該路徑似乎將進一步延伸到曲線。

enter image description here

+0

爲什麼我沒有想到這個..非常感謝! –