iPhone绘图关于QuartZ中绘制Line和虚线等
iPhone绘图关于QuartZ中绘制Line案例是本文要介绍的内容,主要介绍了如何在QuartZ中绘制Line的内容,来看详细内容 。下面的代码和例子都是从官方的QuartzDemo中截取的,在此在写下以便以后用到 。
1.基本的划线代码 。
- 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]));
效果如下图:
2.画虚线
- 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[]是一个CGFloat类型的数组,dashCount表示数组元素的个数 。下面将给出5种情况下虚线的图片:
{{10.0, 10.0}, 2}如下图,先默认dashPhase为0.
dash pattern为{10,10}表示的是先绘制10 points,然后跳过10 points,然后重复,就向上图 。
{{10.0, 20.0, 10.0}, 3}如下图:
这是一个奇数个的例子,就是先绘制10 points, 接着跳过20 points,再绘制10points,接着跳过10 points,再绘制20points,在跳过10 points,然后接着重复 。
{{10.0, 20.0, 30.0}, 3},如下图
{{10.0, 20.0, 10.0, 30.0}, 4},如下图
{{10.0, 10.0, 20.0, 30.0, 50.0}, 5},如下图:
函数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案例