Graphics Reference
In-Depth Information
Note About Centering the Layer
We could call
-setFrame
on the layer before adding it to the root layer of our window's
contentView
layer tree. However, we have decided instead to set the
bounds
of the layer
first and then set the
position.
(Remember
frame
is a derived value based on
position
,
bounds
,
anchorPoint
, and
transform.
) Setting the
bounds
and
position
properties like
this makes it simpler to center the layer in the containing view. We simply obtain the parent
view's width and divide it in half, and then we take the parent view's height and divide it in
half. We then call
-setPosition
on the layer, which perfectly centers our layer in the
contentView
. This works because the layer's
anchorPoint
defaults to 0.5,0.5—the center
of the containing view. If we were to change the
anchorPoint
to 0.0, 0.0 the bottom left of
the layer would then display at the center of the
contentView
. Figure 3-1 shows the values
for the different anchor points you can use on your layer.
(1.0,1.0)
(0.0,0.5)
(0.5,0.5)
(1.0,0.0)
(0.0,0.0)
FIGURE 3-1
Layer Anchor Points
Layer Allocation Considerations
Another consideration of which you should be aware when you set up your layers is that
even though you have an instance variable (ivar) of your
CALayer
, it is not retained unless
you explicitly retain it. In the world of memory management in Objective-C, the rule of
thumb is that you retain only that which you need to retain. You should not retain
objects you don't need to hold onto, and you should retain objects that you do need.