美文网首页
CALayer子类三、CAReplicatorLayer

CALayer子类三、CAReplicatorLayer

作者: Carson_Zhu | 来源:发表于2018-02-09 03:21 被阅读21次

    CAReplicatorLayer可以复制自己子层的layer,并且复制的出来的layer和原来的子layer拥有相同的动效。然后通过设置一些属性,就可以完成很酷的效果,非常强大。

    属性

    • instanceCount
      复制图层的个数,包括加到上面的。
    • preservesDepth
      子图层是否平面化。
    • instanceDelay
      复制层动画延迟时间。
    • instanceTransform
      子图层的transform变换,一般用来决定复制图层的初始位置以及初始试图变换。
    • instanceColor
      设置多个复制图层的颜色,默认位白色。
    • instanceRedOffset
      设置每个复制图层相对上一个复制图层的红色偏移量。
    • instanceGreenOffset
      设置每个复制图层相对上一个复制图层的绿色偏移量。
    • instanceBlueOffset
      设置每个复制图层相对上一个复制图层的蓝色偏移量。
    • instanceAlphaOffset
      设置每个复制图层相对上一个复制图层的透明度偏移量。

    实例

    水波
    - (void)replicatorLayer_circle{
        CAShapeLayer *shapeLayer = [CAShapeLayer layer];
        shapeLayer.frame = CGRectMake(0, 0, 80, 80);
        shapeLayer.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 80, 80)].CGPath;
        shapeLayer.fillColor = [UIColor redColor].CGColor;
        shapeLayer.opacity = 0.0; // 取值范围0~1.0,表示从完全透明到完全不透明。
        
        // 透明度动画
        CABasicAnimation *alphaAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];
        alphaAnim.fromValue = @(1.0);
        alphaAnim.toValue = @(0.0);
        
        // 比例动画
        CABasicAnimation *scaleAnim = [CABasicAnimation animationWithKeyPath:@"transform"];
        scaleAnim.fromValue = [NSValue valueWithCATransform3D:CATransform3DScale(CATransform3DIdentity, 0.0, 0.0, 0.0)];
        scaleAnim.toValue = [NSValue valueWithCATransform3D:CATransform3DScale(CATransform3DIdentity, 1.0, 1.0, 0.0)];
        
        CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
        animationGroup.animations = @[alphaAnim, scaleAnim];
        animationGroup.duration = 2.0;
        animationGroup.autoreverses = NO;
        animationGroup.repeatCount = MAXFLOAT;
        [shapeLayer addAnimation:animationGroup forKey:@"animationGroup"];
        
        CAReplicatorLayer *replicatorLayer = [CAReplicatorLayer layer];
        replicatorLayer.bounds = CGRectMake(0, 0, 80, 80);
        replicatorLayer.position = self.view.center;
        replicatorLayer.instanceDelay = 0.5;
        replicatorLayer.instanceCount = 8;
        [replicatorLayer addSublayer:shapeLayer];
        [self.view.layer addSublayer:replicatorLayer];
    }
    

    运行效果:


    播放器
    - (void)replicatorLayer_lineShake {
        CAReplicatorLayer *replicatorLayer = [CAReplicatorLayer layer];
        replicatorLayer.frame = self.view.bounds;
        replicatorLayer.bounds = self.view.frame;
        replicatorLayer.position = self.view.center;
        replicatorLayer.instanceCount = 7;
        replicatorLayer.instanceTransform = CATransform3DMakeTranslation(45, 0, 0);
        replicatorLayer.instanceDelay = 0.2;
        [self.view.layer addSublayer:replicatorLayer];
        
        CALayer *layer = [[CALayer alloc] init];
        layer.frame = CGRectMake(0, 0, 10, 80);
        layer.position = CGPointMake(50, self.view.center.y);
        layer.backgroundColor = [UIColor redColor].CGColor;
        layer.anchorPoint = CGPointMake(0.5, 1);
        [replicatorLayer addSublayer:layer];
        // 添加一个基本动画
        CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform.scale.y"];
        anim.toValue = @0.1;
        anim.duration = 0.4;
        anim.autoreverses = YES;//往返都有动画
        anim.repeatCount = MAXFLOAT;//执行次数
        [layer addAnimation:anim forKey:@"scaleAnimation"];
    }
    

    运行效果:


    圆圈
    - (void)replicatorLayer_round{
        CALayer *layer = [[CALayer alloc]init];
        layer.frame = CGRectMake(0, 0, 20, 20);
        layer.cornerRadius = 10;
        layer.masksToBounds = YES;
        layer.transform = CATransform3DMakeScale(0.01, 0.01, 0.01);
        layer.backgroundColor = [UIColor redColor].CGColor;
        
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
        animation.duration = 1;
        animation.repeatCount = MAXFLOAT;
        animation.fromValue = @(1);
        animation.toValue = @(0.01);
        [layer addAnimation:animation forKey:nil];
        
        NSInteger instanceCount = 9;
        
        CAReplicatorLayer *replicatorLayer = [CAReplicatorLayer layer];
        replicatorLayer.bounds = CGRectMake(0, 0, 100, 100);
        replicatorLayer.position = self.view.center;
        replicatorLayer.preservesDepth = YES;
        replicatorLayer.instanceColor = [UIColor whiteColor].CGColor;
        replicatorLayer.instanceRedOffset = 0.1;
        replicatorLayer.instanceGreenOffset = 0.1;
        replicatorLayer.instanceBlueOffset = 0.1;
        replicatorLayer.instanceAlphaOffset = 0.1;
        replicatorLayer.instanceCount = instanceCount;
        replicatorLayer.instanceDelay = 1.0/instanceCount;
        replicatorLayer.instanceTransform = CATransform3DMakeRotation((2 * M_PI) /instanceCount, 0, 0, 1);
        [replicatorLayer addSublayer:layer];
        
        [self.view.layer addSublayer:replicatorLayer];
    }
    

    运行效果:

    相关文章

      网友评论

          本文标题:CALayer子类三、CAReplicatorLayer

          本文链接:https://www.haomeiwen.com/subject/lnixtftx.html