Graphics Reference
In-Depth Information
LISTING 3-12
Continued
[positionAnimation
setFromValue
:
[
NSValue
valueWithPoint:
NSPointFromCGPoint([layer
position
])]];
[positionAnimation setToValue:
[
NSValue
valueWithPoint:NSMakePoint(0.0, 0.0)]];
[positionAnimation
setDuration
:5.0f];
CABasicAnimation
*borderWidthAnimation =
[
CABasicAnimation animationWithKeyPath
:
@”borderWidth”
];
[borderWidthAnimation
setFromValue
:[
NSNumber numberWithFloat
:5.0f]];
[borderWidthAnimation setToValue:[
NSNumber numberWithFloat
:30.0f]];
[borderWidthAnimation
setDuration
:5.0f];
[layer
addAnimation
:boundsAnimation
forKey
:
@”bounds”
];
[layer
addAnimation
:positionAnimation
forKey
:
@”position”
];
[layer
addAnimation
:borderWidthAnimation
forKey
:
@”borderWidth”
];
}
Each animation has a duration of 5 seconds, and they begin to play back simultaneously
in the next run loop and they end at the same time. The position of the layer moves to
the bottom left corner, the border width grows to 30 pixels, and size of the layer grows
from 100 × 100 pixels to 300 × 300 pixels.
Let's say that we would prefer that, rather than having all our animations play simultane-
ously, we want them to play back sequentially—one following the previous. We can
achieve this by using a group animation and setting the
beginTime
field. I should
mention now that in this case it might make more sense to use a keyframe animation
instead, but you need to read Chapter 4, “Keyframe Animation,” to see how that works.
We must explicitly specify the duration of our animation group so that the time for each
individual animation can be split up accordingly. In our example, we set our duration of
the animation group to last 15 seconds and get each of our individual animations to play
back for 5 seconds. Listing 3-13 shows how we can take a previous example and instead
use animation grouping for greater control over animation playback.
LISTING 3-13
Using Animation Grouping
- (
IBAction
)animate:(
id
)sender;
{
NSRect oldRect = NSMakeRect(0.0, 0.0, 100.0, 100.0);
NSRect newRect = NSMakeRect(0.0, 0.0, 300.0, 300.0);
CABasicAnimation
*boundsAnimation =
[
CABasicAnimation animationWithKeyPath
:
@”bounds”
];