绘图

创建新的类,继承于UIView

在view上添加头文件

只需要在自己新创建的view上绘图


图片绘制:将图片直接绘制倒view上

UIImage* image = [UIImage imageNamed:@"5.png"];

[image drawAtPoint:CGPointMake(100, 100)];

//-------------------------------------------------------------------------------------------------------------------------------------------------------


文字绘制:将文字直接绘制倒view上

NSString* str = @"ABCDEFG";

[str drawInRect:CGRectMake(100, 100, 200, 50) withFont:[UIFont systemFontOfSize:30.0]    lineBreakMode:UILineBreakModeCharacterWrap alignment:UITextAlignmentLeft];

换行方式                                                                           对其方式

//-------------------------------------------------------------------------------------------------------------------------------------------------------


绘制线:

    //得到上下文

    CGContextRef ref = UIGraphicsGetCurrentContext();

    //线宽设定

    CGContextSetLineWidth(ref, 10.0);

    //线的边角样式(圆角型)

    CGContextSetLineCap(ref, kCGLineCapRound);

    CGContextSetLineJoin(ref, kCGLineJoinRound);


    //虚线

    float lengths[] = {20,10};

    //(上下文,起点的偏移量,事例描述的时20像素的虚线10的空格,数组有2个元素)

    CGContextSetLineDash(ref, 0, lengths, 2);

    //如果要恢复线的属性只需要输入null即可

    CGContextSetLineDash(ref, 0, NULL, 0);

    //线条颜色

    CGContextSetStrokeColorWithColor(ref, [UIColor purpleColor].CGColor);


    //移动绘图点

    CGContextMoveToPoint(ref, 100, 100);

    //绘制直线

    CGContextAddLineToPoint(ref, 220, 220);

    CGContextAddLineToPoint(ref, 100, 220);

    //封闭(不需要绘制第三条线,运行封闭语句系统自动完成图形的绘制)

    CGContextClosePath(ref);

    //开始绘制线并在view上显示

    CGContextStrokePath(ref);

//-------------------------------------------------------------------------------------------------------------------------------------------------------


绘制图:

    //得到上下文

    CGContextRef ref = UIGraphicsGetCurrentContext();

    //线宽设定

    CGContextSetLineWidth(ref, 5.0);

    //填充的颜色

    CGContextSetFillColorWithColor(ref, [UIColor purpleColor].CGColor);

    //线条颜色

    CGContextSetStrokeColorWithColor(ref, [UIColor blueColor].CGColor);

    //透明度

    CGContextSetAlpha(ref, 0.5);

    //影子的偏移量,颜色

    CGContextSetShadowWithColor(ref, CGSizeMake(20, 20), 20, [UIColor grayColor].CGColor);

    //绘制圆型

    CGContextAddRect(ref, CGRectMake(100, 100, 100, 100));

    //图形显示方式为:线条填充图一起显示(此方法一定要在绘制图形完成后使用,否则无效)

    CGContextDrawPath(ref, kCGPathFillStroke);

    CGContextStrokePath(ref);

//-------------------------------------------------------------------------------------------------------------------------------------------------------


绘制半圆:

    CGContextRef ref = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(ref, [UIColor redColor].CGColor);

    //如果没有这句,那么下面绘制就是一个从0度到100度的圆型的一部分,因为没有设置move点

    CGContextMoveToPoint(ref, 150, 150);

    //(100为半径,最后的0说明时顺时针[逆时针为1])

    CGContextAddArc(ref, 150, 150, 100, 0 * M_PI / 180, 100 * M_PI / 180, 0);

    CGContextFillPath(ref);

//---------------------------------

    CGContextSetFillColorWithColor(ref, [UIColor greenColor].CGColor);

    CGContextMoveToPoint(ref, 150, 150);

    CGContextAddArc(ref, 150, 150, 100, 100 * M_PI / 180, 230 * M_PI / 180, 0);

    CGContextFillPath(ref);

    CGContextSetFillColorWithColor(ref, [UIColor blueColor].CGColor);

    CGContextMoveToPoint(ref, 150, 150);

    CGContextAddArc(ref, 150, 150, 100, 230 * M_PI / 180, 360 * M_PI / 180, 0);

    CGContextFillPath(ref);

//-------------------------------------------------------------------------------------------------------------------------------------------------------


绘制弧线:


    CGContextMoveToPoint(ref, 100, 350);

    //(120,250)是弧线顶点的坐标 (300,350)是弧线最右端的坐标

    CGContextAddQuadCurveToPoint(ref, 120, 250, 300, 350);

 

    CGContextStrokePath(ref);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值