2010-06-28 80 views
9

我正在開發一個應用程序,我需要在幾個點之間繪製虛線。我試過在iPhone上使用Quartz繪製虛線

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

但我看到的是虛線而不是虛線。我怎樣才能得到虛線?

+0

https://developer.apple.com/library/ios/# documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_paths/dq_paths.html – Jay 2010-06-28 18:05:40

+0

這是一篇舊文章,但上述評論實際上並沒有幫助。該鏈接不包括繪製點。 – 2012-07-20 13:48:03

+1

確實如此。有一個關於「繪製路徑」的完整部分,它描述瞭如何使用CGContextSetLineDash繪製「線條短劃線樣式」https://developer.apple.com/library/ios/#documentation/GraphicsImaging/Reference/CGContext/Reference/reference的.html#// apple_ref/DOC/c_ref/CGContextSetLineDash – pinkeerach 2013-03-13 17:53:00

回答

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