Question

Guys I'm trying to perform a CABasicAnimation (just for the test purposes) following the Apple's guide

There's a piece of code:

CABasicAnimation* fadeAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];
fadeAnim.fromValue = [NSNumber numberWithFloat:1.0];
fadeAnim.toValue = [NSNumber numberWithFloat:0.0];
fadeAnim.duration = 5.0;
[theLayer addAnimation:fadeAnim forKey:@"opacity"];

// Change the actual data value in the layer to the final value.
theLayer.opacity = 0.0;

Which tells that I should actually change a property at the end. But it seems not working fine(it changes opacity immediately) - the duration is not 5 (I changed it to 5 for better visibility) so the animation is not CABasicAnimation but implicit. It only works when I set theLayer.opacity = 0.0; BEFORE I add the animation to layer. Am I doing something wrong or it is a bug in docs? P.S running the latest XCode, iOS 7.1 simulator.

Was it helpful?

Solution

Update the model layer before adding the animation.

CABasicAnimation* fadeAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];
fadeAnim.fromValue = [NSNumber numberWithFloat:1.0];
fadeAnim.toValue = [NSNumber numberWithFloat:0.0];
fadeAnim.duration = 5.0;

// Change the actual data value in the layer to the final value.
theLayer.opacity = 0.0;

[theLayer addAnimation:fadeAnim forKey:@"opacity"];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top