【iOS】iPhone绘图关于QuartZ中绘制Polygons
来源:互联网 发布:二叉树层次遍历java 编辑:程序博客网 时间:2024/04/28 07:42
iPhone绘图关于QuartZ中绘制Polygons案例是本文要介绍的内容,主要介绍了如何在QuartZ中绘制Polygons的内容,内容不多,主要是基于代码实现,一起来看这个有趣的案例。
1.绘制矩形的一般方法
- //Drawingwithawhitestrokecolor
- CGContextSetRGBStrokeColor(context,1.0,1.0,1.0,1.0);
- //Anddrawingwithabluefillcolor
- CGContextSetRGBFillColor(context,0.0,0.0,1.0,1.0);
- //Drawthemwitha2.0strokewidthsotheyareabitmorevisible.
- CGContextSetLineWidth(context,2.0);
- //AddRecttothecurrentpath,thenstrokeit
- CGContextAddRect(context,CGRectMake(30.0,30.0,60.0,60.0));
- CGContextStrokePath(context);
- //StrokeRectconveniencethatisequivalenttoabove
- CGContextStrokeRect(context,CGRectMake(30.0,120.0,60.0,60.0));
- //Strokerectconvenienceequivalenttotheabove,plusacalltoCGContextSetLineWidth().
- CGContextStrokeRectWithWidth(context,CGRectMake(30.0,210.0,60.0,60.0),10.0);
- //Demonstatethestrokeisonbothsidesofthepath.
- CGContextSaveGState(context);
- //red
- CGContextSetRGBStrokeColor(context,1.0,0.0,0.0,1.0);
- CGContextStrokeRectWithWidth(context,CGRectMake(30.0,210.0,60.0,60.0),2.0);
- CGContextRestoreGState(context);
- CGRectrects[]=
- {
- CGRectMake(120.0,30.0,60.0,60.0),
- CGRectMake(120.0,120.0,60.0,60.0),
- CGRectMake(120.0,210.0,60.0,60.0),
- };
- //Bulkcalltoaddrectstothecurrentpath.
- CGContextAddRects(context,rects,sizeof(rects)/sizeof(rects[0]));
- CGContextStrokePath(context);
- //Createfilledrectanglesviatwodifferentpaths.
- //Add/Fillpath
- CGContextAddRect(context,CGRectMake(210.0,30.0,60.0,60.0));
- CGContextFillPath(context);
- //Fillconvienience.
- CGContextFillRect(context,CGRectMake(210.0,120.0,60.0,60.0));
注释:
- CGContextAddRect(context,CGRectMake(30.0,30.0,60.0,60.0));
- CGContextStrokePath(context);
此两句绘制的是左上角的矩形,当CGContextStrokePath调用之后,current path会被清空。
- CGContextStrokeRect(context,CGRectMake(30.0,120.0,60.0,60.0));
上面的一条语句等价于上面的两条。
语句
- CGContextStrokeRectWithWidth(context,CGRectMake(30.0,210.0,60.0,60.0),10.0)
等价与上面的语句在加上CGContextSetLineWidth(10.0)
下面的三条语句通过两种方法来fill矩形区域。
- CGContextAddRect(context,CGRectMake(210.0,30.0,60.0,60.0));
- CGContextFillPath(context);
- //Fillconvienience.
- CGContextFillRect(context,CGRectMake(210.0,120.0,60.0,60.0));
结果如下图:
2.绘制多边形(Polygon)
- //Drawingwithawhitestrokecolor
- CGContextSetRGBStrokeColor(context,1.0,1.0,1.0,1.0);
- //Drawingwithabluefillcolor
- CGContextSetRGBFillColor(context,0.0,0.0,1.0,1.0);
- //Drawthemwitha2.0strokewidthsotheyareabitmorevisible.
- CGContextSetLineWidth(context,2.0);
- CGPointcenter;
- //Addastartothecurrentpath
- center=CGPointMake(90.0,90.0);
- CGContextMoveToPoint(context,center.x,center.y+60.0);
- for(inti=1;i<5;++i)
- {
- CGFloatx=60.0*sinf(i*4.0*M_PI/5.0);
- CGFloaty=60.0*cosf(i*4.0*M_PI/5.0);
- CGContextAddLineToPoint(context,center.x+x,center.y+y);
- }
- //Andclosethesubpath.
- CGContextClosePath(context);
- //Nowaddthehexagontothecurrentpath
- center=CGPointMake(210.0,90.0);
- CGContextMoveToPoint(context,center.x,center.y+60.0);
- for(inti=1;i<6;++i)
- {
- CGFloatx=60.0*sinf(i*2.0*M_PI/6.0);
- CGFloaty=60.0*cosf(i*2.0*M_PI/6.0);
- CGContextAddLineToPoint(context,center.x+x,center.y+y);
- }
- //Andclosethesubpath.
- CGContextClosePath(context);
- //Nowdrawthestar&hexagonwiththecurrentdrawingmode.
- CGContextDrawPath(context,drawingMode);
我们会根据drawingMode的五个常量讨论
- kCGPathFill,kCGPathEOFill,kCGPathStroke,kCGPathFillStroke,orkCGPathEOFillStroke.
(1)kCGPathFill如下图:
此fill 模式为缺省模式(非零缠绕数原则),大概规则为,在需要填充颜色的区域的一点向画区域外画一条线,g如果是从左向右穿过的,则加1,如果从右向左穿过,则减一,最后结果为0则不fill,大于0则填充,所以line的方向对fill的区域有影响。
还有一种为even-odd(奇偶原则),只计算line穿过path段的个数,为偶数时,不填充,奇数时填充,所以path的方向不会影响填充的结果。
(2) kCGPathEOFill模式
此填充模式为奇偶模式
(3)kCGPathStroke模式
(4)kCGPathFillStroke模式
(5)kCGPathEOFillStroke模式
小结:iPhone绘图关于QuartZ中绘制Polygons案例的内容介绍完了,希望本文对你有所帮助!
转载自点击打开链接
0 0
- 【iOS】iPhone绘图关于QuartZ中绘制Polygons
- 【IOS 开发学习总结-OC-66】Quartz 2D绘图(4-3)——绘制曲线+在内存中绘图+绘制位图
- iOS Quartz 绘图
- iOS Quartz绘图
- ios绘图:绘制图形
- iOS Quartz 2D绘图用CGContextRef绘制三角形 —— HERO博客
- iOS 绘图机制简介,Quartz 2D绘图用CGContextRef绘制音频波形图 —— HERO博客
- iOS 绘图 Quartz 2D
- ios--------------Quartz 2D 绘图
- 关于Quartz 2D绘图
- IOS中使用Quartz 2D绘制虚线
- ios开发中关于绘图的问题
- Iphone Quartz2D 绘图及绘制文字简介
- Iphone Quartz2D 绘图及绘制文字简介
- Iphone Quartz2D 绘图及绘制文字简介
- Iphone Quartz2D 绘图及绘制文字简介
- iOS绘图 - 自定义柱状图绘制
- 关于iPhone开发中的绘图
- WiFi在城市建设中的位置
- 我可以在#include 行里使用#ifdef 来定义两个不同的东西吗?
- VS2010虚拟内存不足报错
- 天猫商城应用自定义类代码
- 简单的MD5密码加密和解密方法
- 【iOS】iPhone绘图关于QuartZ中绘制Polygons
- Set Matrix Zeroes
- C#开发笔记-读取文件,并将文件使用分隔符隔开
- poj 1704 (nim博弈)
- 虚函数
- 浅谈C/C++中的static和extern关键字
- 我得到了一些代码, 里边有太多的#ifdef。我不想使用预处理器 把所有的#include 和#ifdef 都扩展开, 有什么办法只保留一种 条件的代码呢?
- 数据库连接池实现
- 项目管理能力提升四要素