Core steps for iOS drawing

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.