Graphics Reference
In-Depth Information
CGFontRelease
(fontRef);
}
@end
If you run the sample code, you'll notice that the text isn't pixelated even though we aren't
setting the
contentsScale
anywhere. Another benefit of implementing
CATextLayer
as a backing layer is that its
contentsScale
is automatically set by the view.
In this simple example, we've only implemented a few of the styling and layout properties
of
UILabel
, but with a bit more work we could create a
LayerLabel
class that supports
the full functionality of
UILabel
and more (you will find several such classes already
available as open source projects online).
If you only intend to support iOS 6 and above, a
CATextLayer
-based label may be of
limited use. But in general, using +
layerClass
to create views backed by different layer
types is a clean and reusable way to utilize
CALayer
subclasses in your apps.
When constructing complex objects in 3D, it is convenient to be able to organize the
individual elements hierarchically. For example, suppose you were making an arm: You
would want the hand to be a child of the wrist, which would be a child of the forearm,
which would be a child of the elbow, which would be a child of the upper arm, which
would be a child of the shoulder, and so on.
The reason for this is that it allows you to move each section independently. Pivoting the
elbow would move the lower arm and hand but not the shoulder. Core Animation layers
easily allow for this kind of hierarchical arrangement in
2D,
but in 3D it's not possible
because each layer flattens its children into a single plane (as explained in Chapter 5,
“Transforms”).
CATransformLayer
solves this problem. A
CATransformLayer
is unlike a regular
CALayer
in that it cannot display any content of its own; it exists only to host a transform
that can be applied to its sublayers.
CATransformLayer
does not flatten its sublayers,
so it can be used to construct a hierarchical 3D structure, such as our arm example.
Creating an arm programmatically would require rather a lot of code, so we'll demonstrate
this with something a bit simpler: In the cube example in Chapter 5, we worked around the
layer-flattening problem by rotating the
camera
instead of the cube by using the
sublayerTransform
of the containing layer. This is a neat trick, but only works for a
single object. If our scene contained two cubes, we would not be able to rotate them
independently using this technique.