IPhone plotting Case Study on Line in QuartZ

Source: Internet
Author: User

IPhone plottingAboutQuartZMediumDrawThe Line case is the content to be introduced in this article. It mainly introduces howQuartZMediumDrawLine Content, see the details. The following code and examples are from the officialQuartzThe screenshot in the Demo is written here for future use.

1. Basic underlined code.

 
 
  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])); 

The effect is as follows:

2. Draw dotted lines

 
 
  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 [] is an CGFloat array. dashCount indicates the number of array elements. The following figure shows the dotted line in five cases:

{10.0, 10.0}, 2}. For example, the default value of dashPhase is 0.

The value of dash pattern {10, 10} indicates that 10 points are first drawn, 10 points are skipped, and then repeated.

{10.0, 20.0, 10.0}, 3}, for example:

This is an odd example: first draw 10 points, then skip 20 points, then draw 10 points, then skip 10 points, then draw 20 points, skip 10 points, then, repeat.

{10.0, 20.0, 30.0}, 3}, such

{10.0, 20.0, 10.0, 30.0}, 4}, such

{10.0, 10.0, 20.0, 30.0, 50.0}, 5}, for example:

The dashPhase parameter of the function CGContextSetLineDash indicates how many points are skipped when the dotted line is drawn. For example, if dash pattern is {10, 10} And dashPhase is 5, we plot 5 points, skip 10 points, draw 10, skip 10, and draw 10... Duplicate.

Summary:IPhone plottingAboutQuartZMediumDrawI hope this article will help you with the introduction of the Line case! If you want to learn moreIphone plottingFor more information, see:

IPhone plotting Case Study of Polygons in QuartZ

IPhone plotting Case Study on drawing Curves in QuartZ

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.