1. 新建一个DrawLine类继承UIView
  2. DrawLine.h代码
@property (nonatomic,strong)NSMutableArray *data;@property (nonatomic, strong) CAShapeLayer *lineChartLayer;
@property (nonatomic, strong)UIBezierPath * path1;
/** 渐变背景视图 */
@property (nonatomic, strong) UIView *gradientBackgroundView;
/** 渐变图层 */
@property (nonatomic, strong) CAGradientLayer *gradientLayer;
/** 颜色数组 */
@property (nonatomic, strong) NSMutableArray *gradientLayerColors;@property (nonatomic,assign)int ySpace;//y轴间隔
@property (nonatomic,assign)int yNum;//y轴的分点数
@property (nonatomic,assign)int xSpace;//x轴间隔
@property (nonatomic,assign)int xNum;//x轴的分点数
@property (nonatomic,strong)NSString *ytitle;//y轴标题
@property (nonatomic,strong)NSString *xtitle;//x轴标题
@property (nonatomic,assign)BOOL showNum;//显示数值//建立坐标
-(void)initCoordinate;
//开始画图
-(void)beginDraw;

3.DrawLine.m代码

- (instancetype)initWithFrame:(CGRect)frame{if (self = [super initWithFrame:frame]) {self.backgroundColor = [UIColor whiteColor];[self drawGradientBackgroundView];}return self;
}-(void)initCoordinate
{//y轴
//    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(50, 15, 1, self.frame.size.height-60)];
//    view.backgroundColor=[UIColor lightGrayColor];
//    [self addSubview:view];UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(70, 10, 150, 21)];label.text=self.ytitle;label.font=[UIFont systemFontOfSize:12];label.textColor=[UIColor grayColor];[self addSubview:label];for(int i=self.yNum;i>0;i--){UIView *cutoffLine=[[UIView alloc] initWithFrame:CGRectMake(51, 15+(self.frame.size.height-60)/(self.yNum+1)*(self.yNum+1-i), self.frame.size.width-100, 1)];cutoffLine.backgroundColor=[UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1.0];[self.gradientBackgroundView addSubview:cutoffLine];UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, cutoffLine.center.y-10.5, 45, 21)];label.font=[UIFont systemFontOfSize:10];label.textColor=[UIColor grayColor];label.textAlignment=NSTextAlignmentRight;label.text=[NSString stringWithFormat:@"%d",self.ySpace*i];[self addSubview:label];}//x轴UIView *view=[[UIView alloc] initWithFrame:CGRectMake(50, 15+self.frame.size.height-60, screen_width-100, 1)];view.backgroundColor=[UIColor lightGrayColor];[self addSubview:view];for(int i=0;i<self.xNum;i++){UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake((screen_width-100)/self.xNum*i+50, self.frame.size.height-40, (screen_width-100)/self.xNum, 21)];label.font=[UIFont systemFontOfSize:10];label.textColor=[UIColor grayColor];label.text=[NSString stringWithFormat:@"%d",self.xSpace*i];[self addSubview:label];}label=[[UILabel alloc] initWithFrame:CGRectMake(screen_width-70, 20+self.frame.size.height-60, 70, 21)];label.text=self.xtitle;label.font=[UIFont systemFontOfSize:12];label.textColor=[UIColor grayColor];[self addSubview:label];
}#pragma mark 画折线图
- (void)dravLine{CGFloat barWidth=(screen_width-100)/self.xNum*(self.xNum-1)/((self.xNum-1)*self.xSpace);//条形图1个单位的宽CGFloat barHeight=(self.frame.size.height-60)/(self.yNum+1)/self.ySpace;//条形图1个单位的高//(xorignal,yorignal)坐标原点CGFloat xorignal=50;CGFloat yorignal=15+self.frame.size.height-60;//    UILabel * label = (UILabel*)[self viewWithTag:1000];UIBezierPath * path = [[UIBezierPath alloc]init];path.lineWidth = 1.0;self.path1 = path;UIColor * color = [UIColor greenColor];[color set];//起点[path moveToPoint:CGPointMake( [self.data[0][@"x"] floatValue]*barWidth+xorignal, yorignal-[self.data[0][@"y"] floatValue]*barHeight )];//数值UILabel *label=(UILabel *)[self viewWithTag:1000];if(label==nil){label=[[UILabel alloc] initWithFrame:CGRectMake([self.data[0][@"x"] floatValue]*barWidth+xorignal-50, yorignal-[self.data[0][@"y"] floatValue]*barHeight-20, 100, 21)];label.font=[UIFont systemFontOfSize:10];label.textColor=[UIColor grayColor];label.tag=1000;label.textAlignment=NSTextAlignmentCenter;label.text=[NSString stringWithFormat:@"%.0f",[self.data[0][@"y"] floatValue]];[self addSubview:label];}else{label.frame=CGRectMake([self.data[0][@"x"] floatValue]*barWidth+xorignal-50, yorignal-[self.data[0][@"y"] floatValue]*barHeight-20, 100, 21);label.text=[NSString stringWithFormat:@"%.0f",[self.data[0][@"y"] floatValue]];}if(!self.showNum){label.hidden=YES;}else{label.hidden=NO;}//圆点UIView *roundView=(UIView *)[self viewWithTag:100];if(roundView==nil){roundView=[[UIView alloc] initWithFrame:CGRectMake([self.data[0][@"x"] floatValue]*barWidth+xorignal-3, yorignal-[self.data[0][@"y"] floatValue]*barHeight-3, 6, 6)];roundView.layer.masksToBounds=YES;roundView.layer.cornerRadius=3;roundView.backgroundColor=[UIColor grayColor];roundView.tag=100;[self addSubview:roundView];}else{roundView=[[UIView alloc] initWithFrame:CGRectMake([self.data[0][@"x"] floatValue]*barWidth+xorignal-3, yorignal-[self.data[0][@"y"] floatValue]*barHeight-3, 6, 6)];}//转折点for (NSInteger i = 1; i< self.data.count; i++) {[path addLineToPoint:CGPointMake([self.data[i][@"x"] floatValue]*barWidth+xorignal, yorignal-[self.data[i][@"y"] floatValue]*barHeight)];UILabel *label=(UILabel *)[self viewWithTag:1000+i];if(label==nil){label=[[UILabel alloc] initWithFrame:CGRectMake([self.data[i][@"x"] floatValue]*barWidth+xorignal-50, yorignal-[self.data[i][@"y"] floatValue]*barHeight-20, 100, 21)];label.font=[UIFont systemFontOfSize:10];label.textColor=[UIColor grayColor];label.tag=1000+i;label.textAlignment=NSTextAlignmentCenter;label.text=[NSString stringWithFormat:@"%.0f",[self.data[i][@"y"] floatValue]];[self addSubview:label];}else{label.frame=CGRectMake([self.data[i][@"x"] floatValue]*barWidth+xorignal-50, yorignal-[self.data[i][@"y"] floatValue]*barHeight-20, 100, 21);label.text=[NSString stringWithFormat:@"%.0f",[self.data[i][@"y"] floatValue]];}if(!self.showNum){//不显示转折点数值label.hidden=YES;}else{label.hidden=NO;}UIView *roundView=(UIView *)[self viewWithTag:100+i];if(roundView==nil){roundView=[[UIView alloc] initWithFrame:CGRectMake([self.data[i][@"x"] floatValue]*barWidth+xorignal-3, yorignal-[self.data[i][@"y"] floatValue]*barHeight-3, 6, 6)];roundView.layer.masksToBounds=YES;roundView.layer.cornerRadius=3;roundView.backgroundColor=[UIColor grayColor];roundView.tag=100+i;[self addSubview:roundView];}else{roundView=[[UIView alloc] initWithFrame:CGRectMake([self.data[i][@"x"] floatValue]*barWidth+xorignal-3, yorignal-[self.data[i][@"y"] floatValue]*barHeight-3, 6, 6)];}}self.lineChartLayer = [CAShapeLayer layer];self.lineChartLayer.path = path.CGPath;//线的颜色self.lineChartLayer.strokeColor = [UIColor greenColor].CGColor;self.lineChartLayer.fillColor = [[UIColor clearColor] CGColor];self.lineChartLayer.lineCap = kCALineCapRound;self.lineChartLayer.lineJoin = kCALineJoinRound;[self.gradientBackgroundView.layer addSublayer:self.lineChartLayer];//直接添加导视图上
}#pragma mark 渐变的颜色
- (void)drawGradientBackgroundView {// 渐变背景视图(不包含坐标轴)self.gradientBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];[self addSubview:self.gradientBackgroundView];/** 创建并设置渐变背景图层 *///初始化CAGradientlayer对象,使它的大小为渐变背景视图的大小self.gradientLayer = [CAGradientLayer layer];self.gradientLayer.frame = self.gradientBackgroundView.bounds;//设置渐变区域的起始和终止位置(范围为0-1),即渐变路径self.gradientLayer.startPoint = CGPointMake(0, 0.0);self.gradientLayer.endPoint = CGPointMake(1.0, 0.0);//设置颜色的渐变过程self.gradientLayerColors = [NSMutableArray arrayWithArray:@[(__bridge id)[UIColor whiteColor].CGColor, (__bridge id)[UIColor whiteColor].CGColor]];self.gradientLayer.colors = self.gradientLayerColors;//将CAGradientlayer对象添加在我们要设置背景色的视图的layer层[self.gradientBackgroundView.layer addSublayer:self.gradientLayer];}//开始画(动画)
-(void)beginDraw
{if(self.lineChartLayer!=nil){[self.lineChartLayer removeFromSuperlayer];}[self dravLine];self.lineChartLayer.lineWidth = 2;CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];pathAnimation.duration = 1.0;pathAnimation.repeatCount = 1;pathAnimation.removedOnCompletion = YES;pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];// 设置动画代理,动画结束时添加一个标签,显示折线终点的信息pathAnimation.delegate = self;[self.lineChartLayer addAnimation:pathAnimation forKey:@"strokeEnd"];
}

iOS开发交流群:301058503

iOS开发(OC)——折线图的绘制(带动画)相关推荐

  1. Unity3D开发之折线图制作

    因为之前有人问我有没有源码,看这篇博客没看懂.所以最近博主又把这个实现的代码精简了下放到这里.有兴趣的可以来看下.点击打开链接 . 柱状图.折线图这个在数据可视化中会经常用到,可是unity自身并不带 ...

  2. 数据处理-数据可视化-折线图的绘制/柱状图绘制(指数级坐标轴)(python版)

    一:折线图的绘制 #encoding=utf-8 import matplotlib.pyplot as plt plt.rcParams['font.sans-serif'] = ['Arial U ...

  3. ios开发中如何调用苹果自带地图导航

    前段时间一直在赶项目,在外包公司工作就是命苦,天天加班不说,工作都是和工期合同挂钩的,稍微逾期就有可能被扣奖金,不谈这些伤脑筋的事情了,让我们说说iOS开发中如何调用苹果手机自带的地图. 学习如逆水行 ...

  4. 【IOS开发高级系列】异步绘制专题

    1 图片处理 1.1 编辑图片的几个方法 第一种 先用UIImage对象加载一张图片 然后转化成CGImageRef放到CGContext中去编辑 第二种 用CGImageCreate函数创建CGIm ...

  5. ios开发oc高仿京东金融白条额度余额的 ios开发水波纹 ios开发水正弦曲线波纹 ios开发雷达扫描的动画效果

    ios开发oc高仿京东金融白条额度余额的   ios开发水波纹   ios开发水正弦曲线波纹 直接上代码,复制粘贴就可以 vc里的 WaterRippleView *topView = [[Water ...

  6. ios开发oc高仿京东金融白条额度余额的 ios开发水波纹 ios开发水正弦曲线波纹 ios开发雷达扫描的动画效果...

    ios开发oc高仿京东金融白条额度余额的   ios开发水波纹   ios开发水正弦曲线波纹 直接上代码,复制粘贴就可以 vc里的 WaterRippleView *topView = [[Water ...

  7. iOS开发CoreAnimation解读之一——初识CoreAnimation核心动画编程

    iOS开发CoreAnimation解读之一--初识CoreAnimation核心动画编程 一.引言 二.初识CoreAnimation 三.锚点对几何属性的影响 四.Layer与View之间的关系 ...

  8. GDI+入门(5、在GDI+中绘制带动画效果的图片)

    五.在GDI+中绘制带动画效果的图片 private void Form2_Load(object sender, EventArgs e) { SetStyle(ControlStyles.Doub ...

  9. Echarts折线图的平移假动画

    Echarts折线图的平移假动画 #可视化项目中经常用图表的方式来展示数据,折线图是经常出现的,但是在echarts折线图中正常情况是通过删除数据的第一个,添加一个新数据在数组的末尾来实现动画,此动画 ...

最新文章

  1. 演讲十忌(翻译并制作成PPT)
  2. 1. golang 语言环境安装
  3. Linux常用基本命令(cut)
  4. visual studio 按钮判断管理员和用户_用户管理的设计原则
  5. Runtime error 216 at xxx 故障解决一例
  6. 数据标注:语义分割数据标注工具labelme安装、使用方法
  7. 看动图,读懂光纤通信背后的原理!
  8. unity3d Realistic eye shading 真实的眼睛渲染
  9. 如何给Digspark ATTINY85下载程序
  10. 第十五届全国大学生智能车竞赛安徽赛区获奖名单
  11. zoj 1104 Leaps Tall Buildings(超人不会飞- -。。)
  12. Origin画图技巧之设置框图默认设置
  13. OpenCV彩色图像读取
  14. 深入理解光流法外推雷达回波
  15. linux安装OceanBase数据库
  16. 如何编辑程序(一个简单的程序)
  17. Discuz!论坛教程之去掉附件图片提示下载框方法
  18. /backend_agg.py:238: RuntimeWarning: Glyph 26085 missing from current font.
  19. Endnote下载的pdf文件合并到同一个文件夹
  20. GAMES104 B1+B2 引擎的结构与开发分层

热门文章

  1. 『无欲则无求』Linux软件包管理 — 39、Linux系统软件包介绍
  2. python教程jupyternotebook_搭建Python Jupyter Notebook教程
  3. 计算机键盘的简洁键,一个简单的问题:键盘的NKRO技术是什么?
  4. YOLOv7相较于之前的版本有哪些优点,具体说明一下
  5. Windows10为Oracle开防火墙
  6. WPS删除第一页的页眉
  7. Crypto模块安装方法
  8. 极速围观,AI奇艺是如何出卖百度的!官方自己出的工具高速下载百度网盘文件。
  9. C4D模型工具—选择平滑着色(Phong)断开边
  10. 细数1款国内外贸开源商城系统和15款国外的英文开源商城系统