Graphics Reference
In-Depth Information
If you add this listing to your project we created in the previous section, you simply
connect a button to the action in Interface Builder. To do so, follow these steps:
1. Open AppDelegate.h and add an action declaration, as follows:
@interface AppDelegate : NSObject
{
IBOutlet NSWindow *window;
CALayer *layer;
}
- ( IBAction )animate:( id )sender;
2. Open AppDelegate.m and add the animate implementation code provided in
Listing 3-8.
3. Open Interface Builder. From the Objects Library, drag a button onto the main
window.
4. Control-click the button you just dragged on the main window and drag a connec-
tion to the AppDelegate object. Select the animate action.
5. Return to Xcode and Build and Go to see this animation run.
That's it. That is really all there is to animating a layer. You create the animation, set the
to and from values, set a duration (which is optional as the default 0.25 seconds will be
used if you don't specify a duration explicitly), and add the animation to the layer you
want to animate.
That being said, you will not likely leave it at that because the details of implementation
add nuance and complexity. For example, the first time you run the animation from
Listing 3-8, you notice that while your layer animates to the correct position in the
parent view using the duration you specified, when the animation completes, it jumps
right back to its starting position. Is this a bug? How can we fix it? We get to that next.
Animating Versus Setting Layer Properties
When you create your CABasicAnimation , you need to specify a start and stop value for
the animation using the calls to -setFromValue and -setToValue respectively. When you
add your basic animation to a layer, it runs. However, when the property animation
finishes, in the case of animating the position property, the layer snaps right back to its
starting position.
Remember that when animating, you use at least two objects. These objects are the layer
itself, a CALayer or CALayer -derived object, and the animation that you assign to it—the
CABasicAnimation object in our previous examples. Just because you have set a final value
(destination) for your animation object does not mean that the layer property being
animated assumes this value when the animation has finished. You must explicitly set the
Search WWH ::




Custom Search