How do you animate an SVG path in IOS?

后端 未结 3 2425
灰色年华
灰色年华 2020-12-08 08:35

I have an SVG path like this:



        
3条回答
  •  佛祖请我去吃肉
    2020-12-08 09:27

    Once you've rendered your SVG path, to make it look like it's being drawn with a pencil, you could simply cover it all with an opaque layer, and then animate the movement of this layer along the path.

    To find the CGPath along which you'll move the layer you can use this library: https://github.com/arielelkin/PocketSVG

    This will parse the SVG data into a UIBezierPath. Then:

    SvgToBezier *myBezier = [[SvgToBezier alloc] initFromSVGPathNodeDAttr:@"M176.17,369.617c0,0,335.106-189.361,214.894,38.298s129.787,282.978,178.723,42.553C618.724,210.042,834.681,87.702,790,307.915" rect:CGRectMake(0,0,1024,768)];
    
    UIBezierPath *myPath = myBezier.bezier;
    
    CAKeyframeAnimation *mySVGPathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    bounceAnimLeft.duration = 3;
    bounceAnimLeft.path = myPath.CGPath;
    
    [myObjectToMove.layer addAnimation:mySVGPathAnimation forKey:@"pathAnimation"];
    

提交回复
热议问题