2013-12-18 35 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