2013-12-18 38 views
3

我正在使用UIBezierPath創建的圓形進度條上工作。進度條看起來像下面的圖片:將圓角應用到用UIBezierPath創建的圓弧

enter image description here

我的問題是:如何使圓角圓弧的邊緣,而不是矩形?

我用來畫圓弧的代碼看起來就像是:

// Draw the arc with bezier path 
     int radius = 100; 

     arc = [CAShapeLayer layer]; 
     arc.path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(100, 50) radius:radius startAngle:M_PI endAngle:M_PI/150 clockwise:YES].CGPath; 
     arc.position = CGPointMake(CGRectGetMidX(self.view.frame)-radius, 
            CGRectGetMidY(self.view.frame)-radius); 
     arc.fillColor = [UIColor clearColor].CGColor; 
     arc.strokeColor = [UIColor purpleColor].CGColor; 
     arc.cornerRadius = 0.5; 
     arc.lineWidth = 6; 

     [self.view.layer addSublayer:arc]; 

     // Animation of the progress bar 
     drawAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; 
     drawAnimation.duration   = 5.0; // "animate over 10 seconds or so.." 
     drawAnimation.repeatCount   = 1.0; // Animate only once.. 
     drawAnimation.removedOnCompletion = YES; // Remain stroked after the animation.. 
     drawAnimation.fromValue = [NSNumber numberWithFloat:0.0f]; 
     drawAnimation.toValue = [NSNumber numberWithFloat:10.0f]; 
     drawAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]; 
     [arc addAnimation:drawAnimation forKey:@"drawCircleAnimation"]; 

我試圖用arc.cornerRadius但它似乎什麼都不返回。

任何幫助,將不勝感激。

預先感謝您!

花崗岩

回答

13

lineCapStylekCGLineCapRound(在貝塞爾路徑)來繪製的線的端部具有圓形邊緣。

您還可以在形狀圖層上設置lineCap來做同樣的事情。

+0

非常感謝,它的工作:) – Granit

+0

@Grangji在你的例子中,它仍然無法正常工作。我做錯了什麼? –

+2

它應該設置在CAShapeLayer上,而不是在UIBezierPath上。 –

7

要讓角落變成圓形,您可以使用它。

arc.lineCap = @"round"; 
+0

使用適當的常數等效! 'kCGLineCapRound' – Warpling

+0

@向我嘗試使用kCGLineCapRound,但它沒有奏效。與「圓」它的工作。任何想法爲什麼kCGLineCapRound不工作? –

+0

@RajTandel我不確定。你確定要設置'lineCap'而不是'lineCapStyle'嗎? – Warpling