Graphics Reference
In-Depth Information
@implementation
ViewController
- (
void
)addFace:(
NSInteger
)index withTransform:(
CATransform3D
)transform
{
//get the face view and add it to the container
UIView
*face =
self
.faces[index];
[
self
.containerView
addSubview
:face];
//center the face view within the container
CGSize
containerSize =
self
.containerView.
bounds
.
size
;
face.
center
=
CGPointMake
(containerSize.
width
/
2.0
,
containerSize.
height
/
2.0
);
//apply the transform
face.
layer
.
transform
= transform;
}
- (
void
)viewDidLoad
{
[
super
viewDidLoad
];
//set up the container sublayer transform
CATransform3D
perspective =
CATransform3DIdentity
;
perspective.
m34
= -
1.0
/
500.0
;
self
.containerView.
layer
.
sublayerTransform
= perspective;
//add cube face 1
CATransform3D
transform =
CATransform3DMakeTranslation
(
0
,
0
,
100
);
[
self
addFace
:
0
withTransform
:transform];
//add cube face 2
transform =
CATransform3DMakeTranslation
(
100
,
0
,
0
);
transform =
CATransform3DRotate
(transform,
M_PI_2
,
0
,
1
,
0
);
[
self
addFace
:
1
withTransform
:transform];
//add cube face 3
transform =
CATransform3DMakeTranslation
(
0
, -
100
,
0
);
transform =
CATransform3DRotate
(transform,
M_PI_2
,
1
,
0
,
0
);
[
self
addFace
:
2
withTransform
:transform];
//add cube face 4
transform =
CATransform3DMakeTranslation
(
0
,
100
,
0
);
transform =
CATransform3DRotate
(transform, -
M_PI_2
,
1
,
0
,
0
);
[
self
addFace
:
3
withTransform
:transform];