Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to prevent disappearing layer in CABasicAnimation?

I'm animating a CALayer using CABasicAnimation and expecting the layer to remain in position when the animation completes. Nearly all solutions show the following, which works fine

animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;

However, the 2010 WWDC speaker in the Core Animation in Practice Part 1 (~38-41mins) says that most of the solutions to the disappearing layer found are "bogus" and the correct way to animate a layer is roughly the following

animation.fromValue = [NSNumber numberWithFloat:layer.position.y];
layer.position = CGPointMake(layer.position.y, endPoint); 
animation.toValue = [NSNumber numberWithFloat:endPoint];

The reason given is that the removedOnCompletion/fillMode solution only freezes the presentation layer, and the actual layer still has its original position set.

Please correct me if I've misinterpreted the speaker.

If I have understood him correctly, when does it matter?

Thanks, Steve

like image 561
Steve Avatar asked Oct 21 '22 11:10

Steve


2 Answers

It depends on what you want to do... Your own explanation seems pretty clear.

If you only animate properties without changing the actual value, then "what you see" is actually different from "what is". Most of the time, you don't want that. (And when you use UIView animations, or CALayer implicit animations, "what is" and "what you see" are synchronized, which is consistent with the direct setter syntax).

But in some cases, it can be useful to preserve the actual value, and temporary display something different on screen... as long as you know what you're doing!

like image 98
Guillaume Avatar answered Oct 30 '22 14:10

Guillaume


You understand it right.

It means that when you do an animation on a layer a "special" layer called "presentation layer" is created and the animation actually took place on it, but the actual layer isn't changed. In fact you MUST set the property to the layer at the end of the animation to "syncronize" the layer with the final animation status.

like image 27
il Malvagio Dottor Prosciutto Avatar answered Oct 30 '22 15:10

il Malvagio Dottor Prosciutto