CAReplicatorLayer 是本质上是一个继承于 CALayer 的,
它的作用是可以生成很多相似的图层,这些图层都是他的子图层
常用的属性有:
instanceCount: 拷贝图层的次数,
preservesDepth: 如果设置为 YES,图层将保持和 CATransformLayer 相同的性质和限制
instanceDelay: 后一个子图层和前一个子图层的时间间隔
instanceTransform:设置两个子图层之间的 偏移量
instanceColor: 设置层的颜色,前提是要设置子层的背景颜色,否则不会生效
instanceRedOffset、instanceGreenOffset、instanceBlueOffset:颜色的渐变,相对
于前一个层的渐变(取值-1~+1).RGB有三种颜色,所以这里也是红绿蓝三种。
instanceAlphaOffset:相对于前一个层透明图的渐变。
下面上实例:
self.view.backgroundColor = [UIColor whiteColor];
CALayer *layer = [[CALayer alloc] init];
layer.frame = CGRectMake(30, 200, 20, 80);
layer.backgroundColor = [UIColor redColor].CGColor;
[layer addAnimation:[self scaleAnimation] forKey:@"transform"];
CAReplicatorLayer *replayLayer = [[CAReplicatorLayer alloc] init];
replayLayer.instanceCount = 5;
replayLayer.instanceDelay = 0.3;
replayLayer.instanceTransform = CATransform3DMakeTranslation(50, 0, 0);
[replayLayer addSublayer:layer];
[self.view.layer addSublayer:replayLayer];
}
- (CAAnimation *)scaleAnimation
{
CABasicAnimation *basic = [[CABasicAnimation alloc] init];
basic.toValue = @0.3;
basic.duration = 1;
basic.removedOnCompletion = NO;
basic.fillMode = kCAFillModeForwards;
basic.keyPath = @"transform.scale.y";
// the object plays backwards after playing forwards
basic.autoreverses = YES;
basic.repeatCount = MAXFLOAT;
return basic;
}
效果图为: