Understanding CoreAnimation: UIView Animation Mechanics, PresentationLayer and ModelLayer
This article explains how iOS CoreAnimation works behind UIView animations, detailing the role of CALayer, the actionForLayer:forKey: delegate method, animation overlapping behavior, and the distinction between presentationLayer and modelLayer during screen refreshes.
CoreAnimation is Apple’s drawing‑based animation framework. For basic graphics knowledge you can refer to the earlier articles "CoreAnimation Introduction (1) – Graphics Basics" and "CoreAnimation Introduction (2) – First Look at CALayer and Animations", as well as the team’s "CoreAnimation Overview".
1. UIView Animation Implementation Principle
UIView provides a set of UIViewAnimationWithBlocks methods; you place code that changes animatable properties inside the animations block to trigger an animation. An example is shown (image omitted).
The effect is displayed (GIF omitted). The UIView object holds a CALayer, which actually performs the animation; UIView merely wraps this layer. By creating a custom MyTestLayer subclass of CALayer and overriding its setters, and a MyTestView subclass of UIView that overrides +layerClass to return MyTestLayer , you can observe the call order when setting bounds on the view.
When the view’s setBounds is called, it forwards the call to the layer’s setBounds ; the same applies to getters. Changing a layer property invokes the delegate method actionForLayer:forKey: to obtain an animation object. The view acts as the delegate of its layer.
The method returns an object conforming to CAAction , usually an animation. If it returns nil , the default implicit animation runs; if it returns NSNull , no animation occurs. Overriding this method to return nil demonstrates that even without explicit animation code, changing a property still triggers a default 0.25 s implicit animation.
When two animations overlap on the same property, they are additive rather than interruptive. For example, a 3‑second bounds animation followed by a 1‑second bounds animation results in the second animation starting from the current displayed state, causing the view to briefly shrink below its target size before both animations finish and the view settles at the final model value.
2. PresentationLayer and ModelLayer
UIView animations are performed by the underlying CALayer. By changing a view’s bounds inside an animation block and printing the view’s and layer’s bounds, you see both become the target value (e.g., (200, 200)). The question is how the animation is still visible.
CALayer provides two instance methods: presentationLayer and modelLayer . presentationLayer represents the visual state currently displayed on screen, while modelLayer is the real state of the layer.
Printing layer , layer.presentationLayer , layer.modelLayer , and layer.presentationLayer.modelLayer shortly after starting an animation shows that presentationLayer holds the interpolated values along the animation path, whereas modelLayer (and presentationLayer.modelLayer ) refer to the layer itself.
Thus, during an animation the system repeatedly reads values from presentationLayer to render each frame. When the animation is removed, rendering falls back to modelLayer . This explains why a CABasicAnimation with a specified fromValue appears to return to the start after finishing—the visual layer is still showing the presentation state.
If you set the layer’s final value before adding the animation, there is no flash at the end because the visual output continues to come from presentationLayer until the next screen refresh, at which point it adopts the model value.
Summary
UIView holds a CALayer that performs the actual animation; the view acts as the layer’s delegate. Changing a view’s property changes the underlying layer’s property, which triggers actionForLayer:forKey: to decide whether to animate.
Overlapping animations on the same property start from the current displayed state and ultimately settle at the model layer’s real state.
CALayer maintains a presentationLayer (the on‑screen animated state) and a modelLayer (the true state). The presentation layer updates on each screen refresh, using animation values when present, otherwise falling back to the model layer.
Demo code repository: https://github.com/Shizq5509/CADemo
References
Understanding the relationship between UIView animations and Core Animation – link
iOS CoreAnimation series – link
Advanced iOS Core Animation techniques – link
iOS Animation Development series – link
Qunar Tech Salon
Qunar Tech Salon is a learning and exchange platform for Qunar engineers and industry peers. We share cutting-edge technology trends and topics, providing a free platform for mid-to-senior technical professionals to exchange and learn.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.