Core Graphics核心绘图 ( 二)
来源:互联网 发布:木马编程详解 编辑:程序博客网 时间:2024/05/12 18:09
上一篇讲了一点core Graplics的基础东西,这一篇继续讲它的一些属性。
1 线条属性
线宽:CGContextSetLineWidth
连接样式:CGContextSetLineJoin
顶点样式:CGContextSetLineCap
2 绘制虚线
CGContextSetLineDash(ctx, 0, lengths, sizeof(lengths) / sizeof(lengths[0]));
3 设置颜色
填充颜色 [[UIColor redColor] setFill];
描边颜色 [[UIColor yellowColor] setStroke];
填充和描边颜色 [[UIColor blueColor] set];
4 渲染函数
仅描边 CGContextStrokePath(ctx);
仅填充 CGContextFillPath(ctx);
描边并填充 CGContextDrawPath(ctx, kCGPathFill);
填充模式 —— 奇偶填充
1)设置线条属性
- (void)drawRect:(CGRect)rect { // 1. 获取上下文 CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextMoveToPoint(ctx, 50, 50); CGContextAddLineToPoint(ctx, 200, 50); CGContextAddLineToPoint(ctx, 200, 200); CGContextClosePath(ctx); // 1> 设置线宽 CGContextSetLineWidth(ctx, 10); // 2> 设置连接样式 //CGContextSetLineJoin(ctx, kCGLineJoinBevel); // 3> 设置顶点样式 CGContextSetLineCap(ctx, kCGLineCapRound); CGContextStrokePath(ctx);}
效果:
2)绘制虚线
- (void)drawRect:(CGRect)rect { // 1. 获取上下文 CGContextRef ctx = UIGraphicsGetCurrentContext(); // 2. 移动到起始点 CGContextMoveToPoint(ctx, 0, 50); CGContextAddLineToPoint(ctx, 300, 50); // 1> 设置线宽 CGContextSetLineWidth(ctx, 80); // 2> 设置虚线 CGFloat lengths[] = {8,10,3,5}; /** 参数 1. 上下文 2. 起始位置 3. 长度数组 4. 数组计数 */ CGContextSetLineDash(ctx, 0, lengths, sizeof(lengths)/sizeof(lengths [0])); CGContextStrokePath(ctx); }
效果:
3) 设置颜色
// 1. 获取上下文 CGContextRef ctx = UIGraphicsGetCurrentContext(); // 2. 移动到起始点 CGContextMoveToPoint(ctx, 0, 50); CGContextAddLineToPoint(ctx, 300, 50); // 1> 设置线宽 CGContextSetLineWidth(ctx, 80); // 2> 设置虚线 /** 参数 1. 上下文 2. 起始位置 3. 长度数组 4. 数组计数 */ CGFloat lengths[] = {8,10,3,5}; CGContextSetLineDash(ctx, 0, lengths, sizeof(lengths)/sizeof(lengths [0])); [[UIColor yellowColor] setStroke]; CGContextStrokePath(ctx);
效果:
4)填充模式 —— 奇偶填充
- (void)drawFill{ CGContextRef ctx = UIGraphicsGetCurrentContext(); //绘制一个矩形 CGRect rect = CGRectMake(50, 50, 200, 200); CGContextAddRect(ctx, rect); //绘制一个园 CGContextAddEllipseInRect(ctx, rect); //设置填充颜色 [[UIColor greenColor] set]; //填充 - 被覆盖过奇数次填充,偶数不填充 CGContextDrawPath(ctx, kCGPathEOFillStroke);}
效果:
绘图路径
1)使用路径绘制
- (void)drawPath{ //获取上下文 CGContextRef ctx = UIGraphicsGetCurrentContext(); //创建路径 CGMutablePathRef path = CGPathCreateMutable(); //向路径中添加形状 CGPathAddRect(path, NULL, CGRectMake(50, 50, 200, 200)); //把路径添加到上下文 CGContextAddPath(ctx, path); //释放路径 CGPathRelease(path); //设置绘制属性 CGContextSetLineWidth(ctx, 10); //设置颜色 [[UIColor greenColor] set]; //渲染 CGContextDrawPath(ctx, kCGPathStroke);}
效果:
注意:
CoreGraphics 框架里面的使用到 create、copy、retain 函数创建的对象,最后都需要进行释放。ARC
自动引用计数只是负责 OC 部分代码的 retain/release/autorelase,而 C
语言的框架,绝大多数的内存管理仍然需要程序员自行处理
2) 路径画园
- (void)drawFill2{ // 1. 获取上下文 CGContextRef ctx = UIGraphicsGetCurrentContext(); { CGMutablePathRef path = CGPathCreateMutable(); CGPathAddArc(path, NULL, 150, 150, 100, 0, 2 * M_PI, 0); CGContextAddPath(ctx, path); CGPathRelease(path); } { CGMutablePathRef path = CGPathCreateMutable(); CGPathAddArc(path, NULL, 150, 150, 50, 0, 2 * M_PI, 1); CGContextAddPath(ctx, path); CGPathRelease(path); } // 4. 设置颜色 [[UIColor greenColor] set]; // 5. 渲染 - 计数为 0 不填充 CGContextDrawPath(ctx, kCGPathFill);}
效果:
3)关于 drawRect
为什么要实现 drawRect: 方法才能绘图到 view 上?
drawRect: 是将视图渲染到屏幕上时,系统自动调用的
只有在 drawRect: 方法中才能够获取到屏幕对应的上下文,在其他方法中通过 UIGraphicsGetCurrentContext 无法获得上下文
drawRect: 方法在什么时候被调用?
当 view 第一次显示到屏幕上时(加到 UIWindow 上显示出来)
手动调用重绘方法 setNeedsDisplay 或者 setNeedsDisplayInRect: 时会被调用
drawRect: 方法的 rect 参数表示什么含义?
参数 rect 表示当前 View 的 bounds
注意: 开发中视图的绘制是有系统调用的,程序员不要直接调用 drawRect 方法 如果需要,可以调用 setNeedsDisplay
让视图重绘
- Core Graphics核心绘图 ( 二)
- Core Graphics核心绘图 ( 一)
- Core Graphics核心绘图 ( 三) --贝塞尔曲线
- 核心绘图——Core Graphics
- iOS中核心绘图Core Graphics的小试
- 媒体层:Core Graphics(绘图)
- core graphics绘图1
- Core Graphics 绘图
- Core Graphics绘图
- UIKit和Core Graphics绘图(二)——构造路径,阴影以及渐变扩展
- Core Graphics绘图实例代码
- Core Graphics 绘图及Demo
- Swift- Core Graphics绘图框架详解2(绘制图形)
- 使用Core Graphics 进行iOS绘图
- UIKit与Core Graphics绘图技术详解
- iOS绘图系统UIKit与Core Graphics
- iOS开发--Core Graphics绘图
- android.graphics包-核心渲染包(绘图)
- Spark性能优化指南——高级篇
- css3实现文字溢出处理(精品代码)
- WebSocket
- mysql常用函数
- LeetCode:Burst Balloons
- Core Graphics核心绘图 ( 二)
- java线程(二)x
- Android提示版本更新
- 《Win7设备驱动程序开发》学习笔记
- 计算机各层网络协议
- 论文系统Step1:从日志记录中提取特定信息
- Android Scroll分析
- 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。
- Python 文件处理方法详解