2011-05-02 176 views
11

我需要畫出下圖enter image description here繪製空心圓在iPhone

灰色的部分是什麼,我想畫在另一幅圖像,我需要使用CGContext上的方法用的是代碼,我嘗試使用CGContextAddArc但因爲當我補充中風時,中心凹陷處也充滿了灰色的質感。

任何幫助表示讚賞。

信息:我有完整的藍色形象,我需要添加藍色圖像上方的半圓

感謝

+0

你如何試圖在使用CGContext上創建一個甜甜圈恥辱,然後加入本作中的層或shapelayer ......對不起,這一點,但我沒有在這個讓我的頭圍繞這個做一個變通瞬間,所以在莫只是拍攝空白 – tosi 2011-12-20 12:58:08

回答

10

在他的答案和一些修改稱爲什麼Ole Begemann進一步的工作,我能達到所要求的。

CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextSetStrokeColorWithColor(context, [UIColor colorWithPatternImage:[UIImage imageNamed:@"background_grey_pattern.png"]].CGColor); 
CGMutablePathRef path = CGPathCreateMutable(); 
CGContextSetLineWidth(context, 40); 
CGPathAddArc(path, NULL, aRect.size.width/2, aRect.size.height/2, 45, 0*3.142/180, angle*3.142/180, 0); 
CGContextAddPath(context, path); 
CGContextStrokePath(context); 
CGPathRelease(path); 

因此,而不是2弧我只使用一個,並撫摸它與更高的寬度。

希望這有助於他人有相同的要求。

+2

也許使用M_PI常數而不是3.142 ... – RobP 2015-06-02 04:02:08

24

有核芯顯卡文檔中看看Filling a Path。基本上,你要做的是在你的路徑(外部和內部)添加兩個弧,然後使用Core Graphics填充規則來獲得優勢。該守則將是這個樣子:

CGMutablePathRef path = CGPathCreateMutable(); 

// Add the outer arc to the path (as if you wanted to fill the entire circle) 
CGPathMoveToPoint(path, ...); 
CGPathAddArc(path, ...); 
CGPathCloseSubpath(path); 

// Add the inner arc to the path (later used to substract the inner area) 
CGPathMoveToPoint(path, ...); 
CGPathAddArc(path, ...); 
CGPathCloseSubpath(path); 

// Add the path to the context 
CGContextAddPath(context, path); 

// Fill the path using the even-odd fill rule 
CGContextEOFillPath(context); 

CGPathRelease(path); 
+0

非常感謝,將嘗試此並更新你:) – RVN 2011-05-03 09:00:28

4

這是切線,但有點相關。這是我用Swift繪製的甜甜圈(或空心圓圈)代碼。

class func drawDonut(donutColor: UIColor, rect: CGRect, innerDonutProportion: CGFloat) { 

    //// Variable Declarations 
    let innerDonutSide: CGFloat = rect.size.width * innerDonutProportion 
    let innerDonutOffset: CGFloat = 0.5 * (rect.size.width - innerDonutSide) 
    let innerDonutRect = CGRectMake(innerDonutOffset, innerDonutOffset, innerDonutSide, innerDonutSide) 

    //// InnerCircle Drawing 
    let innerCirclePath = UIBezierPath(ovalInRect: innerDonutRect) 

    //// Outer Circle Drawing 
    let outerCirclePath = UIBezierPath(ovalInRect: rect) 

    // Clip out the innerCirclePath 
    outerCirclePath.appendPath(innerCirclePath) 
    outerCirclePath.addClip() 
    outerCirclePath.usesEvenOddFillRule = true; 

    // and Fill the outerCircle 
    donutColor.setFill() 
    outerCirclePath.fill() 
} 
+0

不錯!但對於rect.origin不是(0,0)的一般情況,您應該改用:let innerDonutRect = CGRectMake(rect.origin.x + innerDonutOffset,rect.origin.y + innerDonutOffset,innerDonutSide,innerDonutSide)。除此之外,你的代碼對我來說是完美的。 – rene 2016-04-03 18:35:05