Graphics Reference
In-Depth Information
Figure 5.9
The
m34
value of
CATransform3D
, used for perspective
By default,
m34
has a value of zero. We can apply perspective to our scene by setting the
m34
property of our transform to -1.0 /
d
, where
d
is the distance between the imaginary
camera and the screen, measured in points. How do we calculate what this distance should
be? We don't have to; we can just
make something up
.
Because the camera doesn't really exist, we are free to decide where it is positioned based
on what looks good in our scene. A value between 500 and 1000 usually works fairly well,
but you may find that smaller or larger values look better for a given layer arrangement.
Decreasing the distance value increases the perspective effect, so a very small value will
look extremely distorted, and a very large value will just look like there is no perspective at
all (isometric). Listing 5.5 shows the code to apply perspective to our view, and Figure 5.10
shows the result.
Listing 5.5
Applying Perspective to the Transform
@implementation
ViewController
- (
void
)viewDidLoad
{
[
super
viewDidLoad
];
//create a new transform
CATransform3D
transform =
CATransform3DIdentity
;
//apply perspective
transform.
m34
= -
1.0
/
500.0
;
//rotate by 45 degrees along the Y axis
transform =
CATransform3DRotate
(transform,
M_PI_4
,
0
,
1
,
0
);
//apply to layer
self
.
layerView
.
layer
.
transform
= transform;