美文网首页ios他人收集整理动画设计学习iOS 技术文档收录
iOS/OC: 绘制图表时几种动画展示特效(源码)

iOS/OC: 绘制图表时几种动画展示特效(源码)

作者: 疯狂的向日葵 | 来源:发表于2016-10-01 08:55 被阅读1675次

    版权声明:本文为博主原创文章,未经博主允许不得转载。

    金融类项目时经常需要绘制各类曲线,这里列出了一些常见的曲线展示动画,思路可供参考

    1.曲线出现的时候向y轴两边拉伸

    拉伸特效.gif

    不废话了,直接抛demo代码

    - (void)viewDidLoad {
        [super viewDidLoad];
        //中心轴线
        UIBezierPath *Nopath = [UIBezierPath bezierPath];
        [Nopath moveToPoint:CGPointMake(0, 40)];
        //封闭图形
        UIBezierPath *fillpath = [UIBezierPath bezierPath];
        [fillpath moveToPoint:CGPointMake(0, 0)];
    
        for (int i = 0; i < 20; i ++) {
            if (i<5) {
                
                [fillpath addLineToPoint:CGPointMake(20*i, 20*i)];
                [Nopath addLineToPoint:CGPointMake(15*i, 40)];
                
            }else if(i <10)
            {
                [fillpath addLineToPoint:CGPointMake(15*i, 10*i)];
                [Nopath addLineToPoint:CGPointMake(15*i, 40)];
                
            }else{
                [fillpath addLineToPoint:CGPointMake(18*i, 6*i)];
                [Nopath addLineToPoint:CGPointMake(18*i, 40)];
                
            }
            
            if (i ==19) {
                //这几个点很关键...
                [fillpath addLineToPoint:CGPointMake(18*i, 6*i)];
                [fillpath addLineToPoint:CGPointMake(18*i, 40)];
                [Nopath addLineToPoint:CGPointMake(18*i, 40)];
                [Nopath addLineToPoint:CGPointMake(18*i, 40)];
                
            }
        }
        //这几个点也很关键...
        [Nopath addLineToPoint:CGPointMake(0, 40)];
        [Nopath addLineToPoint:CGPointMake(0, 40)];
        
        [fillpath addLineToPoint:CGPointMake(0, 40)];
        [fillpath addLineToPoint:CGPointMake(0, 0)];
        
        //layer
        CAShapeLayer *layer = [CAShapeLayer layer];
        layer.frame = CGRectMake(20, 20, 200, 200);
        layer.bounds = CGRectMake(0, 0, 200, 200);
        layer.path = fillpath.CGPath;
        layer.fillColor = [UIColor greenColor].CGColor;
        layer.lineJoin = kCALineCapRound;
        [self.view.layer addSublayer:layer];
    
        //动画
        CABasicAnimation *fillAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
        fillAnimation.duration = 4.0f;
        fillAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        fillAnimation.fillMode = kCAFillModeForwards;
        fillAnimation.fromValue = (id)Nopath.CGPath; //从中间轴
        fillAnimation.toValue = (id)fillpath.CGPath;//像两边拉伸
        [layer addAnimation:fillAnimation forKey:@"stretchingKey"];
    }
    

    2.曲线渐进出现,且图层颜色渐变

    渐进.gif

    源码

        //遮罩路径
        UIBezierPath *maskpath = [UIBezierPath bezierPath];
        [maskpath moveToPoint:CGPointMake(0, 0)];
        for (int i = 0; i < 20; i ++) {
            if (i<5) {
                
                [maskpath addLineToPoint:CGPointMake(20*i, 20*i)];
                
            }else if(i <10)
            {
                [maskpath addLineToPoint:CGPointMake(15*i, 10*i)];
                
            }else{
                [maskpath addLineToPoint:CGPointMake(18*i, 6*i)];
            }
            
            if (i ==19) {
                [maskpath addLineToPoint:CGPointMake(18*i, 200)];
            }
        }
        //回到原点,封闭图形
        [maskpath addLineToPoint:CGPointMake(0, 200)];
        [maskpath addLineToPoint:CGPointMake(0, 0)];
        //遮罩图层
        CAShapeLayer *shadeLayer = [CAShapeLayer layer];
        shadeLayer.path = maskpath.CGPath;
        shadeLayer.fillColor = [UIColor greenColor].CGColor;
        //渐变图层
        CAGradientLayer *gradientLayer = [CAGradientLayer layer];
        gradientLayer.frame = CGRectMake(0, 0, 0, 200);
        gradientLayer.startPoint = CGPointMake(0, 0);
        gradientLayer.endPoint = CGPointMake(0, 1);
        gradientLayer.colors = @[(__bridge id)[UIColor colorWithRed:166/255.0 green:206/255.0 blue:247/255.0 alpha:0.7].CGColor,(__bridge id)[UIColor colorWithRed:237/255.0 green:246/255.0 blue:253/255.0 alpha:0.5].CGColor];
        gradientLayer.locations = @[@(0.5f)];
        
        CALayer *baseLayer = [CALayer layer];
        baseLayer.frame = CGRectMake(20, 300, 18*19, 200);
        [baseLayer addSublayer:gradientLayer];
        [baseLayer setMask:shadeLayer];
        
        [self.view.layer addSublayer:baseLayer];
        
        CABasicAnimation *anmi1 = [CABasicAnimation animation];
        anmi1.keyPath = @"bounds";
        anmi1.duration = 5.0f;
        anmi1.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 2*18*19, 200)];
        anmi1.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        anmi1.fillMode = kCAFillModeForwards;
        anmi1.autoreverses = NO;
        anmi1.removedOnCompletion = NO;
        [gradientLayer addAnimation:anmi1 forKey:@"bounds"];
    

    3.逐渐绘制路径

    绘制路径.gif

    源码

    这是最简单的一种了

        //绘制路径
        UIBezierPath *strokepath = [UIBezierPath bezierPath];
        [strokepath moveToPoint:CGPointMake(0, 0)];
        for (int i = 0; i < 20; i ++) {
            if (i<5) {
                
                [strokepath addLineToPoint:CGPointMake(20*i, 20*i)];
                
            }else if(i <10)
            {
                [strokepath addLineToPoint:CGPointMake(15*i, 10*i)];
                
            }else{
                [strokepath addLineToPoint:CGPointMake(18*i, 6*i)];
            }
        }
        CAShapeLayer *layer2 = [CAShapeLayer layer];
        layer2.frame = CGRectMake(20, 500,18*19, 200);
        layer2.path = strokepath.CGPath;
        layer2.strokeColor = [UIColor greenColor].CGColor;
        layer2.lineWidth = 2.0f;
        layer2.fillColor = nil;
        layer2.lineJoin = kCALineCapRound;
        [self.view.layer addSublayer:layer2];
        
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
        animation.duration = 5.0f;
        animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        animation.fromValue = @(0.0);
        animation.toValue = @(1.0);
        [layer2 addAnimation:animation forKey:@"path"];
    

    未完待续...

    相关文章

      网友评论

      • 2257fe37c44d:路径和图层一起绘画 怎么实现
      • 大号鱼骨头:只能说强大的贝塞尔曲线,CAShapeLayer以及CABasicAnimation。楼主棒棒哒。
        疯狂的向日葵:@大号鱼骨头 我也在学习
      • 卟师:我能转载分享吗?我会标注上作者和出处的
        疯狂的向日葵:@卟师 可以
      • 王大虾34:第二个例子,2.曲线渐进出现,且图层颜色渐变,
        anmi1.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 2*18*19, 200)];这边宽度2*18*19,为什么要乘以2 啊,不乘以2效果不对,但是不理解为什么要乘以2
        疯狂的向日葵:@小超飞鱼 有道理!
        小超飞鱼:2倍宽是因为layer以bounds做动画的话,其position一开始为(0,100),动画过程中不变,所以动画在x轴看来是从0向两边扩展(负方向看不到),也就需要2倍的宽度。验证的话,你可以看下最后gradientLayer的frame其实是负的
        疯狂的向日葵:@王大虾1号 这个我写的时候就发现了,两倍宽才行,一倍宽正好一半,具体原因我也没有理解.
      • 十字云:最喜欢未完待续,然后写一堆干货 :blush:

      本文标题:iOS/OC: 绘制图表时几种动画展示特效(源码)

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