Quartz2D绘图引擎

功能:绘制图形 图形上下文的作用: 保存绘图信息、绘图状态

自定义UI控件的步骤 新建一个类,继承自UIView 实现- (void)drawRect:(CGRect)rect方法,然后在这个方法中,可以: 取得跟当前view相关联的图形上下文

1),获得图形上下文 CGContextRef ctx = UIGraphicsGetCurrentContext();

2),拼接路径(下面代码是搞一条线段) CGContextMoveToPoint(ctx, 10, 10); CGContextAddLineToPoint(ctx, 100, 100);

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]; }

//绘制线 -(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);

[[UIColor orangeColor] set]; // CGContextStrokePath(context);渲染

[[UIColor yellowColor] setFill]; // CGContextFillPath(context);填充 渲染,填充 CGContextDrawPath(context, kCGPathFillStroke); // CGContextDrawPath(context, kCGPathFill); // CGContextDrawPath(context, kCGPathEOFill);

}

//矩形,多边 -(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

转载于:https://my.oschina.net/shaosimingbaifeng/blog/737377

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值