< Previous PageNext Page > Hide TOC

Transactions

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.

Contents:

Implicit transactions
Explicit Transactions


Implicit 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.

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.

Temporarily Disabling Layer Actions

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];

Overriding the Duration of Implied Animations

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.

Nesting Transactions

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


< Previous PageNext Page > Hide TOC


© 2008 Apple Inc. All Rights Reserved. (Last updated: 2008-11-13)


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.