iOS - UIBezierPath 使起始角度始终垂直

iOS - UIBezierPath making start angle always vertical

您好,我目前正在尝试制作一个圆的随机角度,但我希望 startangle 始终是垂直的。在上图中,它显示了 180 度的圆,但起始角度是水平的,它会根据我指定的角度而变化。所以基本上我希望圆的起点总是从 0 度或垂直开始。有人知道解决方案吗?

这是我正在使用的代码。

let center = CGPointMake(size.width/2, size.height/2)

let path = UIBezierPath()
path.addArcWithCenter(center, radius: 200, startAngle: 0, endAngle: 180.degreesToRadians, clockwise: true)
path.addLineToPoint(center)
path.closePath()

let angleShape = SKShapeNode(path: path.CGPath)
angleShape.strokeColor = UIColor(red: 0.203, green: 0.596, blue: 0.858, alpha: 1.0)
angleShape.fillColor = UIColor(red: 0.203, green: 0.596, blue: 0.858, alpha: 1.0)
addChild(angleShape)

改变这个:

path.addArcWithCenter(center, radius: 200, startAngle: 0, endAngle: 180.degreesToRadians, clockwise: true) 

对此:

path.addArcWithCenter(center, radius: 200, startAngle: CGFloat(M_PI_2), endAngle: CGFloat(M_PI + M_PI_2), clockwise: true)

如果你想要随机 startAngleendAngle 试试这个

func randomRange(min min: CGFloat, max: CGFloat) -> CGFloat {
    assert(min < max)
    return CGFloat(Float(arc4random()) / 0xFFFFFFFF) * (max - min) + min
}

let startAngle = randomRange(min: CGFloat(0), max: CGFloat(M_PI*2))
let endAngle = randomRange(min: CGFloat(0), max: CGFloat(M_PI*2))

path.addArcWithCenter(center, radius: 200, startAngle: startAngle, endAngle: endAngle, clockwise: true)