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.
- CGContextRef context = UIGraphicsGetCurrentContext();
- // Drawing lines with a white stroke color
- CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
- // Draw them with a 2.0 stroke width so they are a bit more visible.
- CGContextSetLineWidth(context, 2.0);
- // Draw a single line from left to right
- CGContextMoveToPoint(context, 10.0, 30.0);
- CGContextAddLineToPoint(context, 310.0, 30.0);
- CGContextStrokePath(context);
- // Draw a connected sequence of line segments
- CGPoint addLines[] =
- {
- CGPointMake(10.0, 90.0),
- CGPointMake(70.0, 60.0),
- CGPointMake(130.0, 90.0),
- CGPointMake(190.0, 60.0),
- CGPointMake(250.0, 90.0),
- CGPointMake(310.0, 60.0),
- };
- // Bulk call to add lines to the current path.
- // Equivalent to MoveToPoint(points[0]); for(i=1; i<count; ++i) AddLineToPoint(points[i]);
- CGContextAddLines(context, addLines, sizeof(addLines)/sizeof(addLines[0]));
- CGContextStrokePath(context);
- // Draw a series of line segments. Each pair of points is a segment
- CGPoint strokeSegments[] =
- {
- CGPointMake(10.0, 150.0),
- CGPointMake(70.0, 120.0),
- CGPointMake(130.0, 150.0),
- CGPointMake(190.0, 120.0),
- CGPointMake(250.0, 150.0),
- CGPointMake(310.0, 120.0),
- };
- // Bulk call to stroke a sequence of line segments.
- // Equivalent to for(i=0; i<count; i+=2) { MoveToPoint(point[i]); AddLineToPoint(point[i+1]); StrokePath(); }
- CGContextStrokeLineSegments(context, strokeSegments, sizeof(strokeSegments)/sizeof(strokeSegments[0]));
The effect is as follows:
2. Draw dotted lines
- CGContextRef context = UIGraphicsGetCurrentContext();
- CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
- // Each dash entry is a run-length in the current coordinate system
- // The concept is first you determine how many points in the current system you need to fill.
- // Then you start consuming that many pixels in the dash pattern for each element of the pattern.
- // So for example, if you have a dash pattern of {10, 10}, then you will draw 10 points, then skip 10 points, and repeat.
- // As another example if your dash pattern is {10, 20, 30}, then you draw 10 points, skip 20 points, draw 30 points,
- // skip 10 points, draw 20 points, skip 30 points, and repeat.
- // The dash phase factors into this by stating how many points into the dash pattern to skip.
- // 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),
- // then skip 10, draw 10, skip 10, draw 10, etc.
- CGContextSetLineDash(context, dashPhase, dashPattern, dashCount);
- // Draw a horizontal line, vertical line, rectangle and circle for comparison
- CGContextMoveToPoint(context, 10.0, 20.0);
- CGContextAddLineToPoint(context, 310.0, 20.0);
- CGContextMoveToPoint(context, 160.0, 30.0);
- CGContextAddLineToPoint(context, 160.0, 130.0);
- CGContextAddRect(context, CGRectMake(10.0, 30.0, 100.0, 100.0));
- CGContextAddEllipseInRect(context, CGRectMake(210.0, 30.0, 100.0, 100.0));
- // And width 2.0 so they are a bit more visible
- CGContextSetLineWidth(context, 2.0);
- 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