简单的转菊花

1.初始化CAReplicatorLayer对象

  • 初始化复制层,设置位置,尺寸,颜色大小

    //1.初始化复制层
    CAReplicatorLayer *replicatorLayer = [CAReplicatorLayer layer];
    replicatorLayer.bounds = CGRectMake(0, 0, 200, 200);
    replicatorLayer.backgroundColor = [UIColor grayColor].CGColor;
    [self.view.layer addSublayer:replicatorLayer];
    
  • 创建原生图层,设置基本单位图层的大小,尺寸,颜色等

        //初始化原生子层
    CALayer *layer = [CALayer layer];
    layer.bounds = CGRectMake(0, 0, 10, 10);
    layer.cornerRadius = 5;
    layer.position = CGPointMake(100, 20);
    layer.backgroundColor = [UIColor blackColor].CGColor;
    [replicatorLayer addSublayer:layer];
    
简单的转菊花_第1张图片
Snip20160312_13.png
  • 给原生层添加动画

        //给原生子层添加动画
            CABasicAnimation *basic = [CABasicAnimation animation];
            basic.keyPath = @"transform.scale";
            basic.repeatCount = MAXFLOAT;//设置重复次数
            basic.autoreverses = YES;//设置动画的反弹效果
            basic.fromValue = @1;
            basic.toValue = @0.1;
            //动画时长
            CGFloat duration = 1;
            basic.duration = duration;
            [layer addAnimation:basic forKey:nil];
    
  • 设置复制层的子层数量,子层的形变位置等

        //子层数
            float count = 30;
            replicatorLayer.instanceCount = count;
            //一个单位图层动画时长为1 ,(从大到小,从小到大,2个阶段) ,延迟子层的延迟时间 = 动画总时长 / 总个数 * 2个阶段
            replicatorLayer.instanceDelay = duration / count * 2;
            replicatorLayer.instanceTransform = CATransform3DRotate(layer.transform, 2 * M_PI / count, 0, 0, 1);
    
简单的转菊花_第2张图片
Snip20160312_14.png

你可能感兴趣的:(简单的转菊花)