Why do docs indicate that CALayer animations should be in UIView animation blocks?

I am currently reading the Apple Core Animation Guide , where I found the following excerpt regarding layer-based views in iOS:

If you want to use the Core Animation classes to start the animation, you must issue all of your Core Animation calls from within the view-based animation block. The UIView class disables layer animation by default, but restores them inside animation blocks. Therefore, any changes you make outside the animation block are not animated.

At the bottom of the citation, the documentation includes the following list of codes:

[UIView animateWithDuration:1.0 animations:^{ // Change the opacity implicitly. myView.layer.opacity = 0.0; // Change the position explicitly. CABasicAnimation* theAnim = [CABasicAnimation animationWithKeyPath:@"position"]; theAnim.fromValue = [NSValue valueWithCGPoint:myView.layer.position]; theAnim.toValue = [NSValue valueWithCGPoint:myNewPosition]; theAnim.duration = 3.0; [myView.layer addAnimation:theAnim forKey:@"AnimateFrame"]; }]; 

which implies that both implicit and explicit animations in CALayer support for UIView must be placed in the animation block.

However, I found that this was clearly not true. In particular, I have successfully implemented explicit animations using the Core Animation classes outside of the UIView animation UIView .

Did I misunderstand this passage, or is it obsolete or something else?


Some additional notes:

I assume that the "UIView class disables the animation of layers by default, but restores them inside animation blocks" refers to the +[UIView setAnimationsEnabled:] method. When I get back to a computer that can do this, I will check if +[UIView areAnimationsEnabled] YES or NO .

+6
source share
1 answer

This quote refers to a layer that supports the view. This does not apply to the autonomous layers that you create and manage yourself.


Each view of iOS is supported by a layer. When you change the properties of a view, it changes the property of the bottom layer. By default, the layer will have implicit animations, but the layer “disables” this behavior, except when you are inside the UIView animation block. This is what this piece of documentation says.

There are several ways to use Core Animation to animate a layer property. The most common is adding an animation object to the layer when you want to animate a property, but you can also create settings through the actions dictionary and styles dictionary if you always want to animate when a property changes. The last two will also be disabled for the layer that supports the view.

+4
source

Source: https://habr.com/ru/post/981416/


All Articles