Swift 的角度单位是什么?

What unit does Swift use for angles?

我正在编写一些代码来绘制圆形图表,并且在阅读一些示例代码时,对 Swift 中的角度测量方式感到困惑。

我看了,Swift好像用弧度:

CGContextAddArc(context, origo.x, origo.y, radius, floatPi * 3 / 2, floatPi * 3 / 2 + floatPi * 2 * percent, 0)

中,Swift似乎将角度定义为0到1的范围:

let circlePath = UIBezierPath(ovalInRect: CGRect(x: 200, y: 200, width: 150, height: 150))
var segments: [CAShapeLayer] = []
let segmentAngle: CGFloat = (360 * 0.125) / 360

for var i = 0; i < 8; i++ {
    let circleLayer = CAShapeLayer()
    circleLayer.path = circlePath.CGPath

    // start angle is number of segments * the segment angle
    circleLayer.strokeStart = segmentAngle * CGFloat(i)

    // end angle is the start plus one segment, minus a little to make a gap
    // you'll have to play with this value to get it to look right at the size you need
    let gapSize: CGFloat = 0.008
    circleLayer.strokeEnd = circleLayer.strokeStart + segmentAngle - gapSize

    circleLayer.lineWidth = 10
    circleLayer.strokeColor = UIColor(red:0,  green:0.004,  blue:0.549, alpha:1).CGColor
    circleLayer.fillColor = UIColor.clearColor().CGColor

    // add the segment to the segments array and to the view
    segments.insert(circleLayer, atIndex: i)
    view.layer.addSublayer(segments[i])
}

那么,Swift如何测量角度?

谢谢

Swift 与角度单位完全无关,它只是一个 Float/Double 而已。 Core Graphics 框架的函数指定了他们期望的角度单位。只需阅读相关功能的文档即可。

来自CGContextAddArc docs

startAngle: The angle to the starting point of the arc, measured in radians from the positive x-axis.

endAngle: The angle to the end point of the arc, measured in radians from the positive x-axis.

此外 CAShapeLayerstrokeStartstrokeEnd 属性说明应该绘制给定路径的哪一部分,而不是角度。下次再读the docs