CoreGraphics简单绘图

来源:互联网 发布:淘宝购物车无法结算 ie 编辑:程序博客网 时间:2024/06/05 21:18

一、导入coreGraphics.framework

二、绘制图形

1、绘制矩形

01// 绘制矩形
02- (void)drawRectangle {
03 
04    // 定义矩形的rect
05    CGRect rectangle = CGRectMake(100, 290, 120, 25);
06     
07    // 获取当前图形,视图推入堆栈的图形,相当于你所要绘制图形的图纸
08    CGContextRef ctx = UIGraphicsGetCurrentContext();
09     
10    // 在当前路径下添加一个矩形路径
11    CGContextAddRect(ctx, rectangle);
12     
13    // 设置试图的当前填充色
14    CGContextSetFillColorWithColor(ctx, [UIColor blackColor].CGColor);
15     
16    // 绘制当前路径区域
17    CGContextFillPath(ctx);
18}

2、绘制椭圆

01// 绘制椭圆
02- (void)drawEllipse {
03 
04    // 获取当前图形,视图推入堆栈的图形,相当于你所要绘制图形的图纸
05    CGContextRef ctx = UIGraphicsGetCurrentContext();
06     
07    // 定义其rect
08    CGRect rectangle = CGRectMake(10, 100, 300, 280);
09     
10    // 在当前路径下添加一个椭圆路径
11    CGContextAddEllipseInRect(ctx, rectangle);
12     
13    // 设置当前视图填充色
14    CGContextSetFillColorWithColor(ctx, [UIColor orangeColor].CGColor);
15     
16    // 绘制当前路径区域
17    CGContextFillPath(ctx);
18}

3、绘制三角形

01// 绘制三角形
02- (void)drawTriangle {
03 
04    // 获取当前图形,视图推入堆栈的图形,相当于你所要绘制图形的图纸
05    CGContextRef ctx = UIGraphicsGetCurrentContext();
06     
07    // 创建一个新的空图形路径。
08    CGContextBeginPath(ctx);
09     
10    /**
11     *  @brief 在指定点开始一个新的子路径 参数按顺序说明
12     *
13     *  @param c 当前图形
14     *  @param x 指定点的x坐标值
15     *  @param y 指定点的y坐标值
16     *
17     */
18    CGContextMoveToPoint(ctx, 160, 220);
19     
20    /**
21     *  @brief 在当前点追加直线段,参数说明与上面一样
22     */
23    CGContextAddLineToPoint(ctx, 190, 260);
24    CGContextAddLineToPoint(ctx, 130, 260);
25     
26    // 关闭并终止当前路径的子路径,并在当前点和子路径的起点之间追加一条线
27    CGContextClosePath(ctx);
28 
29    // 设置当前视图填充色
30    CGContextSetFillColorWithColor(ctx, [UIColor blackColor].CGColor);
31     
32    // 绘制当前路径区域
33    CGContextFillPath(ctx);
34}

4、绘制曲线

01// 绘制曲线
02- (void)drawCurve {
03 
04    // 获取当前图形,视图推入堆栈的图形,相当于你所要绘制图形的图纸
05    CGContextRef ctx = UIGraphicsGetCurrentContext();
06     
07    // 创建一个新的空图形路径。
08    CGContextBeginPath(ctx);
09     
10    /**
11     *  @brief 在指定点开始一个新的子路径 参数按顺序说明
12     *
13     *  @param c 当前图形
14     *  @param x 指定点的x坐标值
15     *  @param y 指定点的y坐标值
16     *
17     */   
18    CGContextMoveToPoint(ctx, 160, 100);
19     
20    /**
21     *  @brief 在指定点追加二次贝塞尔曲线,通过控制点和结束点指定曲线。
22     *         关于曲线的点的控制见下图说明,图片来源苹果官方网站。参数按顺序说明
23     *  @param c   当前图形
24     *  @param cpx 曲线控制点的x坐标
25     *  @param cpy 曲线控制点的y坐标
26     *  @param x   指定点的x坐标值
27     *  @param y   指定点的y坐标值
28     *
29     */
30    CGContextAddQuadCurveToPoint(ctx, 160, 50, 190, 50);
31     
32    // 设置图形的线宽
33    CGContextSetLineWidth(ctx, 20);
34     
35    // 设置图形描边颜色
36    CGContextSetStrokeColorWithColor(ctx, [UIColor brownColor].CGColor);
37     
38    // 根据当前路径,宽度及颜色绘制线
39    CGContextStrokePath(ctx);      
40}
曲线描绘示意图

5、绘制圆形

01//以指定中心点绘制圆弧
02- (void)drawCircleAtX:(float)x Y:(float)y {
03     
04    // 获取当前图形,视图推入堆栈的图形,相当于你所要绘制图形的图纸
05    CGContextRef ctx = UIGraphicsGetCurrentContext();
06     
07    // 创建一个新的空图形路径。
08    CGContextSetFillColorWithColor(ctx, [UIColor blackColor].CGColor);
09     
10    /**
11     *  @brief 在当前路径添加圆弧 参数按顺序说明
12     
13     *  @param c           当前图形
14     *  @param x           圆弧的中心点坐标x
15     *  @param y           曲线控制点的y坐标
16     *  @param radius      指定点的x坐标值
17     *  @param startAngle  弧的起点与正X轴的夹角,
18     *  @param endAngle    弧的终点与正X轴的夹角
19     *  @param clockwise   指定1创建一个顺时针的圆弧,或是指定0创建一个逆时针圆弧
20     *
21     */
22    CGContextAddArc(ctx, x, y, 20, 0, 2 * M_PI, 1);
23     
24    //绘制当前路径区域
25    CGContextFillPath(ctx);
26}

三、在drawRect中调用

01- (void)drawRect:(CGRect)rect {
02 
03    // 绘制椭圆
04    [self drawEllipse];
05     
06    // 绘制三角
07    [self drawTriangle];
08     
09    // 绘制矩形
10    [self drawRectangle];
11     
12    // 绘制曲线
13    [self drawCurve];
14     
15    // 绘制圆形
16    [self drawCircleAtX:120 Y:170];
17 
18    [self drawCircleAtX:200 Y:170];
19     
20}
效果如图:

0 0
原创粉丝点击