Graphics Reference
In-Depth Information
which is more suited to creating arbitrary particle effects). There is another more practical
use however: reflections.
Reflections
By using
CAReplicatorLayer
to apply a transform with a negative scale factor to a
single duplicate layer, you can create a mirror image of the contents of a given view (or an
entire view hierarchy), creating a real-time “reflection” effect.
Let's try implementing this idea in the form of a reusable
UIView
subclass called
ReflectionView
that will automatically generate a reflection of its contents. The code
to create this is very simple (see Listing 6.9), and actually using the
ReflectionView
is
even simpler; we can just drop an instance of our
ReflectionView
into Interface
Builder (see Figure 6.9), and it will generate a reflection of its subviews at runtime without
the need to add any setup code to the view controller (see Figure 6.10).
Listing 6.9
Automatically Drawing a Reflection with
CAReplicatorLayer
#import
"ReflectionView.h"
#import
<QuartzCore/QuartzCore.h>
@implementation
ReflectionView
+ (
Class
)layerClass
{
return
[
CAReplicatorLayer
class
];
}
- (
void
)setUp
{
//configure replicator
CAReplicatorLayer
*layer = (
CAReplicatorLayer
*)
self
.
layer
;
layer.
instanceCount
=
2
;
//move reflection instance below original and flip vertically
CATransform3D
transform =
CATransform3DIdentity
;
CGFloat
verticalOffset =
self
.
bounds
.
size
.
height
+
2
;
transform =
CATransform3DTranslate
(transform,
0
, verticalOffset,
0
);
transform =
CATransform3DScale
(transform,
1
, -
1
,
0
);
layer.
instanceTransform
= transform;
//reduce alpha of reflection layer
layer.
instanceAlphaOffset
= -
0.6
;
}