Graphics Reference
In-Depth Information
The
CAReplicatorLayer
class is designed to efficiently generate collections of similar
layers. It works by drawing one or more duplicate copies of each of its sublayers, applying
a different transform to each duplicate. This is probably easier to demonstrate than to
explain, so let's construct an example.
Repeating Layers
In Listing 6.8, we create a small white square layer in the middle of the screen, then turn it
into a ring of ten layers using
CAReplicatorLayer
. The
instanceCount
property
specifies how many times the layer should be repeated. The
instanceTransform
applies a
CATransform3D
(in this case, a translation and rotation that moves the layer to
the next point in the circle).
The transform is applied incrementally, with each instance positioned relative to the
previous one. This is why the duplicates don't all end up in the same place. Figure 6.8
shows the result.
Listing 6.8
Repeating Layers Using
CAReplicatorLayer
@interface
ViewController ()
@property
(
nonatomic
,
weak
)
IBOutlet
UIView
*containerView;
@end
@implementation
ViewController
- (
void
)viewDidLoad
{
[
super
viewDidLoad
];
//create a replicator layer and add it to our view
CAReplicatorLayer
*replicator = [
CAReplicatorLayer
layer
];
replicator.
frame
=
self
.containerView.
bounds
;
[
self
.containerView.
layer
addSublayer
:replicator];
//configure the replicator
replicator.
instanceCount
=
10
;
//apply a transform for each instance
CATransform3D
transform =
CATransform3DIdentity
;
transform =
CATransform3DTranslate
(transform,
0
,
200
,
0
);
transform =
CATransform3DRotate
(transform,
M_PI
/
5.0
,
0
,
0
,
1
);