Animate CAShapeLayer path change

倖福魔咒の 提交于 2019-12-19 07:45:11

问题


I'm trying to animate change of path property of CAShapeLayer like this:

animatePathChange(for: progressLayer, toPath: progressPath.cgPath)
progressLayer.path = progressPath.cgPath

And this isanimatePathChangefunction code:

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")
}

But the end result is not what i want. How can i achieve animation that changes layer's path from old value to new?

This is how it looks right now with the code above:


回答1:


Don't animate the path. Configure the whole path and then set the strokeEnd — let's say it's 0, so the user sees nothing. Now each time you want to make a change, animate the change from the current strokeEnd to the new strokeEnd. The path will appear to extend further round the curve.



来源:https://stackoverflow.com/questions/42378051/animate-cashapelayer-path-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!