IOS - 绘图

来源:互联网 发布:喜马拉雅朗读软件下载 编辑:程序博客网 时间:2024/06/05 03:44

创建新的类,继承于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);

0 0
原创粉丝点击