Search code examples
iphoneiosopengl-esdrawingcore-animation

How to stroke and reveal a path slowly?


For a scientific simulation, I need to stroke and reveal a path slowly over time, starting at the first point and traveling through all other points until reaching the last point. Like if you take a pencil and start slowly drawing a circle, spiral or triangle.

How could I do this with Core Animation or maybe OpenGL ES? Core Graphics would possibly be too slow to refresh at 60 fps.

There is CAKeyframeAnimation which can travel along a path, but I need a way of actually stroking that path while traveling along it.

Edit: Should run on iOS 3.2 as well!


Solution

    1. Create a CAShapeLayer to display your path.
    2. Animate the layer's strokeEnd property from 0.0 to 1.0.

    See this blog post of mine for details.