在iPhone上使用Quartz绘制虚线

时间:2010-06-28 13:42:08

标签: iphone drawing quartz-graphics dotted-line

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

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

但我看到的是虚线而不是虚线。我怎样才能得到虚线?

2 个答案:

答案 0 :(得分: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);

此代码应该可以正常工作。

答案 1 :(得分:0)

请参阅以下关于线路属性角色的精彩页面! https://horseshoe7.wordpress.com/2014/07/16/core-graphics-line-drawing-explained/

根据上面的页面,这里是'dot'行的代码,如(...)

// 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);
相关问题