Graphics Programs Reference
In-Depth Information
For the More Curious: The Presentation Layer and the
Model Layer
You can think of an instance of CALayer as having two parts: the content that gets com-
posited onto the screen and a set of parameters that describe how it should be composited
( opacity , transform , position , etc.). When a layer is being animated, it actually
has two copies of these parameters: the model version and the presentation version. The
presentation parameters are the ones that are being smoothly changed by the animation ob-
ject. The model parameters are the persistent ones - the ones that will be used once the an-
imation is over.
So, when a layer is being animated, its content is composited to the screen using the
presentation parameters. When it is animation-less, the model parameters are used.
Apple calls these sets of parameters the model layer and the presentation layer .
When you ask a layer for its position , you are getting the position of the model layer.
To get the presentation version, you ask for the presentationLayer first.
CGPoint whereIsItWhenAnimationStops = [layer position];
CGPoint whereIsItNow = [[layer presentationLayer] position];
Why is this useful? Imagine a game that has animating objects on the screen, and if the user
taps one of the objects, it blows up. Only the presentation layer knows where the object
currently is on the screen, which you must know in order to judge the accuracy of the
user's tap.
In this chapter, our examples have had the animated objects return to their original states
after the animation is complete. Often, however, you want to animate an object to a state
and then have it stay there once the animation is over. To do this, you must keep the
presentation and model layers clear in your mind. Not doing so leads to two common er-
rors:
Your animation goes great, but at the end it snaps back to its initial position (or
opacity or whatever) when you wanted it to remain where it was. This happens be-
cause you forgot to update the model parameters to match the final state of your
animation. Try using an explicit animation in touchesBegan:withEvent:
method in HypnosisView.m . (Also comment out
touchesMoved:withEvent: .)
Search WWH ::




Custom Search