Search code examples
iphoneanimationcore-animationcore-graphicsquartz-graphics

iPhone Quartz CGContextRotateCTM Animation


I am drawing a pie graph and on tap, I am rotating it to the next pie with

CGContextRotateCTM(context, degreesToRadians(mAngle)).

Is there a way to animate this transition? Could you please post an example?


Solution

  • I think if you really want rotate your chart just changing drawing code you'll need to create a timer and change rotation angle in its method and also make view redraw there.

    Instead of applying rotation in drawing code I'd rotate the view with chart itself if that's possible - either with UIView animations or with CALayer animations - they are pretty straightforward and provide much more flexibility. e.g.

    [UIView animateWithDuration:1.0f 
                     animations:^{
                         view.transform = CGAffineTransformMakeRotation(mAngle);
                     }];
    

    Will rotate your view on mAngle with animation during 1 sec (method is available starting iOS4). Also if you want to rotate your view by 360 degrees or more you'll need to use CALayer animation (check this question for details).

    Also using standard animations most likely won't make your view redraw, so it will be more efficient.