CAlayer子类之CAReplicatorLayer(实现转圈动画)

一看就懂demo下载地址:github

  CAReplicatorLayer,负责创建子layer特定数目的copy,这个与别的不太同,别的layer是创建好后添加到父layer,这个则是创建好后添加目标layer为自己的子layer。

1.首先创建一个UIView

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(10, 200, 25, 25)];

view.backgroundColor = [UIColor purpleColor];

view.layer.cornerRadius = CGRectGetHeight(view.bounds)/2;

view.layer.masksToBounds = YES;

view.layer.transform = CATransform3DMakeScale(0.1, 0.1, 0.1);起始大小

2.接着创建一个layer

CAReplicatorLayer *layer = [CAReplicatorLayer layer];

layer.bounds = self.view.bounds;

layer.position = self.view.center;

layer.preservesDepth = YES;

layer.instanceColor = [UIColor whiteColor].CGColor;

layer.instanceCount = 20;copy的数量

layer.instanceDelay = 1.0/20;创建copy的时间间隔

layer.instanceTransform = CATransform3DMakeRotation((2 * M_PI) /20, 0, 0, 1);旋转的角度

[layer addSublayer:view.layer];

[self.view.layer addSublayer:layer];

注意添加顺序,layer添加view的layer作为子layer,self.view再添加layer作为子视图。

3.创建一个动画

CABasicAnimation *ani = 

[CABasicAnimation animationWithKeyPath:@"transform.scale"];

animation.duration = 1;

animation.repeatCount = MAXFLOAT;

animation.fromValue = @(1);

animation.toValue = @(0.1);

[view.layer addAnimation:animation forKey:nil];

动画不断重复从大到小的变化,最终效果:


推荐阅读更多精彩内容