CAReplicatorLayer

CAReplicatorLayer的目的是为了高效生成许多相似的图层。它会绘制一个或多个图层的子图层,并在每个复制体上应用不同的变换。看上去演示能够更加解释这些,我们来写个例子吧。

重复图层(Repeating Layers)

清单6.8中,我们在屏幕的中间创建了一个小白色方块图层,然后用CAReplicatorLayer生成十个图层组成一个圆圈。instanceCount属性指定了图层需要重复多少次。instanceTransform指定了一个CATransform3D3D变换(这种情况下,下一图层的位移和旋转将会移动到圆圈的下一个点)。

变换是逐步增加的,每个实例都是相对于前一实例布局。这就是为什么这些复制体最终不会出现在同意位置上,图6.8是代码运行结果。

清单6.8 用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 = ; //apply a transform for each instance
CATransform3D transform = CATransform3DIdentity;
transform = CATransform3DTranslate(transform, , , );
transform = CATransform3DRotate(transform, M_PI / 5.0, , , );
transform = CATransform3DTranslate(transform, , -, );
replicator.instanceTransform = transform; //apply a color shift for each instance
replicator.instanceBlueOffset = -0.1;
replicator.instanceGreenOffset = -0.1; //create a sublayer and place it inside the replicator
CALayer *layer = [CALayer layer];
layer.frame = CGRectMake(100.0f, 100.0f, 100.0f, 100.0f);
layer.backgroundColor = [UIColor whiteColor].CGColor;
[replicator addSublayer:layer];
}
@end

Swift 3.0 代码

 //    CAReplicatorLayer
private lazy var replicatorView: UIView! = UIView() func replicatorLayerOfView() {
self.view.addSubview(self.replicatorView)
let replicator = CAReplicatorLayer()
replicator.frame = CGRect(x: , y: , width: , height: )
self.replicatorView.layer.addSublayer(replicator) replicator.instanceCount = var transform = CATransform3DIdentity
transform = CATransform3DTranslate(transform, , , )
transform = CATransform3DRotate(transform, .pi / 5.0, , , )
transform = CATransform3DTranslate(transform, , -, )
replicator.instanceTransform = transform replicator.instanceBlueOffset = -0.1
replicator.instanceGreenOffset = -0.1 let layer = CALayer()
layer.frame = CGRect(x: , y: , width: , height: )
layer.backgroundColor = UIColor.white.cgColor
replicator.addSublayer(layer) self.replicatorView.snp.makeConstraints { (make) in
make.top.equalTo(self.gradientView)
make.size.equalTo(self.gradientView)
make.left.equalTo(self.gradientView.snp.right).inset(-)
} }

CAReplicatorLayer-LMLPHP

图6.8 用CAReplicatorLayer创建一圈图层

注意到当图层在重复的时候,他们的颜色也在变化:这是用instanceBlueOffsetinstanceGreenOffset属性实现的。通过逐步减少蓝色和绿色通道,我们逐渐将图层颜色转换成了红色。这个复制效果看起来很酷,但是CAReplicatorLayer真正应用到实际程序上的场景比如:一个游戏中导弹的轨迹云,或者粒子爆炸(尽管iOS 5已经引入了CAEmitterLayer,它更适合创建任意的粒子效果)。除此之外,还有一个实际应用是:反射。

反射

使用CAReplicatorLayer并应用一个负比例变换于一个复制图层,你就可以创建指定视图(或整个视图层次)内容的镜像图片,这样就创建了一个实时的『反射』效果。让我们来尝试实现这个创意:指定一个继承于UIViewReflectionView,它会自动产生内容的反射效果。实现这个效果的代码很简单(见清单6.9),实际上用ReflectionView实现这个效果会更简单,我们只需要把ReflectionView的实例放置于Interface Builder(见图6.9),它就会实时生成子视图的反射,而不需要别的代码(见图6.10).

清单6.9 用CAReplicatorLayer自动绘制反射

 #import "ReflectionView.h"
#import @implementation ReflectionView + (Class)layerClass
{
return [CAReplicatorLayer class];
} - (void)setUp
{
//configure replicator
CAReplicatorLayer *layer = (CAReplicatorLayer *)self.layer;
layer.instanceCount = ; //move reflection instance below original and flip vertically
CATransform3D transform = CATransform3DIdentity;
CGFloat verticalOffset = self.bounds.size.height + ;
transform = CATransform3DTranslate(transform, , verticalOffset, );
transform = CATransform3DScale(transform, , -, );
layer.instanceTransform = transform; //reduce alpha of reflection layer
layer.instanceAlphaOffset = -0.6;
}

- (id)initWithFrame:(CGRect)frame
{
//this is called when view is created in code
if ((self = [super initWithFrame:frame])) {
[self setUp];
}
return self;
} - (void)awakeFromNib
{
//this is called when view is created from a nib
[self setUp];
}
@end

CAReplicatorLayer-LMLPHP

05-06 21:09