objective-c 用 UIBezierPath 画圆
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25992714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Draw circle with UIBezierPath
提问by Mosbah
I'm trying to draw a circle using UIBezierPath addArcWithCenter method :
我正在尝试使用 UIBezierPath addArcWithCenter 方法绘制一个圆圈:
UIBezierPath *bezierPath =
[UIBezierPath bezierPathWithOvalInRect:CGRectMake(0., 0., 100., 100.)];
[bezierPath addArcWithCenter:center
radius:0.
startAngle:0 endAngle:2 * M_PI clockwise:YES];
CAShapeLayer *progressLayer = [[CAShapeLayer alloc] init];
[progressLayer setPath:bezierPath.CGPath];
[progressLayer setStrokeColor:[UIColor colorWithWhite:1. alpha:.2].CGColor];
[progressLayer setFillColor:[UIColor clearColor].CGColor];
[progressLayer setLineWidth:.3 * self.bounds.size.width];
[progressLayer setStrokeStart:_volumeValue/100.];
[progressLayer setStrokeEnd:volume/100.]; // between 0 and 100
[_circleView.layer addSublayer:progressLayer];
but what I get is the following :
但我得到的是以下内容:


I tried to play with the different parameters but no luck
我尝试使用不同的参数但没有运气
Thank you
谢谢
UPDATE :
更新 :
I'm sorry if I didn't explain what I'm trying to do:
如果我没有解释我要做什么,我很抱歉:
*The background circle is drawed using :
*背景圆圈是使用以下方法绘制的:
[UIBezierPath bezierPathWithOvalInRect:CGRectMake(0., 0., 100., 100.)]
*I'm trying to draw the red circle step by step using bezierPathWithOvalInRect between 2 values : _volumeValue and volume
*我正在尝试使用 2 个值之间的 bezierPathWithOvalInRect 逐步绘制红色圆圈:_volumeValue 和 volume
But I can't get a perfect circle, instead I get the horizontale part after certain value.
但是我不能得到一个完美的圆,而是在某个值后得到水平部分。


回答by Fogmeister
OK, try this...
好的,试试这个...
UIBezierPath *bezierPath = [UIBezierPath bezierPath];
[bezierPath addArcWithCenter:center radius:50 startAngle:0 endAngle:2 * M_PI clockwise:YES];
CAShapeLayer *progressLayer = [[CAShapeLayer alloc] init];
[progressLayer setPath:bezierPath.CGPath];
[progressLayer setStrokeColor:[UIColor colorWithWhite:1.0 alpha:0.2].CGColor];
[progressLayer setFillColor:[UIColor clearColor].CGColor];
[progressLayer setLineWidth:0.3 * self.bounds.size.width];
[progressLayer setStrokeEnd:volume/100];
[_circleView.layer addSublayer:progressLayer];
volumeshould be between 0 and 100.
volume应该在 0 到 100 之间。
Also, you were creating a path with an ellipse and then adding an arc to it. Don't do that. Just add the arc to the empty path.
此外,您正在创建一条带有椭圆的路径,然后向其添加一条弧线。不要那样做。只需将圆弧添加到空路径即可。
If you want to change where the arc starts from then change the startAngleand endAnglewhen adding the arc. Don't change the stroke start value.
如果要更改圆弧的起点startAngle,请endAngle在添加圆弧时更改和。不要更改笔划起始值。
Animating change
动画变化
[CATransaction begin];
CABasicAnimation *animateStrokeDown = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
animateStrokeDown.toValue = [NSNumber numberWithFloat:volume/100.];
[progressLayer addAnimation:animateStrokeDown forKey:@"animateStrokeDown"];
[CATransaction commit];
OK, what this will do is animate the strokeEndproperty of the path. You have to realise though, it works like this...
好的,这将做的是动画strokeEnd路径的属性。你必须意识到,它是这样工作的......
Begin state: start = 0, end = 6
0123456
-------
// increase volume
Animate to: end = 9
0123456789
----------
// decrease volume
Animate to: end = 1
01
--
The start has not moved. The end has moved. You are not "filling in" the rest of the line. You are changing the line.
起点未动。终点已经移动。您没有“填写”该行的其余部分。你正在改变线路。
This is why the start should always be 0 and you just change the stroke end.
这就是为什么开始应始终为 0 而您只需更改行程结束。
回答by Daxesh Nagar
startAngle:degreesToRadians(-90) endAngle:0 clockwise:YES
(The reason is that the default coordinate system on iOS has a x-axis pointing to the right, and a y-axis pointing down.)
(原因是 iOS 上的默认坐标系 x 轴指向右侧,y 轴指向下方。)
read this document https://developer.apple.com/library/ios/documentation/uikit/reference/UIBezierPath_class/index.html
阅读本文档 https://developer.apple.com/library/ios/documentation/uikit/reference/UIBezierPath_class/index.html
specifying a start angle of 0 radians, an end angle of π radians, and setting the clockwise parameter to YES draws the bottom half of the circle. However, specifying the same start and end angles but setting the clockwise parameter set to NO draws the top half of the circle.
指定起始角为 0 弧度,结束角为 π 弧度,并将顺时针参数设置为 YES 绘制圆的下半部分。但是,指定相同的开始和结束角度但将顺时针参数设置为 NO 会绘制圆的上半部分。
回答by Nikolai Ruhe
I have no idea what you expect and what's wrong with your actual result—but a radius of 0 in your second line of code seems fishy. It just adds a point at centerto the current path (which already includes a circle).
我不知道你期望什么,你的实际结果有什么问题——但是你的第二行代码中的半径为 0 似乎很可疑。它只是center在当前路径(已经包含一个圆)上添加了一个点。

