Quartz2D(4)
来源:互联网 发布:宅立方 知乎 编辑:程序博客网 时间:2024/05/29 19:53
- (void)drawRect:(CGRect)rect 2 { 3 //获取上下文 4 CGContextRef ctx=UIGraphicsGetCurrentContext(); 5 //绘图 6 //第一条线 7 CGContextMoveToPoint(ctx, 20, 100); 8 CGContextAddLineToPoint(ctx, 100, 320); 9 10 //第二条线11 CGContextMoveToPoint(ctx, 40, 200);12 CGContextAddLineToPoint(ctx, 80, 100);13 //渲染14 CGContextStrokePath(ctx);15 16 }
效果图:
- (void)drawRect:(CGRect)rect 2 { 3 //获取上下文 4 CGContextRef ctx=UIGraphicsGetCurrentContext(); 5 //绘图 6 //第一条线 7 CGContextMoveToPoint(ctx, 20, 100); 8 CGContextAddLineToPoint(ctx, 100, 320); 9 10 //设置第一条线的状态11 //设置线条的宽度12 CGContextSetLineWidth(ctx, 12);13 //设置线条的颜色14 [[UIColor brownColor]set];15 //设置线条两端的样式为圆角16 CGContextSetLineCap(ctx,kCGLineCapRound);17 //对线条进行渲染18 CGContextStrokePath(ctx);19 20 //第二条线21 CGContextMoveToPoint(ctx, 40, 200);22 CGContextAddLineToPoint(ctx, 80, 100);23 //渲染24 CGContextStrokePath(ctx);25 26 }效果图:
新的需求:要让两条线的颜色不一样,要求第二条线变成原版的样子。要达到上面的要求,有以下几种做法:
第一种做法:在对第二条线进行设置的时候,清空它的状态1 - (void)drawRect:(CGRect)rect 2 { 3 //获取上下文 4 CGContextRef ctx=UIGraphicsGetCurrentContext(); 5 //绘图 6 //第一条线 7 CGContextMoveToPoint(ctx, 20, 100); 8 CGContextAddLineToPoint(ctx, 100, 320); 9 10 //设置第一条线的状态11 //设置线条的宽度12 CGContextSetLineWidth(ctx, 12);13 //设置线条的颜色14 [[UIColor brownColor]set];15 //设置线条两端的样式为圆角16 CGContextSetLineCap(ctx,kCGLineCapRound);17 //对线条进行渲染18 CGContextStrokePath(ctx);19 20 //第二条线21 CGContextMoveToPoint(ctx, 40, 200);22 CGContextAddLineToPoint(ctx, 80, 100);23 24 //清空状态25 CGContextSetLineWidth(ctx, 1);26 [[UIColor blackColor]set];27 CGContextSetLineCap(ctx,kCGLineCapButt);28 29 //渲染30 CGContextStrokePath(ctx);31 32 }第二种做法:把第一条线从开始绘制到渲染的代码剪切到第二条线渲染完成之后,这样先绘制并渲染了第一条线,该线并没有对绘制信息进行过设置,显示出来的第二条线即位系统默认的效果。1 - (void)drawRect:(CGRect)rect 2 { 3 //获取上下文 4 CGContextRef ctx=UIGraphicsGetCurrentContext(); 5 //绘图 6 7 //第二条线 8 CGContextMoveToPoint(ctx, 40, 200); 9 CGContextAddLineToPoint(ctx, 80, 100);10 11 //清空状态12 // CGContextSetLineWidth(ctx, 1);13 // [[UIColor blackColor]set];14 15 // CGContextSetLineCap(ctx,kCGLineCapButt);16 17 //渲染18 CGContextStrokePath(ctx);19 20 //第一条线21 CGContextMoveToPoint(ctx, 20, 100);22 CGContextAddLineToPoint(ctx, 100, 320);23 24 //设置第一条线的状态25 //设置线条的宽度26 CGContextSetLineWidth(ctx, 12);27 //设置线条的颜色28 [[UIColor brownColor]set];29 //设置线条两端的样式为圆角30 CGContextSetLineCap(ctx,kCGLineCapRound);31 //对线条进行渲染32 CGContextStrokePath(ctx);33 }两种方式完成的效果相同:
图形上下文栈1.简单说明在获取图形上下文之后,通过CGContextSaveGState(ctx);
方法,把当前获取的上下文拷贝一份,保存一份最纯洁的图形上下文。在画第二条线之前,使用CGContextRestoreGState(ctx);方法,还原开始的时候保存的那份最纯洁的图形上下文。代码:注意:在栈里保存了几次,那么就可以取几次(比如不能保存了1次,取两次,在取第二次的时候,栈里为空会直接挂掉)。1 - (void)drawRect:(CGRect)rect 2 { 3 //获取上下文 4 CGContextRef ctx=UIGraphicsGetCurrentContext(); 5 //保存一份最初的图形上下文 6 CGContextSaveGState(ctx); 7 8 //绘图 9 //第一条线10 CGContextMoveToPoint(ctx, 20, 100);11 CGContextAddLineToPoint(ctx, 100, 320);12 13 //设置第一条线的状态14 //设置线条的宽度15 CGContextSetLineWidth(ctx, 12);16 //设置线条的颜色17 [[UIColor brownColor]set];18 //设置线条两端的样式为圆角19 CGContextSetLineCap(ctx,kCGLineCapRound);20 //对线条进行渲染21 CGContextStrokePath(ctx);22 23 //还原开始的时候保存的那份最纯洁的图形上下文24 CGContextRestoreGState(ctx);25 //第二条线26 CGContextMoveToPoint(ctx, 40, 200);27 CGContextAddLineToPoint(ctx, 80, 100);28 29 //清空状态30 // CGContextSetLineWidth(ctx, 1);31 // [[UIColor blackColor]set];32 // CGContextSetLineCap(ctx,kCGLineCapButt);33 34 //渲染35 CGContextStrokePath(ctx);36 }
0 0
- Quartz2D(4)
- Quartz2D-4(图片裁剪)
- Quartz2D使用(截屏)
- quartz2d
- Quartz2d
- Quartz2D
- Quartz2D
- Quartz2D
- Quartz2D
- Quartz2D
- Quartz2D
- Quartz2D
- Quartz2D
- QuartZ2D
- Quartz2D
- Quartz2D
- Quartz2D
- Quartz2D
- CSU 1724 相等距离的和(离线+线段树)
- iOS极光推送 点击推送消息跳转页面
- Address 192.168.1.108 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-
- 设计模式读书笔记-----中介者模式
- kafka学习一:入门
- Quartz2D(4)
- python压缩解压
- 6-15位字母加数字密码验证
- 2.1.常用位操作符
- Qt实现IP输入框
- Android 移动端网络优化
- oracle 数据库,无效的SCN号错误
- Android 搜索到的关键字改变颜色
- 设计模式读书笔记-----备忘录模式