Here is one of the two challenges at the end of this chapter. It’s pretty simple, really. It looks just like the code implemented for the bounce effect, except that you can just work with floats instead of working with matrices.
[code] // Animate the layer
[[timeLabel layer] addAnimation:bounce
// CHALLENGE CAKeyframeAnimation *fader = [CAKeyframeAnimation animationWithKeyPath:@"opacity"]; // Create our floats float forwardOpacity = 0.9; float backOpacity = 0.2; float forwardOpacity2 = 0.7; float backOpacity2 = 0.4; [fader setValues:[NSArray arrayWithObjects: [NSNumber numberWithFloat:1.0], [NSNumber numberWithFloat:forwardOpacity], [NSNumber numberWithFloat:backOpacity], [NSNumber numberWithFloat:forwardOpacity2], [NSNumber numberWithFloat:backOpacity2], [NSNumber numberWithFloat:1.0], nil]]; // Set the duration [fader setDuration:0.6]; // Animate the layer [[timeLabel layer] addAnimation:fader forKey:@"faderAnimation"];[/code]
So the two forward opacities and the two backward opacities should be approximately in line with the bounce effects. Also, obviously, we want it to start and stop at a fully visible state (opacity = 1.0), and we want the duration to be the same as the bounce effect. Voila.