Graphics Reference
In-Depth Information
Figure 2.12
A layer with backing image drawn using
CALayerDelegate
Note a couple of interesting things here:
▪
We have to manually call
-display
on
blueLayer
to force it to be updated.
Unlike
UIView
,
CALayer
does not redraw its contents automatically when it
appears onscreen; it is left to the discretion of the developer to decide when the layer
needs redrawing.
▪
The circle that we have drawn is clipped to the layer
bounds
even though we have
not enabled the
masksToBounds
property. That's because when you draw the
backing image using the
CALayerDelegate
, the
CALayer
creates a drawing
context with the exact dimensions of the layer. There is no provision made for
drawing that spills outside of those bounds.
So now you understand the
CALayerDelegate
and how to use it. But unless you are
creating standalone layers, you will almost never need to implement the
CALayerDelegate
protocol. The reason for this is that when
UIView
creates its
backing layer, it automatically sets itself as the layer's
delegate
and provides an
implementation for
-displayLayer:
that abstracts these issues away.
When using view-backing layers, you do not need to implement
-displayLayer:
or
-drawLayer:inContext:
to draw into your layer's backing image; you can just
implement the
-drawRect:
method of
UIView
in the normal fashion, and
UIView
takes care of everything, including automatically calling
-display
on the layer when it
needs to be redrawn.