Drawing CGPath as you create the path

Is it possible to draw CGPath as it is created (as each line is added), and not along the entire path of points drawn in an instant? I would like the path to be drawn when lines are added to it.

Also, is it possible to draw one line at a time as part of a larger path, and not waste the way each time with one additional line?

+4
source share
2 answers

If you use CAShapeLayer and set the path as a property of the path, you can animate the value of strokeEnd.

I don’t know how simple it would be to add dynamically to this, but I used it before to animate lines with time points along this path (but all the paths of these lines were known in advance)

+4
source

@ wattson12 is absolutely right. Here is an example of how I did this in the past:

- (void)animateBezier { CAShapeLayer *bezier = nil; UIBezierPath *bezierPath = [self bezierPath]; // clearly, generate your path anyway you want bezier = [[CAShapeLayer alloc] init]; bezier.path = bezierPath.CGPath; bezier.strokeColor = [UIColor redColor].CGColor; bezier.fillColor = [UIColor clearColor].CGColor; bezier.lineWidth = 5.0; bezier.strokeStart = 0.0; bezier.strokeEnd = 1.0; [self.view.layer addSublayer:bezier]; CABasicAnimation *animateStrokeEnd = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; animateStrokeEnd.duration = 1.0; animateStrokeEnd.fromValue = [NSNumber numberWithFloat:0.0f]; animateStrokeEnd.toValue = [NSNumber numberWithFloat:1.0f]; [bezier addAnimation:animateStrokeEnd forKey:@"strokeEndAnimation"]; } 

I don’t know if this is really what you want, or if you want to revive later adding an extra point in the way. If this is what you want, you can probably do something where you set the fromValue value accordingly or animate the drawing of another segment as a separate path.

+3
source

All Articles