惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

aimingoo的专栏
aimingoo的专栏
雷峰网
雷峰网
博客园 - 叶小钗
V
V2EX
博客园 - Franky
博客园_首页
小众软件
小众软件
Microsoft Security Blog
Microsoft Security Blog
The GitHub Blog
The GitHub Blog
A
About on SuperTechFans
H
Hackread – Cybersecurity News, Data Breaches, AI and More
阮一峰的网络日志
阮一峰的网络日志
WordPress大学
WordPress大学
Blog — PlanetScale
Blog — PlanetScale
J
Java Code Geeks
T
The Blog of Author Tim Ferriss
U
Unit 42
Microsoft Azure Blog
Microsoft Azure Blog
月光博客
月光博客
S
SegmentFault 最新的问题
B
Blog RSS Feed
博客园 - 【当耐特】
D
Docker
N
Netflix TechBlog - Medium

博客园 - jacktu

git 常用命令 请求处理常见tag语法 mysql执行group by提示only_full_group_by问题解决方法 go test命令(Go语言测试命令)完全攻略 mac下安装Homebrew提示Failed to connect to raw.githubusercontent.com port 443: Connection refused的解决办法【终极最简单版】 【转】Android类动态加载技术 Wireshark图解教程 asp.net 操作excel的一系列问题(未完待续) 不越狱无硬件,轻松搞定iPhone投影与录屏 Android 常见问题之Assets文件大小限制 会说话的TOM猫的原理是什么 android GC 【转】Android 3.1 r1 API中文文档(6)——ImageView android PorterDuffXfermode ,PorterDuff.Mode 使用 以及Porter-Duff规则详解 UIApplicationDelegate协议定义的方法说明 IOS开发网络篇之──ASIHTTPRequest详解 ios5 中文键盘高度变高覆盖现有ui问题的解决方案(获取键盘高度的方法) UILabel小结 【转】iPhone 模态对话框 立即返回结果
iPhone绘图关于QuartZ中绘制Line和虚线等
jacktu · 2011-12-02 · via 博客园 - jacktu

iPhone绘图关于QuartZ绘制Line案例是本文要介绍的内容,主要介绍了如何在QuartZ绘制Line的内容,来看详细内容  。下面的代码和例子都是从官方的QuartzDemo中截取的,在此在写下以便以后用到  。

  1.基本的划线代码  。

  1. CGContextRef context = UIGraphicsGetCurrentContext();  
  2. // Drawing lines with a white stroke color  
  3. CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);  
  4. // Draw them with a 2.0 stroke width so they are a bit more visible.  
  5. CGContextSetLineWidth(context, 2.0);  
  6. // Draw a single line from left to right  
  7. CGContextMoveToPoint(context, 10.0, 30.0);  
  8. CGContextAddLineToPoint(context, 310.0, 30.0);  
  9. CGContextStrokePath(context);  
  10. // Draw a connected sequence of line segments  
  11. CGPoint addLines[] =  
  12. {  
  13. CGPointMake(10.0, 90.0),  
  14. CGPointMake(70.0, 60.0),  
  15. CGPointMake(130.0, 90.0),  
  16. CGPointMake(190.0, 60.0),  
  17. CGPointMake(250.0, 90.0),  
  18. CGPointMake(310.0, 60.0),  
  19. };  
  20. // Bulk call to add lines to the current path.  
  21. // Equivalent to MoveToPoint(points[0]); for(i=1; i<count; ++i) AddLineToPoint(points[i]);  
  22. CGContextAddLines(context, addLines, sizeof(addLines)/sizeof(addLines[0]));  
  23. CGContextStrokePath(context);  
  24. // Draw a series of line segments. Each pair of points is a segment  
  25. CGPoint strokeSegments[] =  
  26. {  
  27. CGPointMake(10.0, 150.0),  
  28. CGPointMake(70.0, 120.0),  
  29. CGPointMake(130.0, 150.0),  
  30. CGPointMake(190.0, 120.0),  
  31. CGPointMake(250.0, 150.0),  
  32. CGPointMake(310.0, 120.0),  
  33. };  
  34. // Bulk call to stroke a sequence of line segments.  
  35. // Equivalent to for(i=0; i<count; i+=2) { MoveToPoint(point[i]); AddLineToPoint(point[i+1]); StrokePath(); }  
  36. CGContextStrokeLineSegments(context, strokeSegments, sizeof(strokeSegments)/sizeof(strokeSegments[0])); 

  效果如下图:

  iPhone绘图关于QuartZ中绘制Line案例

  2.画虚线

  1. CGContextRef context = UIGraphicsGetCurrentContext();  
  2. CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);  
  3. // Each dash entry is a run-length in the current coordinate system  
  4. // The concept is first you determine how many points in the current system you need to fill.  
  5. // Then you start consuming that many pixels in the dash pattern for each element of the pattern.  
  6. // So for example, if you have a dash pattern of {10, 10}, then you will draw 10 points, then skip 10 points, and repeat.  
  7. // As another example if your dash pattern is {10, 20, 30}, then you draw 10 points, skip 20 points, draw 30 points,  
  8. // skip 10 points, draw 20 points, skip 30 points, and repeat.  
  9. // The dash phase factors into this by stating how many points into the dash pattern to skip.  
  10. // So given a dash pattern of {10, 10} with a phase of 5, you would draw 5 points (since phase plus 5 yields 10 points),  
  11. // then skip 10, draw 10, skip 10, draw 10, etc.  
  12. CGContextSetLineDash(context, dashPhase, dashPattern, dashCount);  
  13. // Draw a horizontal line, vertical line, rectangle and circle for comparison  
  14. CGContextMoveToPoint(context, 10.0, 20.0);  
  15. CGContextAddLineToPoint(context, 310.0, 20.0);  
  16. CGContextMoveToPoint(context, 160.0, 30.0);  
  17. CGContextAddLineToPoint(context, 160.0, 130.0);  
  18. CGContextAddRect(context, CGRectMake(10.0, 30.0, 100.0, 100.0));  
  19. CGContextAddEllipseInRect(context, CGRectMake(210.0, 30.0, 100.0, 100.0));  
  20. // And width 2.0 so they are a bit more visible  
  21. CGContextSetLineWidth(context, 2.0);  
  22. CGContextStrokePath(context); 

  其中CGFloat lengths[]是一个CGFloat类型数组,dashCount表示数组元素的个数  。下面将给出5种情况下虚线的图片

  {{10.0, 10.0}, 2}如下图,先默认dashPhase为0.

  iPhone绘图关于QuartZ中绘制Line案例

  dash pattern为{10,10}表示的是先绘制10 points,然后跳过10 points,然后重复,就向上图  。

  {{10.0, 20.0, 10.0}, 3}如下图:

  iPhone绘图关于QuartZ中绘制Line案例

  这是一个奇数个的例子,就是先绘制10 points, 接着跳过20 points,再绘制10points,接着跳过10 points,再绘制20points,在跳过10 points,然后接着重复  。

  {{10.0, 20.0, 30.0}, 3},如下图

  iPhone绘图关于QuartZ中绘制Line案例

  {{10.0, 20.0, 10.0, 30.0}, 4},如下图

  iPhone绘图关于QuartZ中绘制Line案例

  {{10.0, 10.0, 20.0, 30.0, 50.0}, 5},如下图:

  iPhone绘图关于QuartZ中绘制Line案例

  函数CGContextSetLineDash的函数dashPhase参数表示虚线在绘制的时候跳过多少个points  。举一个例子,dash pattern为{10,10},dashPhase为5,则我们绘制5 points,接着跳过10 points,绘制10, 跳过10 ,绘制10   。  。  。重复  。

  小结:iPhone绘图关于QuartZ绘制Line案例的内容介绍完了,希望本文对你有所帮助!如果想深入了解iphone绘图的更多内容,请参考:

  iPhone绘图关于QuartZ中绘制Polygons案例

  iPhone绘图关于QuartZ中绘制Curves案例