Graphics Reference
In-Depth Information
LISTING 5-3
Continued
CABasicAnimation
*animation =
nil
;
CATransform3D
transform;
[[
self
workLayer
]
removeAllAnimations
];
animation = [CABasicAnimation animationWithKeyPath:
@”transform”
];
transform =
CATransform3DMakeRotation
(1.57f, 0.0f, 0.0f, 1.0f);
value = [NSValue valueWithCATransform3D:transform];
[animation
setToValue
:value];
transform =
CATransform3DMakeRotation
(0.0f, 0.0f, 0.0f, 1.0f);
value = [NSValue valueWithCATransform3D:transform];
[animation
setFromValue
:value];
[animation
setAutoreverses
:
YES
];
[animation
setDuration
:1.0f];
[animation
setRepeatCount
:100];
[workLayer addAnimation:animation forKey:
kScaleKey
];
}
In this example,
CATransform3DMakeRotation
is applied
to the single axis rotation to the layer.
Unlike the transform method shown in
Listing 5-2, this example uses four para-
meters: the first parameter is the angle,
expressed in radians, and the next three
are the
x
-,
y
-, and
z
-axes. The layer is
rotated by 1.57 radians (that is, 90
degrees) on the
x
-axis. The values for the
x
-,
y
-, and
z
-axes are a bit unusual. These
values refer to the
magnitude
of the rota-
tion and accept a value between
-1.0
and
1.0
. The rotation is set to be a full
positive magnitude along the
z
-axis,
which produces what appears to be a 2D
rotation in a clockwise direction.
NOTE
What you see when we walk through these
examples is that they all follow the same
basic flow. We build a
CATransform3D
using
one of the numerous foundation methods
and then apply it to the layer.
NOTE
The value 1.57 used in Listing 5-2 repre-
sents the radian value of the angle. To trans-
late degrees into radians, use the formula
X
π
/180. For example:
90
π
/ 180 = 45(3.1415) / 180 = 0.7853
The next example takes the previous one a step further by rotating the layer along two
axes; see Listing 5-4.