美文网首页
iOS 折线图动画

iOS 折线图动画

作者: 安宇辛 | 来源:发表于2021-11-29 17:34 被阅读0次
折线图效果

一:折线动画

CAShapeLayer *lineLayer = [CAShapeLayer layer];
    lineLayer.path = linePath.CGPath;
    lineLayer.strokeColor = self.lineColor.CGColor;
    lineLayer.fillColor = [UIColor clearColor].CGColor;
    lineLayer.lineWidth = self.lineWidth;
    lineLayer.lineCap = kCALineCapRound;
    lineLayer.lineJoin = kCALineJoinRound;
    lineLayer.contentsScale = [UIScreen mainScreen].scale;
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    animation.duration = 1.f;   // 持续时间
    animation.fromValue = @(0); // 从 0 开始
    animation.toValue = @(1);
    animation.delegate = self;
    [lineLayer addAnimation:animation forKey:@""];
    
    [self.layer addSublayer:lineLayer];

二:填充色随线条动画进行填充(用遮罩实现)

  UIBezierPath *colorPath = [UIBezierPath bezierPath];
        colorPath.lineWidth = 1.f;
        [colorPath moveToPoint:startPoint];
        if (self.addCurve) {
            //曲线路径
            [colorPath addBezierThroughPoints2:pointArr];
        } else {
           //直线路径
            [colorPath addNormalBezierThroughPoints:pointArr];
        }
        [colorPath addLineToPoint:CGPointMake(endPoint.x, maxMidY)];
        [colorPath addLineToPoint:CGPointMake(startPoint.x, maxMidY)];
        [colorPath addLineToPoint:CGPointMake(startPoint.x, startPoint.y)];
        
        //遮罩层
        CAShapeLayer *shadeLayer = [CAShapeLayer layer];
        shadeLayer.path = colorPath.CGPath;
        shadeLayer.fillColor = [UIColor greenColor].CGColor;
        
        //渐变图层
        CAGradientLayer *gradientLayer = [CAGradientLayer layer];
        gradientLayer.frame = shadeLayer.frame;
        gradientLayer.startPoint = CGPointMake(0, 0);
        gradientLayer.endPoint = CGPointMake(0, 1);
        gradientLayer.cornerRadius = 5;
        gradientLayer.masksToBounds = YES;
        gradientLayer.colors = self.colorArr;
        
        CALayer *baseLayer = [CALayer layer];
        [baseLayer addSublayer:gradientLayer];
        [baseLayer setMask:shadeLayer];
        
        CABasicAnimation *anmi1 = [CABasicAnimation animation];
        anmi1.keyPath = @"bounds";
        anmi1.duration = 0.8f;
        anmi1.fromValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 0, 2*self.size.height)];
        anmi1.byValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 2*endPoint.x, 2*self.size.height)];
//        anmi1.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        anmi1.fillMode = kCAFillModeForwards;
        anmi1.autoreverses = NO;
        anmi1.removedOnCompletion = NO;
        [gradientLayer addAnimation:anmi1 forKey:@"bounds"];
        
        [self.layer addSublayer:baseLayer];

相关文章

  • iOS动画折线图

    效果图: 后端传一个数组,然后根据数组生成点的坐标。所以解析下,我们需要画的有点,点和点之间的线,以及背景和下面的...

  • iOS 折线图动画

    一:折线动画 二:填充色随线条动画进行填充(用遮罩实现)

  • ios动画

    ios动画 ios动画2 ios动画3

  • iOS动画

    iOS动画-从UIView动画说起iOS动画-Transform和KeyFrame动画iOS动画-layout动画...

  • 《iOS动画》读书笔记·内容层动画

    《iOS动画》读书笔记·前序《iOS动画》读书笔记·显示层动画《iOS动画》读书笔记·内容层动画《iOS动画》读书...

  • 《iOS动画》读书笔记·显示层动画

    《iOS动画》读书笔记·前序《iOS动画》读书笔记·显示层动画《iOS动画》读书笔记·内容层动画《iOS动画》读书...

  • 《iOS动画》读书笔记·前序

    《iOS动画》读书笔记·前序《iOS动画》读书笔记·显示层动画《iOS动画》读书笔记·内容层动画《iOS动画》读书...

  • 《iOS动画》读书笔记·转场动画

    《iOS动画》读书笔记·前序《iOS动画》读书笔记·显示层动画《iOS动画》读书笔记·内容层动画《iOS动画》读书...

  • iOS - 转场动画

    参考文章:iOS 转场动画一张图看懂 iOS 转场动画iOS自定义转场动画 iOS 转场动画探究(一)

  • iOS开发——登录页面动画、转场动画

    iOS开发——登录页面动画、转场动画 iOS开发——登录页面动画、转场动画

网友评论

      本文标题:iOS 折线图动画

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