在iPhone上使用Quartz绘制虚线

9

我正在开发一款应用程序,需要在几个点之间绘制虚线。我尝试过

CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound)
CGContextSetLineDash(UIGraphicsGetCurrentContext(), 0, lengths, LENGTH_OF_ARRAY)

但是我看到的是虚线而不是点线。如何获得点线?


https://developer.apple.com/library/ios/#documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_paths/dq_paths.html - Jay
这是一篇旧帖子,但上面的评论实际上没有帮助。那个链接并没有涵盖绘制点的内容。 - Daniel Farrell
1
当然可以。在“绘制路径”一节中,有一个完整的部分描述了如何使用CGContextSetLineDash绘制“线条虚线模式”。https://developer.apple.com/library/ios/#documentation/GraphicsImaging/Reference/CGContext/Reference/reference.html#//apple_ref/doc/c_ref/CGContextSetLineDash - pinkeerach
2个回答

12
CGContextRef context = UIGraphicsGetCurrentContext();
CGFloat lengths[2];
lengths[0] = 0;
lengths[1] = dotRadius * 2;
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, dotRadius);
CGContextSetLineDash(context, 0.0f, lengths, 2);

// CGContextAddEllipseInRect(context, self.bounds);

这段代码应该可以正常运行。


0
请查看以下关于线条属性角色的精彩页面!https://horseshoe7.wordpress.com/2014/07/16/core-graphics-line-drawing-explained/ 根据上述页面,以下是“点”线的代码(. . . .)。
// should
CGContextSetLineCap(context, kCGLineCapRound);

// please see the role of line properties why the first should be 0 and the second should be the doulbe of the given line width
CGFloat dash[] = {0, lineWidth*2};

// the second value (0) means the span between sets of dot patterns defined by dash array
CGContextSetLineDash(context, 0, dash, 2);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接