Search code examples
iosobjective-ccore-animationcatransaction

Disable implicit animation without CATransaction begin&commit


I see many people use this to disable implicit animation:

[CATransaction begin];
[CATransaction setDisableActions:YES];
someLayer.backgroundColor = someCGColor;//no animation
[CATransaction commit];

But without CATransaction begin&commit it also works:

[CATransaction setDisableActions:YES];
someLayer.backgroundColor = someCGColor;//no animation

And like this it also works:

[CATransaction setDisableActions:YES];
someLayer1.backgroundColor = someCGColor;//no animation
[CATransaction setDisableActions:NO];
someLayer2.backgroundColor = someCGColor2; //have animation

So the question is, why I need to use CATransaction begin&commit? Are there any cases that I have to use them?

Thanks , Aunn.


Solution

  • This has to do with transaction blocks in Core Animation. By default, there is an implicit transaction block that automatically catches the calls to CATransaction. Using CATransaction begin/commit creates an explicit transaction block that allows you to apply different animation characteristics to different elements of an animation.

    Theoretically, you might need an explicit transaction block if something needs to be done immediately and not on the next redraw call, such as adding or removing animations. This cause problems when done incorrectly, such as by starting an animation before any draw calls are being done.