There are many ways to draw a graph on a view, which may not be the same, but the actual steps are the same:
1) Get context
2) Draw the path
3) Add path to context
4) Modify Graphics state parameters
5) Render Context
Let's draw a circle to illustrate its implementation steps:
1) Create a path using Cgcontextref
| 123456789 |
//获取上下文 CGContextRef ctx = UIGraphicsGetCurrentContext(); //绘制路径: 圆形(中心坐标200、200、半径100、起点弧度0、终点弧度2PI、画的方向0逆1正) CGContextAddArc(ctx, 200, 200, 100, 0, M_PI * 2, 0); //修改图形状态参数 CGContextSetRGBStrokeColor(ctx, 0.5, 0.5, 0.9, 1.0);//笔颜色 CGContextSetLineWidth(ctx, 10);//线条宽度 //渲染上下文 CGContextStrokePath(ctx); |
2) Create a path using Cgpathref
| 12345678910111213 |
//获取上下文 CGContextRef ctx = UIGraphicsGetCurrentContext(); //创建可变路径 CGMutablePathRef path = CGPathCreateMutable(); //添加圆形到路径中(所在路径、不进行变换操作、中心坐标200、200、起点弧度0、终点弧度2PI、画的方向0逆1正) CGPathAddArc(path, NULL, 200, 200, 100, 0, M_PI * 2, 1); //将路径添加到上下文 CGContextAddPath(ctx, path); //修改图形状态参数 CGContextSetRGBStrokeColor(ctx, 0.5, 0.5, 0.9, 1.0);//笔颜色 CGContextSetLineWidth(ctx, 10);//线条宽度 //渲染上下文 CGContextStrokePath(ctx); |
3) Create a path using Uibezierpath
| 1234567 |
  //Create path   uibezierpath * Path = [uibezierpath bezierpathwithovalinrect:cgrectmake (100, 100, 200, 200)];   //Modify the graphics state parameters   [[uicolor colorwithred:0.5 green:0.5 blue:0.9 alpha:1.0] Setstroke]; //pen color   [path setlinewidth:10]; //line width   //Render   [path stroke]; |
The above three methods can be drawn, by comparison we can find the use of Uibezierpath to create a path is the most concise and most intuitive, the recommended use of Uibezierpath, in the future we will also apply more Uibezierpath animation in the implementation of the animation.
Core steps for iOS drawing