Monthly Archives: May 2012

Fade-in animation at UIView’s drawRect

Although UIView’s animateWithDuration:animations: method is quite useful, if you want a view to animate a drawing code written in drawRect method, you can’t use this method.

CABasicAnimation class is suitable for solving this problem like this.

- (void)drawRect:(CGRect)rect
{
    CABasicAnimation *animation;

    animation = [CABasicAnimation animation];
    [animation setDuration:0.5];

    [[self layer] addAnimation:animation
                        forKey:@"contents"];

    // drawing code
}

Reference