【问题标题】:What unit does Swift use for angles?Swift 使用什么单位来表示角度?
【发布时间】:2016-10-27 17:04:18
【问题描述】:

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

在我阅读的first post 中,Swift 似乎使用弧度:

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

second post 中,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 uiview drawrect angle units-of-measurement


    【解决方案1】:

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

    来自CGContextAddArc docs

    startAngle:到圆弧起点的角度,以 x 轴正方向的弧度为单位。

    endAngle:到圆弧终点的角度,以 x 轴正方向的弧度为单位。

    还有CAShapeLayerstrokeStartstrokeEnd 属性表示应该绘制给定路径的哪个部分,它不是角度。下次请阅读the docs

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-07
      • 2011-05-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多