Search code examples
iphoneobjective-ccore-animationcalayer

CGContextDrawImage without Transaction animation


How do I draw a layer without a transaction animation? For example, when I set the contents of a layer using CATransaction it works well:

[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanTrue
                 forKey:kCATransactionDisableActions];
myLayer.contents = (id)[[imagesTop objectAtIndex:Number]CGImage];
[CATransaction commit];

but I need to change contents from the delegate method [myLayer setNeedsDisplay]. Here is the code:

-(void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context
{
    CGContextDrawImage(context, rect, Image.CGImage]);    
}

Solution

  • CALayer implements setNeedsDisplay method:

    Calling this method will cause the receiver to recache its content. This will result in the layer receiving a drawInContext: which may result in the delegate receiving either a displayLayer: or drawLayer:inContext: message.

    ... and displayIfNeeded:

    When this message is received the layer will invoke display if it has been marked as requiring display.

    If you wrap [myLayer displayIfNeeded] in a CATransaction, you can turn off implicit animation.