标签:
效果如下:
KMDrawView.h
1 #import <UIKit/UIKit.h> 2 3 @interface KMDrawView : UIView 4 5 @end
KMDrawView.m
1 #import "KMDrawView.h" 2 3 @interface KMDrawView () 4 - (void)drawFont; 5 - (void)drawLine; 6 - (void)drawCircle; 7 - (void)drawRectangle; 8 9 @end 10 11 @implementation KMDrawView 12 13 - (instancetype)initWithFrame:(CGRect)frame{ 14 if (self=[super initWithFrame:frame]) { 15 self.backgroundColor = [UIColor colorWithWhite:0.7 alpha:1.0]; 16 } 17 return self; 18 } 19 20 - (void)drawRect:(CGRect)rect { 21 [self drawFont]; 22 [self drawLine]; 23 [self drawCircle]; 24 [self drawRectangle]; 25 26 [super drawRect:rect]; 27 } 28 29 #pragma mark - 绘制『文字』、『线条』、『圆形』、『矩形』 30 - (void)drawFont { 31 NSDictionary *dicAttribute = @{ 32 NSForegroundColorAttributeName : [UIColor brownColor], 33 NSFontAttributeName : [UIFont systemFontOfSize:18.0] 34 }; 35 36 [@"我是文字" drawInRect:CGRectMake(20.0, 20.0, 100.0, 30.0) withAttributes:dicAttribute]; 37 } 38 39 - (void)drawLine { 40 CGContextRef contextRef = UIGraphicsGetCurrentContext(); //获取绘制上下文对象实例 41 CGContextSetRGBStrokeColor(contextRef, 0.5, 0.5, 0.5, 1.0); //设置笔画颜色 42 CGContextSetLineWidth(contextRef, 2.0); //设置线条粗细大小 43 44 CGContextMoveToPoint(contextRef, 20.0, 100.0); //设置直线的首端 45 CGContextAddLineToPoint(contextRef, 320.0, 100.0); //设置直线的末端 46 CGContextStrokePath(contextRef); //沿着要求的路径,开始绘制 47 } 48 49 - (void)drawCircle { 50 CGContextRef contextRef = UIGraphicsGetCurrentContext(); //获取绘制上下文对象实例 51 CGContextSetRGBStrokeColor(contextRef, 1.0, 1.0, 1.0, 1.0); //设置笔画颜色 52 CGContextSetLineWidth(contextRef, 2.0); //设置线条粗细大小 53 54 //voidCGContextAddArc(CGContextRef c,CGFloat x,CGFloat y,CGFloat radius,CGFloat startAngle,CGFloat endAngle,int clockwise) 55 //1弧度=180°/π(≈57.3°)度 56 //360°=360 * π/180=2π弧度 57 //x,y为圆点坐标,radius半径,startAngle为开始的弧度,endAngle为结束的弧度,clockwise0为顺时针,1为逆时针。 58 CGContextAddArc(contextRef, 70.0, 200.0, 50.0, 0, 2*M_PI, 0); //添加一个圆;M_PI为180度 59 CGContextDrawPath(contextRef, kCGPathStroke); //绘制路径 60 } 61 62 - (void)drawRectangle { 63 CGContextRef contextRef = UIGraphicsGetCurrentContext(); //获取绘制上下文对象实例 64 CGContextSetRGBStrokeColor(contextRef, 0.0, 0.0, 0.0, 1.0); //设置笔画颜色 65 CGContextSetLineWidth(contextRef, 2.0); //设置线条粗细大小 66 67 CGContextAddRect(contextRef, CGRectMake(20.0, 300.0, 200.0, 100.0)); //设置矩形位置和宽高 68 CGContextStrokePath(contextRef); //沿着要求的路径,开始绘制 69 } 70 71 @end
ViewController.h
1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UIViewController 4 5 6 @end
ViewController.m
1 #import "ViewController.h" 2 #import "KMDrawView.h" 3 4 @interface ViewController () 5 - (void)layoutUI; 6 @end 7 8 @implementation ViewController 9 10 - (void)viewDidLoad { 11 [super viewDidLoad]; 12 13 [self layoutUI]; 14 } 15 16 - (void)didReceiveMemoryWarning { 17 [super didReceiveMemoryWarning]; 18 // Dispose of any resources that can be recreated. 19 } 20 21 - (void)layoutUI { 22 KMDrawView *drawView = [[KMDrawView alloc] initWithFrame:self.view.frame]; 23 [self.view addSubview:drawView]; 24 } 25 26 @end
标签:
原文地址:http://www.cnblogs.com/huangjianwu/p/4609252.html