功能:绘制图形 图形上下文的做用: 保存绘图信息、绘图状态.net
自定义UI控件的步骤 新建一个类,继承自UIView 实现- (void)drawRect:(CGRect)rect方法,而后在这个方法中,能够: 取得跟当前view相关联的图形上下文继承
1),得到图形上下文 CGContextRef ctx = UIGraphicsGetCurrentContext();ip
2),拼接路径(下面代码是搞一条线段) CGContextMoveToPoint(ctx, 10, 10); CGContextAddLineToPoint(ctx, 100, 100);get
3),绘制路径 CGContextStrokePath(ctx); // CGContextFillPath(ctx);渲染
新建一个起点 void CGContextMoveToPoint(CGContextRef c, CGFloat x, CGFloat y) 添加新的线段到某个点 void CGContextAddLineToPoint(CGContextRef c, CGFloat x, CGFloat y) 添加一个矩形 void CGContextAddRect(CGContextRef c, CGRect rect) 添加一个椭圆 void CGContextAddEllipseInRect(CGContextRef context, CGRect rect) 添加一个圆弧 void CGContextAddArc(CGContextRef c, CGFloat x, CGFloat y, CGFloat radius, CGFloat startAngle, CGFloat endAngle, int clockwise)方法
代码:新建的类.m中 -(void)drawRect:(CGRect)rect { 调用方法 [self drawRect]; [self drawTraniagle]; [self drawRectangle]; [self addArc]; }di
//绘制线 -(void)drawRect { //获取上下文 CGContextRef context=UIGraphicsGetCurrentContext(); //设置起点 CGContextMoveToPoint(context, 20, 20); //设置终点 CGContextAddLineToPoint(context, 100, 100); //设置颜色 [[UIColor redColor] set]; //设置宽度 CGContextSetLineWidth(context, 5); //显示,渲染 CGContextStrokePath(context); //设置起点 CGContextMoveToPoint(context, 20, 20); //设置终点 CGContextAddRect(context, CGRectMake(40, 120, 50, 50)); CGContextStrokePath(context); } //三角 -(void)drawTraniagle { CGContextRef context=UIGraphicsGetCurrentContext(); 起点 CGContextMoveToPoint(context, self.frame.size.width/2, 20); CGContextAddLineToPoint(context, self.frame.size.width/2, 100); CGContextAddLineToPoint(context, self.frame.size.width/2+50, 100); 闭合线段 CGContextClosePath(context);view
[[UIColor orangeColor] set]; // CGContextStrokePath(context);渲染vi
[[UIColor yellowColor] setFill]; // CGContextFillPath(context);填充 渲染,填充 CGContextDrawPath(context, kCGPathFillStroke); // CGContextDrawPath(context, kCGPathFill); // CGContextDrawPath(context, kCGPathEOFill);co
}
//矩形,多边 -(void)drawRectangle { CGContextRef context=UIGraphicsGetCurrentContext(); CGContextMoveToPoint(context, self.frame.size.width/2, 120); CGContextAddLineToPoint(context, self.frame.size.width/2, 150); CGContextAddLineToPoint(context, self.frame.size.width/2+50, 180); CGContextAddLineToPoint(context, self.frame.size.width/2+100, 100); CGContextClosePath(context);
[[UIColor orangeColor] set]; CGContextStrokePath(context); } //五环 -(void)addArc { CGContextRef context=UIGraphicsGetCurrentContext(); CGContextAddArc(context, 50, 300, 40, 0, 2 * M_PI, 0); [[UIColor blueColor] set]; CGContextStrokePath(context);
CGContextRef contextI=UIGraphicsGetCurrentContext(); CGContextAddArc(contextI, 150, 300, 40, 0, 2 * M_PI, 0); [[UIColor blackColor] set]; CGContextStrokePath(contextI);
CGContextRef contextII=UIGraphicsGetCurrentContext(); CGContextAddArc(contextII, 250, 300, 40, 0, 2 * M_PI, 0); [[UIColor redColor] set]; CGContextStrokePath(contextII);
CGContextRef contextIII=UIGraphicsGetCurrentContext(); CGContextAddArc(contextIII, 100, 350, 40, 0, 2 * M_PI, 0); [[UIColor yellowColor] set]; CGContextStrokePath(contextIII);
CGContextRef contextV=UIGraphicsGetCurrentContext(); CGContextAddArc(contextV, 200, 350, 40, 0, 2 * M_PI, 0); [[UIColor greenColor] set]; CGContextStrokePath(contextV); } @end