Graphics Programs Reference
In-Depth Information
Here's an example of an animation object that acts on a layer's opacity property.
// Create an animation that will change the opacity of a layer
CABasicAnimation *fader = [CABasicAnimation animationWithKeyPath:@"opacity"];
// It will last 2 seconds
[fader setDuration:2.0];
// The layer's opacity will start at 1.0 (completely opaque)
[fader setFromValue:[NSNumber numberWithFloat:1.0]];
// And will end at 0.0 (completely transparent)
[fader setToValue:[NSNumber numberWithFloat:0.0]];
// Add it to the layer
[rexLayer addAnimation:fader forKey:@"BigFade"];
The key, “BigFade” in this case, is ignored by the system. However, you could use it to
access the animation later if, for example, you needed to cancel it mid-fade.
In this code, the fromValue and toValue take NSNumber s as arguments. The type of
these properties however, is id because animation objects need to be able to support dif-
ferent data types. For example, an animation that changes the position of a layer
would need values that are of type CGPoint .
You can't just pass any object for any property; CABasicAnimation expects the ap-
propriate object based on the key path. For scalar values, like opacity , you can wrap a
number in an NSNumber instance. For properties represented by structures, like posi-
tion , you wrap the structures in instances of NSValue .
CABasicAnimation *mover = [CABasicAnimation animationWithKeyPath:@"position"];
[mover setDuration:1.0];
[mover setFromValue:[NSValue valueWithCGPoint:CGPointMake(0.0, 100.0)]];
[mover setToValue:[NSValue valueWithCGPoint:CGPointMake(100.0, 100.0)]];
Figure 23.4 CAKeyframeAnimation
 
Search WWH ::




Custom Search