CAReplicatorLayer能复制子层
。
也就是说,想要复制层,必须先让这个层成为CAReplicatorLayer的子层sublayer。
例:在一个UIView上复制层。
1、将CAReplicatorLayer创建的层添加到UIView的子层中或者设置为根层。
2、再讲手动创建的层设置为CAReplicatorLayer的子层。
3、此时CAReplicatorLayer的层调用instanceCount属性,并设置偏移量,设置颜色。
4、添加动画。
CAReplicatorLayer *replicatorLayer = [[CAReplicatorLayer alloc]init];
replicatorLayer.frame = self.view.bounds;
replicatorLayer.backgroundColor = [UIColor whiteColor].CGColor;
//设置为view的子层
//[self.view.layer addSublayer:replicatorLayer];
//设置为view根层
[self.view setValue:replicatorLayer forKey:@"layer"];
CALayer *layer = [[CALayer alloc]init];
layer.frame = CGRectMake(100, 100, 10, 100);
layer.backgroundColor = [UIColor redColor].CGColor;
//设置为replicatorLayer的子层
[replicatorLayer addSublayer:layer];
replicatorLayer.instanceCount = 6;
replicatorLayer.instanceTransform = CATransform3DMakeTranslation(45, 0, 0);
replicatorLayer.instanceDelay = 0.2;
>
// 添加一个基本动画
CABasicAnimation *anim = [[CABasicAnimation alloc]init];
anim.keyPath = @"transform.scale.y";//改变y值
anim.toValue = @0.1;
anim.autoreverses = YES;//往返都有动画
anim.repeatCount = MAXFLOAT;//执行次数
//添加
[layer addAnimation:anim forKey:nil];
![](https://img.haomeiwen.com/i1170347/9883865806df671a.gif)
属性:
instanceCount:表示复制层里面有多少个子层,包括原始层。
instanceTransform:设置复制子层偏移量,不包括原始层,相对于前一个层的偏移量。
instanceDelay:设置复制层动画延迟时间,相对于前一个层的延迟时间。
instanceColor:设置层的颜色,(前提是要设置层的背景颜色,如果没有设置背景颜色,默认是透明的,再设置这个属性不会有效果。)
instanceGreenOffset:颜色的Green的渐变,相对于前一个层的渐变(取值从0 - 1)。
音乐震动条
#import "ViewController.h"
>
@interface ViewController ()
//视图中的view,记得拖线
@property (weak, nonatomic) IBOutlet UIView *lightView;
>
@end
>
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// CAReplicatorLayer复制图层,可以把图层里面所有子层复制
// 创建复制图层
CAReplicatorLayer *repL = [CAReplicatorLayer layer];
>
repL.frame = _lightView.bounds;
>
[_lightView.layer addSublayer:repL];
>
CALayer *layer = [CALayer layer];
>
layer.anchorPoint = CGPointMake(0.5, 1);
layer.position = CGPointMake(15, _lightView.bounds.size.height);
layer.bounds = CGRectMake(0, 0, 30, 150);
>
layer.backgroundColor = [UIColor whiteColor].CGColor;
>
[repL addSublayer:layer];
>
CABasicAnimation *anim = [CABasicAnimation animation];
>
anim.keyPath = @"transform.scale.y";
>
anim.toValue = @0.1;
>
anim.duration = 0.5;
>
anim.repeatCount = MAXFLOAT;
>
// 设置动画反转
anim.autoreverses = YES;
>
>
[layer addAnimation:anim forKey:nil];
>
>
// 复制层中子层总数
// instanceCount:表示复制层里面有多少个子层,包括原始层
repL.instanceCount = 3;
>
// 设置复制子层偏移量,不包括原始层,相对于原始层x偏移
repL.instanceTransform = CATransform3DMakeTranslation(45, 0, 0);
>
// 设置复制层动画延迟时间
repL.instanceDelay = 0.1;
>
// 如果设置了原始层背景色,就不需要设置这个属性
repL.instanceColor = [UIColor greenColor].CGColor;
>
repL.instanceGreenOffset = -0.3;
>
>
}
![](https://img.haomeiwen.com/i1170347/e4d5285847e8bc00.gif)
CAShapeLayer...
CAScrollLayer...
网友评论