【问题标题】:How to convert points to radians in Objective-C?如何在 Objective-C 中将点转换为弧度?
【发布时间】:2017-06-13 19:02:56
【问题描述】:

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

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

我将如何解决这个问题?

谢谢

【问题讨论】:

    标签: ios objective-c radians


    【解决方案1】:
    CGFloat radians = percent * 0.01 * 2 * M_PI;
    

    简单代数。

    斯威夫特版本

    为了更通用,你可以写一个转换函数:

    func radiansFromPercent(_ percent: CGFloat) -> CGFloat {
        return percent * 0.01 * 2 * CGFloat.pi
    }
    

    【讨论】:

    • 抱歉,你为什么要乘以 0.01?
    • 您的输入百分比范围为 0 到 100。您需要一个从 0.01 到 1.0 的十进制值
    【解决方案2】:

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

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

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

    【讨论】:

    • 你不能像 Swift 中的罐子一样在 Objective-C 中使用特殊字符作为变量名。你需要使用“pi”,你是正确的常量是 M_PI
    【解决方案3】:

    目标-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
    

    【讨论】:

    • OP 的问题指定了 Objective-C,而不是 Swift。
    猜你喜欢
    • 2015-11-28
    • 2015-05-24
    • 1970-01-01
    • 1970-01-01
    • 2020-04-09
    • 1970-01-01
    • 2011-04-10
    • 1970-01-01
    相关资源
    最近更新 更多