Graphics Programs Reference
In-Depth Information
[self spinTimeLabel];
}
In TimeViewController.m , create an animation object that will spin a layer around
in one second.
- (void)spinTimeLabel
{
// Create a basic animation
CABasicAnimation *spin =
[CABasicAnimation animationWithKeyPath:@"transform.rotation"];
// fromValue is implied
[spin setToValue:[NSNumber numberWithFloat:M_PI * 2.0]];
[spin setDuration:1.0];
}
Now that you have an animation object, it needs to be applied to a layer for it to have any
effect. CALayer instances implement the method addAnimation:forKey: for this
purpose. This method takes two arguments: an animation object and a key. Once again:
this key is not the key path; it is simply a human-readable name for this animation.
In spinTimeLabel , add your animation object to timeLabel 's layer to start the an-
imation.
[spin setToValue:[NSNumber numberWithFloat:M_PI * 2.0]];
[spin setDuration:1.0];
// Kick off the animation by adding it to the layer
[[timeLabel layer] addAnimation:spin
forKey:@"spinAnimation"];
}
Build and run the application. The label field will spin 360 degrees when the user updates
it - either by switching to the Time tab or tapping the button.
Note that the animation object exists independently of the layer it is applied to. This anim-
ation object could be added to any layer to rotate it 360 degrees. You can create animation
objects and keep them around for later use in the application.
Timing functions
You may notice that the rotation of the label field's layer starts and stops suddenly; it
would look nicer if it gradually accelerated and decelerated. This sort of behavior is con-
trolled by the animation's timing function, which is an instance of the class CAMedi-
Search WWH ::




Custom Search