iOS layer animation - explanation

情到浓时终转凉″ 提交于 2019-12-24 06:08:06

问题


let flyRight = CABasicAnimation(keyPath: "position.x")
flyRight.toValue = view.bounds.size.width/2
flyRight.duration = 0.5
flyRight.fromValue = -view.bounds.size.width/2
flyRight.fillMode = kCAFillModeBoth
flyRight.beginTime = CACurrentMediaTime() + 3.4
password.layer.position.x = view.bounds.size.width/2
password.layer.addAnimation(flyRight, forKey: nil)

My question is: how is it possible that before I am adding the animation, I first put the layer at the end position, but when the app goes up, I don't see it there before the animation starts? Instead, the animation works after 3.4 seconds as expected.

I mean, how iOS knows that first it needs to run the animation and only then put the layer to it's final position?


回答1:


This is because there is a presentationLayer and a modelLayer so animation show and modify presentationLayer, so presentationLayer is like a ghost.

You probably know that UIView instances, as well as layer-backed NSViews, modify their layer to delegate rendering to the powerful Core Animation framework. However, it is important to understand that animations, when added to a layer, don’t modify its properties directly. Instead, Core Animation maintains two parallel layer hierarchies: the model layer tree and the presentation layer tree. Layers in the former reflect the well-known state of the layers, wheres only layers in the latter approximate the in-flight values of animations. Consider adding a fade-out animation to a view. If you, at any point during the animation, inspect the layer’s opacity value, you most likely won’t get an opacity that corresponds to what is onscreen. Instead, you need to inspect the presentation layer to get the correct result. While you may not set properties of the presentation layer directly, it can be useful to use its current values to create new animations or to interact with layers while an animation is taking place. By using -[CALayer presentationLayer] and -[CALayer modelLayer], you can switch between the two layer hierarchies with ease.

you can find more info here https://www.objc.io/issues/12-animations/animations-explained/

I hope this helps you




回答2:


It has to do with the way UI changes get applied. When you make a change to a UI object, that change is not rendered to the screen until your code returns and the event loop runs.

When that happens, the system moves your view to it's final position, but then the ACTUAL view/layer hierarchy gets covered with a new layer that's drawn on top, known as the presentation layer. The presentation layer is where the pixels from the animation show up on the screen. That's what you see.

When the animation finishes, the presentation layer is hidden/removed (not honestly sure which) and the actual layer contents are shown. At that point the layer is in it's final position, so everything looks correct.



来源:https://stackoverflow.com/questions/37819903/ios-layer-animation-explanation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!