Use CAShapeLayers and a combination of CATransactions and CABasicAnimation.
You can add this path to the shapeLayer element and let it render.
The CAShapeLayer object has two properties called strokeStart and strokeEnd , which determines where along the path that the end of the line should execute. Default values: 0.0 for strokeStart and 1.0 for strokeEnd .
If you set your path so that strokeEnd initially strokeEnd with 0.0, you will not see the line.
Then you can animate from 0.0 to 1.0, the strokeEnd property, and you will see that the line lengthens.
To change the CAShapeLayer implicit default 0.25-second animation time, you can add a function to the class, for example:
-(void)animateStrokeEnd:(CGFloat)_strokeEnd { [CATransaction begin]; CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:keyPath]; animation.duration = 2.0f;
You can pass any value from 0.0f to 1.0f as the value of _strokeEnd .
setCompletionBlock: ensures that the value you setCompletionBlock: is explicitly set after the animation finishes.
source share