문제

I am developing an application in which I need to draw dotted lines between a couple of points. I tried

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

But I see dashed lines instead of dotted lines. How can I get dotted lines instead?

도움이 되었습니까?

해결책

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);

This code should work correctly.

다른 팁

Please, see the following great page about the roles of line properties! https://horseshoe7.wordpress.com/2014/07/16/core-graphics-line-drawing-explained/

According to the above page, here is the code for the 'dot' line like ( . . . .)

// 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);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top