iOS - UIBezierPath使起始角始终垂直

时间:2016-03-21 21:23:47

标签: ios swift sprite-kit swift2 uibezierpath

enter image description here

您好我正在尝试制作一个圆圈的随机角度,但ID就像始终是垂直的。在上图中,它显示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)

1 个答案:

答案 0 :(得分:1)

改变这个:

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)

enter image description here