在iPhone上使用Quartz绘制虚线

kay*_*vee 9 iphone drawing quartz-graphics dotted-line

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

CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound)
CGContextSetLineDash(UIGraphicsGetCurrentContext(), 0, lengths, LENGTH_OF_ARRAY)
Run Code Online (Sandbox Code Playgroud)

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

Den*_*kov 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);
Run Code Online (Sandbox Code Playgroud)

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