Graphics Reference
In-Depth Information
from having to do something much more complex later on, and avoid us having to
unnecessarily create and position layer objects that we don't need.
Let's refactor our app so that layers are instantiated dynamically as the view is scrolled
instead of all being allocated in advance. That way, we can calculate whether they are
needed before ever creating them. Next, we'll add some code to calculate the visible area so
that we can eliminate layers that are outside the field of view. Listing 15.4 shows the
updated code.
Listing 15.4
Eliminating Layers Outside of the Screen Bounds
#import
"ViewController.h"
#import
<QuartzCore/QuartzCore.h>
#define WIDTH
100
#define HEIGHT
100
#define DEPTH
10
#define SIZE
100
#define SPACING
150
#define CAMERA_DISTANCE
500
#define PERSPECTIVE(z) (float)CAMERA_DISTANCE/(z + CAMERA_DISTANCE)
@interface
ViewController
() <
UIScrollViewDelegate
>
@property
(
nonatomic
,
weak
)
IBOutlet
UIScrollView
*scrollView;
@end
@implementation
ViewController
- (
void
)viewDidLoad
{
[
super
viewDidLoad
];
//set content size
self
.
scrollView
.
contentSize
=
CGSizeMake
((
WIDTH
-
1
)*
SPACING
,
(
HEIGHT
-
1
)*
SPACING
);
//set up perspective transform
CATransform3D
transform =
CATransform3DIdentity
;
transform.
m34
= -
1.0
/
CAMERA_DISTANCE
;
self
.
scrollView
.
layer
.
sublayerTransform
= transform;
}