iPhone绘图关于QuartZ中绘制Line和虚线等

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案例

posted @ 2011-12-02 15:56  jacktu  阅读(2223)  评论(1编辑  收藏  举报