Graphics Reference
In-Depth Information
kCAGravityCenter
kCAGravityTop
kCAGravityBottom
kCAGravityLeft
kCAGravityRight
kCAGravityTopLeft
kCAGravityTopRight
kCAGravityBottomLeft
kCAGravityBottomRight
kCAGravityResize
kCAGravityResizeAspect
kCAGravityResizeAspectFill
Like
contentMode
, the purpose of
contentsGravity
is to determine how content
should be aligned within the layer bounds. We will use
kCAGravityResizeAspect
,
which equates to
UIViewContentModeScaleAspectFit
, and has the effect of
scaling the image to fit the layer bounds without distorting its aspect ratio:
self
.
layerView
.layer.contentsGravity = kCAGravityResizeAspect;
Figure 2.2 shows the results.
Figure 2.2
The snowman image displayed with the correct
contentsGravity
contentsScale
The
contentsScale
property defines a ratio between the pixel dimensions of the layer's
backing image and the size of the view. It's a floating-point value that defaults to 1.0.
The purpose of the
contentsScale
property is not immediately obvious. It doesn't
always have the effect of scaling the backing image onscreen; if you try setting it to various
values in our snowman example, you'll see it has no effect because the
contents
image
is already being scaled to fit the layer bounds by the
contentsGravity
property.