美文网首页
如何做一个图表动画

如何做一个图表动画

作者: Alfred_小乐 | 来源:发表于2017-03-22 18:23 被阅读203次

以前的一个同事告诉我说,他们公司有个需求:生成一个曲线图表,要以动画的方式画出来。曲线图很好画,动画该如何写,其实用CAShapeLayer+CABasicAnimation就可以很好的实现。


效果图

知道了技术点,剩下的就是撸代码了。
首先我们先画一个底层网格线。
新建一个AnmiationChartview继承UIView,重写drawrect:方法

-(void)drawRect:(CGRect)rect
{
// 画网格线
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(ctx, 1);
CGContextSetStrokeColorWithColor(ctx, [UIColor greenColor].CGColor);
// 虚线设置
CGFloat a[] = {1,3};// . .
CGContextSetLineDash(ctx, 0, a, 2);
for (int i=0; i<10; i++) {
    int x = (int)CGRectGetWidth(self.bounds)/11*(i+1);
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, 0, x);
    CGPathAddLineToPoint(path, NULL, CGRectGetWidth(self.bounds), x);
    CGContextAddPath(ctx, path);
    CGContextDrawPath(ctx, kCGPathStroke);
    CGPathRelease(path);
    
}
for (int j=0; j<(int)CGRectGetHeight(self.bounds)/10; j++) {
    int y = (int)CGRectGetHeight(self.bounds)/11*(j+1);
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, y, 0);
    CGPathAddLineToPoint(path, NULL, y, CGRectGetHeight(self.bounds));
    CGContextAddPath(ctx, path);
    CGContextDrawPath(ctx, kCGPathStroke);
    CGPathRelease(path);
}
}

运行可以得到想要的网格线

网格线

创建一个坐标的model,用于后面传递坐标点数组

@interface ChartPoint : NSObject
@property (nonatomic,readonly)CGPoint point;
- (instancetype)initWithpoint:(CGPoint)point;
@end

@implementation ChartPoint
{
    CGPoint _chartpoint;
}
-(instancetype)initWithpoint:(CGPoint)point
{
    if (self = [super init]) {
        _chartpoint = point;
    }
    return self;
}
-(CGPoint)point
{
    return _chartpoint;
}
@end

新建ChartLayer继承CAShapeLayer

@interface ChartLayer : CAShapeLayer
- (void)showchartwithpoints:(NSArray <ChartPoint       *>*)ponits;
@end

当坐标点传递过来时,设置路径

CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, _anpoints.firstObject.point.x, _anpoints.firstObject.point.y);
for (ChartPoint *point in _anpoints) {
    if (point == _anpoints.firstObject) {
        
    }else{
        CGPathAddLineToPoint(path, NULL, point.point.x, point.point.y);
    }
}
self.path = path;
CGPathRelease(path);// 不要忘记release

然后 设置layer的动画

self.lineWidth=2;
self.fillColor = [UIColor clearColor].CGColor;
self.lineCap = kCALineCapRound;
self.strokeColor = [UIColor orangeColor].CGColor;
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
animation.fromValue = @(0);
animation.toValue = @(1);
animation.duration = 5;
animation.removedOnCompletion = YES;
[self addAnimation:animation forKey:@"pathanmiation"];

改变AnmiationChartview的layer为ChartLayer

 +(Class)layerClass
{
    return [ChartLayer class];
}

AnmiationChartview的调用动画方法

-(void)showchartwithpoints:(NSArray<ChartPoint *> *)ponits{

    ChartLayer *layer = (ChartLayer *)self.layer;
    [layer showchartwithpoints:ponits];
}

在viewcontroller中创建View,随机获取坐标点

- (void)viewDidLoad {
    [super viewDidLoad];
    self.anv = [[AnmiationChartview alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
    self.anv.center = self.view.center;
    [self.view addSubview:self.anv];
    [self.anv setBackgroundColor:[UIColor lightGrayColor]];
}
- (void)nextChart
{
    float x,y;
    NSMutableArray *arr = [NSMutableArray     arrayWithCapacity:0];
    for (int i=0; i<40; i++) {
        x += 4.95;
        y = arc4random()%100+50;
        ChartPoint *point = [[ChartPoint alloc] initWithpoint:CGPointMake(x, y)];
        [arr addObject:point];
    }
    [self.anv showchartwithpoints:arr];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self nextChart];
}

运行,点击一下,效果就实现了。。

相关文章

  • 如何做一个图表动画

    以前的一个同事告诉我说,他们公司有个需求:生成一个曲线图表,要以动画的方式画出来。曲线图很好画,动画该如何写,其实...

  • PPT技巧:如何实现PPT中图表动画按数据从小到大演示?

    这个问题,确实是PPT中Excel图表动画的硬伤。 如果直接在图表设置动画,动画的顺序和步骤都都是固定死的(看下动...

  • Flutter 动画

    字体放大效果-补间动画 缓动动画 3.遮罩动画 4.数字动画 5.图表动画

  • 仿造PPT图表

    今天我们来仿造一下PPT图表,看看如何做一个高大上的PPT 一、第一个图表 这个图表看起来不错,简单明了。接下来我...

  • 图 | 如何创建动态图表

    问题分解 如何创建动态图表?这里有必要澄清一下,这里所说的动态并不是指GIF动画的意思,而是指图表会根据我们选择的...

  • 动画与互动

    1. 动画与互动 在叙事结构中全面应用创意D3如何帮你在可视化图表中添加动画与互动用地理特征创建D3地图了解别人如...

  • 当PPT邂逅C4D:炫酷图表一键生成!(内附福利)

    图表在PPT中经常使用到,不过PPT制作图表的样式有限,要实现动态图表无非也就添加些动画或者平滑切换效果 因此,为...

  • canvas图表(3) - 饼图

      原文地址:canvas图表(3) - 饼图  这几天把canvas图表都优化了下,动画效果更加出色了,可以说很...

  • 2019-11-28动态图表

    如何建立双坐标图表:选中区域,插入推荐图表,组合,选择同比,折线为同比,美化图表 动态图表 1.辅助列:数据,图表...

  • 检查一下,你的PPT图表页,是否也存在这些问题?

    图表,是各种 PPT 需求的重头戏,不会优化图表都不好意思说自己擅长 PPT 制作。 但图表和动画一样,都需要花大...

网友评论

      本文标题:如何做一个图表动画

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