-1

在我的应用程序中,我使用 UIBezierPath 将弧线绘制成圆形。我正在尝试将数字与弧度相关联。假设用户有一定数量的积分,积分上限为 100 分。我希望 100 点是 360 度。我希望圆圈的前 33% 为绿色,然后从 34% 到下一个 66% 的圆圈用橙色描边,然后从 67% 到 100% 为红色。

我在这里遇到的问题是将圆的百分比转换为弧度。在创建 UIBezier 路径时,我需要提供 startAngle 和 endAngle,并且在将这些点转换为弧度值时遇到了一些麻烦。

我将如何解决这个问题?

谢谢

4

3 回答 3

1
CGFloat radians = percent * 0.01 * 2 * M_PI;

简单的代数。

斯威夫特版本

使其更通用,您可以编写一个转换函数:

func radiansFromPercent(_ percent: CGFloat) -> CGFloat {
    return percent * 0.01 * 2 * CGFloat.pi
}
于 2017-01-28T00:43:19.550 回答
0

Objective-C

CGFloat fullCircle = 2 * M_PI ;             // M_PI Pi number which is half of the circle in radian
CGFloat startPoint = 0.0      ;
CGFloat endPoint   = fullCircle * 0.33 ;

// Assuming circling clockwise

// .... Draw first step UIBezierPath

startPoint = endPoint        ;
endPoint = startPoint + fullCircle * 0.33 ;
// .... Draw second step UIBezierPath

startPoint = endPoint        ;
endPoint = fullCircle - startPoint ;       // This to make sure the whole circle will be covered
// .... Draw the last step UIBezierPath

迅速

let fullCircle = 2 * M_PI             // M_PI Pi number which is half of the circle in radian
var startPoint: Float = 0.0
var endPoint: Float = fullCircle * 0.33

// Assuming circling clockwise
// .... Draw first step UIBezierPath

startPoint = endPoint
endPoint = startPoint + fullCircle * 0.33
// .... Draw second step UIBezierPath

startPoint = endPoint
endPoint = fullCircle - startPoint       // This to make sure the whole circle will be covered
// .... Draw the last step UIBezierPath
于 2017-01-28T00:48:31.307 回答
0

我想你想要的是单位圆。还记得使用单位圆时的三角函数吗?同样的事情也适用于此。如果您需要获取 π - 在 Swift 中只需说let π = CGFloat.pi(按住 alt+p 表示特殊字符)。在 Objective-C 中 - 我认为它是CGFloat π = M_PI;.

在此处输入图像描述

您可以从零到2π/3前 1/3,然后从2π/34π/3,然后从4π/3(整圈)。

我不应该说我没有制作这个图形 - 它来自 RayWenderlich.com 上的教程 - 但它非常适合 iOS 坐标系。

于 2017-01-28T00:45:33.427 回答