2014-07-21 24 views
2

我用下面的代碼來畫弧在iOS中,弧是畸形的某些起始角度

double radius = 358.40001058578491; 
    startAngle = 0.13541347644783652; 
    double center_x= 684; 
    double center_y = 440; 
    std::complex<double> start1( std::polar(radius,startAngle)); 

    CGPoint targetStart1 = CGPointMake(start1.real() + center_x, start1.imag() +center_y); 
    CGMutablePathRef path = CGPathCreateMutable(); 
    CGPathMoveToPoint(path, NULL, targetStart1.x, targetStart1.y); 
    CGPathAddArc(path, NULL, center_x, center_y, radius, startAngle, 0.785, 0); 
    CGContextAddPath(context, path); 
    CGContextSetLineWidth(context, 30); 

    CGContextSetStrokeColorWithColor(context, targetColor.CGColor); 

    CGContextStrokePath(context); 

    CGPathRelease(path); 

如果妳在視網膜檢查它,它看起來像這樣:

enter image description here

我的弧線是綠色弧線。我已經顯示了起始角度是橙色的地方。正如我在紅色矩形中所示,在弧的最開始處繪製了一件額外的東西。這不會發生在所有的起始角度,而只發生在某些起始角度。

你知道爲什麼會發生嗎?

謝謝。

+0

你在設備上檢查過嗎?或模擬器? – Ryan

+0

@ trick14在這兩個看起來像這樣 – Pegah

回答

3

在你原來的問題中,你指定了一個不太正確的文字起點,因此Core Graphics將從該點繪製一條直線到弧的起點。而且由於該起點距離弧的實際起點只有幾個像素,因此會導致您在問題中出現的那種令人好奇的渲染效果。

在修改後的問題,你計算的起點,但我可能會建議編程計算它像這樣:

CGFloat centerX = 684.0; 
CGFloat centerY = 440.0; 
CGFloat radius  = 360.0; 
CGFloat startAngle = 0.135; 
CGFloat endAngle = 0.785; 

CGFloat startingX = centerX + radius * cosf(startAngle); 
CGFloat startingY = centerY + radius * sinf(startAngle); 
CGContextMoveToPoint(context, startingX, startingY); 

CGContextAddArc(context, centerX, centerY, radius, startAngle, endAngle, 0); 

CGContextSetLineWidth(context, 30); 
CGContextSetStrokeColorWithColor(context, targetColor.CGColor); 
CGContextStrokePath(context); 

當我這樣計算的話,有是導致沒有舍入誤差您的原始問題中顯示的神器。


注意,如果你不弧前繪製任何東西,你可以省略CGContextMoveToPoint呼叫乾脆。如果您已經在弧線之前繪製了某些東西,並且不希望將該線路從那個CGContextGetPathCurrentPoint連接到弧線的起點,則只需要「移動到指向」即可。

+1

你的起點是關閉的,因爲你開始在0.0135的角度,當問題的起始角度是0.135 – user3386109

+1

良好的捕獲。但是當我使用.135時,我得到的起點爲1040.72,488.45,這與OP的不同。 – Rob

+1

@Rob請參閱我計算出發點的方式的更新。 – Pegah