Search code examples
iphoneobjective-ccocoacore-animation

Animate CGContextStrokeEllipseInRect


I'm trying to animate the drawing of a circle. Something the Shazaam app is doing while recording, but much simpler.
I've got this code in the drawRect: method to draw the circle:

CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(currentContext, 0.86, 0.86, 0.86, 1.0);
CGContextSetLineWidth(currentContext, kLineWidth);
CGContextStrokeEllipseInRect(currentContext, CGRectMake(0.0, 0.0, 150.0, 150.0));

I tried to animate the circle with:

[CATransaction begin];
[CATransaction setValue:[NSNumber numberWithFloat: 3.0f] forKey: kCATransactionAnimationDuration];
// Drawing code above
[CATransaction commit];

The only thing it does is draw the circle as stated, but it's not animated.

Can anyone help me with this?


Solution

  • You're not going to be able to animate drawing in this way. Instead you'll have to loop through (using NSTimer, for example), and continually redraw.

    I'm not sure if this is how Shazaam works or not.

    Another alternative is to create a series of images and animate them in a UIImageView.