Every modification to a layer is part of a transaction. CATransaction
is the Core Animation class responsible for batching multiple layer-tree modifications into atomic updates to the render tree.
This chapter describes the two types of transactions Core Animation supports: implicit transactions and explicit transactions.
Implicit transactions
Explicit Transactions
Implicit transactions are created automatically when the layer tree is modified by a thread without an active transaction, and are committed automatically when the thread's run-loop next iterates.
The example in Listing 1 modifies a layer’s opacity
, zPosition
, and position
properties, relying on the implicit transaction to ensure that the resulting animations occur at the same time.
Listing 1 Animation using an implicit transaction
theLayer.opacity=0.0; |
theLayer.zPosition=-200; |
thelayer.position=CGPointMake(0.0,0.0); |
Important: When modifying layer properties from threads that don’t have a runloop, you must use explicit transactions.
You create an explicit transaction by sending the CATransaction
class a begin
message before modifying the layer tree, and a commit
message afterwards. Explicit transactions are particularly useful when setting the properties of many layers at the same time (for example, while laying out multiple layer), temporarily disabling layer actions, or temporarily changing the duration of resulting implied animations.
You can temporarily disable layer actions when changing layer property values by setting the value of the transaction’s kCATransactionDisableActions
to true. Any changes made during the scope of that transaction will not resulting in an animation occuring. Listing 2 shows an example that disables the fade animation that occurs when removing aLayer
from a visible layer-tree.
Listing 2 Temporarily disabling a layer’s actions
[CATransaction begin]; |
[CATransaction setValue:(id)kCFBooleanTrue |
forKey:kCATransactionDisableActions]; |
[aLayer removeFromSuperlayer]; |
[CATransaction commit]; |
You can temporarily alter the duration of animations that run in response to changing layer property values by setting the value of the transaction’s kCATransactionAnimationDuration
key to a new duration. Any resulting animations in that transaction scope will use that duration rather than their own. Listing 3 shows an example that causes an animation to occur over 10 seconds rather than the duration specified by the zPosition
and opacity
animations..
Listing 3 Overriding the animation duration
[CATransaction begin]; |
[CATransaction setValue:[NSNumber numberWithFloat:10.0f] |
forKey:kCATransactionAnimationDuration]; |
theLayer.zPosition=200.0; |
theLayer.opacity=0.0; |
[CATransaction commit]; |
Although the above example shows the duration bracketed by an explicit transaction begin
and commit
, you could omit those and use the implicit transaction instead.
Explicit transactions can be nested, allowing you to disable actions for one part of an animation, or using different durations for the implicit animations of properties that are modified. Only when the outer-most transaction is committed will the animations occur.
Listing 4 shows an example of nesting two transactions. The outer transaction sets the implied animation duration to 2 seconds and sets the layer’s position
property. The inner transaction sets the implied animation duration to 5 seconds and changes the layer’s opacity
and zPosition
.
Listing 4 Nesting explicit transactions
[CATransaction begin]; // outer transaction |
// change the animation duration to 2 seconds |
[CATransaction setValue:[NSNumber numberWithFloat:2.0f] |
forKey:kCATransactionAnimationDuration]; |
// move the layer to a new position |
theLayer.position = CGPointMake(0.0,0.0); |
[CATransaction begin]; // inner transaction |
// change the animation duration to 5 seconds |
[CATransaction setValue:[NSNumber numberWithFloat:5.0f] |
forKey:kCATransactionAnimationDuration]; |
// change the zPosition and opacity |
theLayer.zPosition=200.0; |
theLayer.opacity=0.0; |
[CATransaction commit]; // inner transaction |
[CATransaction commit]; // outer transaction |
© 2008 Apple Inc. All Rights Reserved. (Last updated: 2008-11-13)