Graphics Reference
In-Depth Information
As the CG prefix indicates, the
CGAffineTransform
type belongs to the Core Graphics
framework. Core Graphics is a strictly 2D drawing API, and
CGAffineTransform
is
intended only for 2D transforms (that is, ones that apply only within a two-dimensional
plane).
In Chapter 3, we looked the
zPosition
property, which enables us to move layers
toward or away from the camera (the user's viewpoint). The
transform
property (which
is of type
CATransform3D
) generalizes this idea, allowing us to both move
and
rotate a
layer in three dimensions.
Like
CGAffineTransform
,
CATransform3D
is a matrix. But instead of a 2-by-3
matrix,
CATransform3D
is a 4-by-4 matrix that is capable of arbitrarily transforming a
point in 3D (see Figure 5.6).
Figure 5.6
The
CATransform3D
matrix transforming a 3D point
Core Animation provides a number of functions that can be used to create and combine
CATransform3D
matrices in exactly the same way as with
CGAffineTransform
matrices. The functions are similar to the Core Graphics equivalents, but the 3D translation
and scaling functions provide an additional
z
argument, and the rotation function accepts an
x
,
y
, and
z
argument in addition to the
angle
, which together form a vector that defines
the axis of rotation:
CATransform3DMakeRotation(
CGFloat
angle,
CGFloat
x,
CGFloat
y,
CGFloat
z)
CATransform3DMakeScale(
CGFloat
sx,
CGFloat
sy,
CGFloat
sz)
CATransform3DMakeTranslation(
Gloat
tx,
CGFloat
ty,
CGFloat
tz)
You should now be familiar with the X and Y axes, which extend to the right and down
respectively (although you may recall from Chapter 3 that this is only the case on iOS; on
Mac OS, the Y axis points upward). The Z axis is perpendicular to those axes and extends
outward toward the camera (see Figure 5.7).