1.iOS- 绘图API之绘制线条
<pre name="code" class="objc">// // DrawLineView.m // DrawLine // // Created by 程磊 on 15/5/5. // Copyright (c) 2015年 程磊. All rights reserved. // #import "DrawLineView.h" @implementation DrawLineView /** * 重写父类的drawRect方法,此方法是在视图出现时调用 * */ - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext();//获取其绘图内容 CGContextMoveToPoint(context, 100, 100);//将画笔移动某一点 CGContextAddLineToPoint(context, 200, 100);//从初始点直线连接到这一点 CGContextAddLineToPoint(context, 200, 200);//继续直线连到某一点 CGContextMoveToPoint(context, 100, 300);//将画笔再移动到另一点 CGContextAddLineToPoint(context, 300, 200);//直线连接到某一点 CGContextSetRGBStrokeColor(context, 1, 0, 1, 1);//设置线条颜色 CGContextSetLineWidth(context, 5);//设置线条粗细 CGContextSetShadow(context, CGSizeMake(10, 10), 10);//设置线条阴影,最后一个参数是模糊度 CGContextStrokePath(context);//进行呈现,将绘制的线条呈现出来 } @end
2.iOS- 绘图API之绘制矩形
<span style="font-size:14px;">// // DrawRectView.m // DrawLine // // Created by 程磊 on 15/5/5. // Copyright (c) 2015年 程磊. All rights reserved. // #import "DrawRectView.h" @implementation DrawRectView - (id)initWithCoder:(NSCoder *)aDecoder { return [super initWithCoder:aDecoder]; } - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext();//获取当前绘图 CGContextAddRect(context, CGRectMake(100, 100, 100, 100));//添加矩形坐标 CGContextSetRGBFillColor(context, 1, 0, 0, 1);//设置颜色 CGContextFillPath(context);//填充 // CGContextStrokeRect(context, CGRectMake(100, 100, 100, 100));//设置边框 // CGContextSetLineWidth(context, 10);//设置边框宽度 CGContextSetRGBStrokeColor(context, 0, 1, 0, 1);//设置边框颜色 CGContextStrokeRectWithWidth(context, CGRectMake(100, 100, 100, 100), 10);//设置边框及其宽度 } @end</span><span style="font-size: 18px;"> </span>
3.iOS- 绘图API之绘制圆形
<span style="font-size:14px;">// // DrawCircleView.m // DrawLine // // Created by 程磊 on 15/5/6. // Copyright (c) 2015年 程磊. All rights reserved. // #import "DrawCircleView.h" @implementation DrawCircleView - (id)initWithCoder:(NSCoder *)aDecoder { return [super initWithCoder:aDecoder]; } // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextAddArc(context, 150, 200, 100, 0, 2*M_PI, 0); //圆心坐标、半径,弧度,0顺时针,1逆时针 CGContextSetLineWidth(context, 10);//设置线条粗度 CGContextStrokePath(context);//绘制线条 CGContextAddArc(context, 150, 200, 100, 0, 2*M_PI, 0); //圆心坐标、半径,弧度,0顺时针,1逆时针 CGContextSetRGBFillColor(context, 1, 0, 0, 1);//设置填充颜色 CGContextFillPath(context);//填充色 CGContextAddEllipseInRect(context, CGRectMake(100, 400, 50, 50));//这是在矩形中绘制一个圆,如果是正方形则是圆,否则椭圆 CGContextSetLineWidth(context, 5); CGContextStrokePath(context); } @end</span><span style="font-size: 18px;"> </span>
4.iOS- 绘图API之绘制图片
<span style="font-size:14px;">// // DrawImageView.m // DrawLine // // Created by 程磊 on 15/5/6. // Copyright (c) 2015年 程磊. All rights reserved. // #import "DrawImageView.h" @implementation DrawImageView // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { UIImage *image = [UIImage imageNamed:@"20110825172739-14284794.jpg"]; CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSaveGState(context);//保留起始状态 CGContextTranslateCTM(context, 60, 400);//移动图片坐标 CGContextScaleCTM(context, 1, -1);//围绕某一点旋转,因为CGImage与图片坐标相反,所以 CGContextDrawImage(context, CGRectMake(0, 0, 200, 200), image.CGImage);//绘制图片 CGContextRestoreGState(context);//恢复context状态 } @end</span><span style="font-size: 18px;"> </span>
5.画板Demo
</pre><pre name="code" class="objc" style="font-size: 18px;">
<span style="font-size:14px;">// // DrawingBoardView.m // DrawLine // // Created by 程磊 on 15/5/6. // Copyright (c) 2015年 程磊. All rights reserved. // #import "DrawingBoardView.h" @interface DrawingBoardView () @property (nonatomic, assign) CGMutablePathRef path; @end @implementation DrawingBoardView // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext();//获取绘制文本 CGContextAddPath(context, _path);//添加绘制文本以及路径 CGContextStrokePath(context);//绘制 } - (void)awakeFromNib{ _path = CGPathCreateMutable(); } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = touches.anyObject;//获取触摸手势 CGPoint p = [touch locationInView:self];//获取触摸点 CGPathMoveToPoint(_path, nil, p.x, p.y);//移动到初识位置点 } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint p = [touch locationInView:self]; CGPathAddLineToPoint(_path, nil, p.x, p.y);//将其连线 [self setNeedsDisplay];//只要执行这个方法,系统就会认为你需要重绘,就会调用drawRect:(CGRect)rect } @end</span><span style="font-size: 18px;"> </span>