2017-02-21 71 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

为什么我没有想到这个..非常感谢! –