Graphics Reference
In-Depth Information
subclassing
UIView
is so we can override the
+layerClass
method and dictate what kind
of layer this
UIView
will be backed with, as shown in Listing 10-6.
LISTING 10-6
Overriding the +layerClass Method
+ (
Class
)layerClass
{
return
[
CAReplicatorLayer class
];
}
This class method is called whenever an instance of the
ReplicatorView
is initialized.
Instead of having a
CALayer
backing the view, we will automatically have
CAReplicatorLayer
as the backing layer.
Because we are subclassing the
UIView
, add the setup code shown in Listing 10-7 to the
-initWithFrame:
method of the
ReplicatorView
class. This setup code tells
CAReplicatorLayer
what to do with the sublayers (and subviews) it is about to receive.
LISTING 10-7
Overriding the +layerClass Method
- (
id
)initWithFrame:(
CGRect
)frame
{
if
(!(
self
= [
super
initWithFrame
:frame]))
return nil
;
CGFloat
reflectionBase = 230.0f;
CATransform3D
transform =
CATransform3DMakeScale
(1.0, -1.0, 1.0);
transform =
CATransform3DTranslate
(transform, 0.0, frame.
size
.
height
- 2.0 * reflectionBase, 0.0);
CAReplicatorLayer
*replicatorLayer = (
CAReplicatorLayer
*)[
self
layer];
[replicatorLayer
setInstanceTransform
:transform];
[replicatorLayer
setInstanceRedOffset
:-0.5];
[replicatorLayer
setInstanceGreenOffset
:-0.5];
[replicatorLayer
setInstanceBlueOffset
:-0.5];
[replicatorLayer
setInstanceAlphaOffset
:-0.45];
[replicatorLayer
setInstanceCount
:2];
return self
;
}
After calling
super
, a transform is used to both flip the layer it is associated with and to
shift that layer down 230 pixels. The reason for shifting the layer down 230 pixels is
because we know the
ReplicatorView
will be 220 pixels high, so we want to position the
layer 10 pixels below the top of its parent view.