Graphics Reference
In-Depth Information
Figure 7.4 How model relates to presentation for a moving layer
Most of the time, you do not need to access the presentation layer directly; you can just
interact with the properties of the model layer and let Core Animation take care of updating
the display. Two cases where the presentation layer does become useful are for
synchronizing animations and for handling user interaction:
If you are implementing timer-based animations (see Chapter 11, “Timer-Based
Animation”) in addition to ordinary transaction-based animations, it can be useful to
be able to find out exactly where a given layer appears to be onscreen at a given point
in time so that you can position other elements to correctly align with the animation.
If you want your animated layers to respond to user input, and you are using the
-hitTest: method (see Chapter 3, “Layer Geometry”) to determine whether a
given layer is being touched, it makes sense to call -hitTest: against the
presentation layer rather than the model layer because that represents the layer's
position as the user currently sees it, not as it will be when the current animation has
finished.
We can demonstrate the latter case with a simple example (see Listing 7.7). In the example,
tapping anywhere onscreen animates the layer to the position you've touched. Tapping on
the layer itself sets its color to a random value. We determine whether the tap is inside the
layer by calling the -hitTest: method on the layer's presentation layer.
If you modify the code so that -hitTest: is called directly on the colorLayer instead
of its presentation layer, you will see that it doesn't work correctly when the layer is
moving. Instead of tapping the layer, you now have to tap the location that the layer is
moving toward for it to register the hit (which is why we used the presentation layer for hit
testing originally).
Search WWH ::




Custom Search