Graphics Reference
In-Depth Information
Presentation Versus Model
The behavior of properties on a CALayer is unusual, in that changing a layer property
does not have an immediate effect, but gradually updates over time. How does that work?
When you change a property of a layer, the property value is actually updated immediately
(if you try to read it, you'll find that the value is whatever you just set it to), but that change
is not reflected onscreen. That's because the property you set doesn't adjust the appearance
of the layer directly; instead, it defines the appearance that the layer is going to have when
that property's animation has completed.
When you set the properties of a CALayer , you are really defining a model for how you
want the display to look at the end of the current transaction. Core Animation then acts as a
controller and takes responsibility for updating the view state of these properties onscreen
based on the layer actions and transaction settings.
What we are talking about is effectively the MVC pattern in miniature. CALayer is a
visual class that you would normally associate with the user interface (aka view ) part of the
MVC (Model-View-Controller) pattern, but in the context of the user interface itself,
CALayer behaves more like a model for how the view is going to look when all
animations have completed. In fact, in Apple's own documentation, the layer tree is
sometimes referred to as the model layer tree.
In iOS, the screen is redrawn 60 times per second. If the animation duration is longer than
one 60 th of a second, Core Animation is therefore required to recomposite the layer onto the
screen multiple times between when you set the new value for an animatable property and
when that new value is eventually reflected onscreen. This implies that CALayer must
somehow maintain a record of the current display value of the property in addition to its
“actual” value (the value that you've set it to).
The display values of each layer's properties are stored in a separate layer called the
presentation layer , which is accessed via the -presentationLayer method. The
presentation layer is essentially a duplicate of the model layer, except that its property
values always represent the current appearance at any given point in time. In other words,
you can access a property of the presentation layer to find out the current onscreen value of
the equivalent model layer property (see Figure 7.4).
We mentioned in Chapter 1 that in addition to the layer tree there is a presentation tree. The
presentation tree is the tree formed by the presentation layers of all the layers in the layer
tree. Note that the presentation layer is only created when a layer is first committed (that is,
when it's first displayed onscreen), so attempting to call -presentationLayer before
then will return nil .
You may notice that there is also a -modelLayer method. Calling -modelLayer on a
presentation layer will return the underlying CALayer that it is presenting. Calling
-modelLayer on a regular layer just returns -self . (We already established that
ordinary layers are in fact a type of model.)
 
Search WWH ::




Custom Search