Graphics Reference
In-Depth Information
1.
Select the property you want to animate, in this case, we use
opacity
.
2.
Create a new dictionary from the layer's current
actions
property.
3.
Add the transition to the new
actions
dictionary for the key
opacity
.
4.
Set the layer's
actions
property to the newly created dictionary.
The code to set the default transition for the
opacity
field is in Listing 6-12.
LISTING 6-12
Set the Default Transition for Opacity
CATransition *transition=[
CATransition animation
];
[transition setType:kCATransitionMoveIn];
[transition setSubtype:kCATransitionFromTop];
[transition setDuration:1.0f];
// Get the current list of actions
NSMutableDictionary *actions = [NSMutableDictionary
dictionaryWithDictionary:[transitionLayer actions]];
[actions setObject:transition forKey:
@”opacity”
];
[transitionLayer
setActions
:actions];
First, you need to instantiate the
CATransition
object. Using the default transitions as
specified in the Tables 6-2 and Table 6-3, set the type to
kCATransitionMoveIn
, which
causes the transition to move in from some direction. Then, in the
subtype
field, specify
the direction from which the transition should move in; in this example, we move in
from the top (
kCATransitionFromTop
). Next, specify the duration for the transition; in
this case, a full second.
At this point, we need to add the transition to the layer's list of actions. The layer knows
to use this transition rather than the default fade because of the key-path. We specify in
the dictionary that our key-path is
opacity
. Any time the layer's
opacity
changes, it uses
the specified transition rather than the default fade transition.
To make this work, we first instantiate a new dictionary using the original
actions
field
from
transitionLayer
. We do this just in case some other actions have been specified in
the collection.
We know that there isn't anything else in the dictionary; however, I mention it for clarity.
You just need to understand that if you create a new dictionary and set the layer's actions
to it without first grabbing what might have been there previously, you might be
confused as to why other transitions are not working correctly.
Finally, set the layer's
actions
dictionary to point to the newly created dictionary that
contains the
CATransition
object.