Graphics Programs Reference
In-Depth Information
In TimeViewController.m , replace the code that spins the label in showCur-
rentTime: with a call to bounceTimeLabel .
- (IBAction)showCurrentTime:(id)sender
{
NSDate *now = [NSDate date];
static NSDateFormatter *formatter = nil;
if (!formatter) {
formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
}
[timeLabel setText:[formatter stringFromDate:now]];
[self spinTimeLabel];
[self bounceTimeLabel];
}
Then, implement bounceTimeLabel in TimeViewController.m .
- (void)bounceTimeLabel
{
// Create a key frame animation
CAKeyframeAnimation *bounce =
[CAKeyframeAnimation animationWithKeyPath:@"transform"];
// Create the values it will pass through
CATransform3D forward = CATransform3DMakeScale(1.3, 1.3, 1);
CATransform3D back = CATransform3DMakeScale(0.7, 0.7, 1);
CATransform3D forward2 = CATransform3DMakeScale(1.2, 1.2, 1);
CATransform3D back2 = CATransform3DMakeScale(0.9, 0.9, 1);
[bounce setValues:[NSArray arrayWithObjects:
[NSValue valueWithCATransform3D:CATransform3DIdentity],
[NSValue valueWithCATransform3D:forward],
[NSValue valueWithCATransform3D:back],
[NSValue valueWithCATransform3D:forward2],
[NSValue valueWithCATransform3D:back2],
[NSValue valueWithCATransform3D:CATransform3DIdentity],
nil]];
// Set the duration
[bounce setDuration:0.6];
// Animate the layer
[[timeLabel layer] addAnimation:bounce
forKey:@"bounceAnimation"];
}
Build and run the application. The time field should now scale up and down and up and
down when it is updated. The constant CATransform3DIdentity is the identity mat-
rix . When the transform of a layer is the identity matrix, no scaling, rotation, or translation
is applied to the layer: it sits squarely within its bounding box at its position. So, this an-
imation starts at no transformation, scales a few times, and then reverts back to no trans-
formation.
Search WWH ::




Custom Search