Graphics Reference
In-Depth Information
As of Mac OS X 10.6 Snow Leopard and iPhone OS 3.0, these additional layers are also
available:
.
CAShapeLayer
This layer enables you to create an arbitrary shape based upon a path that you
define. We cover this layer in detail in Chapter 10, “Other Useful Layers.”
.
CAGradientLayer
This layer provides a convenient way to display a linear gradient. You can define
multiple colors and stops at which points the display shifts to the different colors
you have specified. We cover this layer in detail in Chapter 10.
.
CAReplicatorLayer
This layer replicates the sublayers you add to it according to the parameters you
specify. We cover this layer in detail in Chapter 10.
What's a Layer For?
Layers provide a discreet component or building block for creating complex animations.
There are actually two basic categories for animation:
.
View animation, which is primarily used when you want to give your user interface
some visual cues.
.
Layer animation, which is more commonly used for application content and func-
tionality.
You will at times want to use layers for your user interface. However, it is not as common
to use it that way because layers do not receive events such as clicks and key presses. You
need to capture the event at the backing view level and then pass it on to the layer if you
want to do something with that information in the layer.
Imagine a Space Invaders game for the iPhone. You create a ship sprite that moves along
the bottom of the view when you tilt the device side to side. This sprite is drawn on its
own layer. When an accelerometer event is received in the backing view, you pass along
the event to the layer and call -setPosition on the layer to move it where it should be
according to the accelerometer event. You might also want to receive tap events, so you
can fire the ship's photon torpedoes at the invaders; these are also sprites, each in their
own discreet layers.
You can see how quickly the number of layers you would need for a game would accumu-
late. Layers are the right choice in this kind of application as they are lightweight and
perform well. Views also perform well, and you could certainly use them instead of layers;
however, you gain performance when using layers over views when the number of items
(either layers or views) on the screen reaches a certain count. This is less of an issue on
OS X than on the iPhone of course, but keeping your application lean and efficient is
always a good idea.
Search WWH ::




Custom Search