Graphics Reference
In-Depth Information
Drawing, layout, and animation, in contrast, are concepts that apply just as much to
touchscreen devices like the iPhone and iPad as they do to their laptop and desktop cousins.
By separating out the logic for this functionality into the standalone Core Animation
framework, Apple is able to share that code between iOS and Mac OS, making things
simpler both for Apple's own OS development teams and for third-party developers who
make apps that target both platforms.
In fact, there are not two, but
four
such hierarchies, each performing a different role. In
addition to the view hierarchy and layer tree, there are the
presentation tree
and
render tree
,
which we discuss in Chapter 7, “Implicit Animations,” and Chapter 12, “Tuning for
Speed,” respectively.
So if
CALayer
is just an implementation detail of the inner workings of
UIView
, why do
we need to know about it at all? Surely Apple provides the nice, simple
UIView
interface
precisely so that we don't need to deal directly with gnarly details of Core Animation itself?
This is true to some extent. For simple purposes, we don't really need to deal directly with
CALayer
, because Apple has made it easy to leverage powerful features like animation
indirectly
via the
UIView
interface using simple high-level APIs.
But with that simplicity comes a loss of
flexibility.
If you want to do something slightly out
of the ordinary, or make use of a feature that Apple has not chosen to expose in the
UIView
class interface, you have no choice but to venture down into Core Animation to
explore the lower-level options.
We've established that layers cannot handle touch events like
UIView
can, so what can
they do that views
can't
? Here are some features of
CALayer
that are not exposed by
UIView
:
▪
Drop shadows, rounded corners, and colored borders
▪
3D transforms and positioning
▪
Nonrectangular bounds
▪
Alpha masking of content
▪
Multistep, nonlinear animations
We explore these features in the following chapters, but for now let's look at how
CALayer
can be utilized within an app.