来自UIBezierPath的圆圈在SKShapeNode中以错误的方向绘制

时间:2016-04-16 16:52:39

标签: ios swift sprite-kit uibezierpath skshapenode

UIBezierPath引用之后,我尝试绘制一条虚线路径,该路径最终应为虚线弧。但是绘图方向是错误的。 clockwise设置为true,但绘制了圆圈的上半部分,而不是苹果网页中提到的内容

let arcForCompleted =
        UIBezierPath(arcCenter: origin, radius: radius, startAngle: 0, endAngle: CGFloat(M_PI), clockwise: true)
let pattern = getPattern(self.circumference, segments: involved)
let dashedPathForCompleted = CGPathCreateCopyByDashingPath(arcForCompleted.CGPath, nil, 0, pattern, pattern.count)
let dashedCircleForCompleted = SKShapeNode(path: dashedPathForCompleted!)

我猜这是因为UIKit和SpriteKit有不同的坐标系。

1 个答案:

答案 0 :(得分:2)

UIBezierPath是用UIKit编写的,因此它使用UIKit坐标系,(0,0)位于左上角,正y值向下运行。对于SKNode,它具有不同的坐标系,(0,0)位于中心,正y运行。在绘制弧时应该记住这一点,因为它会影响顺时针参数。您可以找到有关SKNode坐标系here的讨论。

您可以将此代码粘贴到游乐场中以查看差异

let bezierPath = UIBezierPath(arcCenter: CGPoint(x: 50.0,y: 50.0), radius: 50, startAngle: 0, endAngle: CGFloat(M_PI), clockwise: true)
class ArcView:UIView
{
    override func drawRect(rect: CGRect) {
        let arcForCompleted = bezierPath
        let pattern:[CGFloat] = [10.0,10.0]
        arcForCompleted.setLineDash(pattern, count: 2, phase: 0.0)
        arcForCompleted.stroke()
    }
}
let arcView = ArcView(frame: CGRect(x: 0.0, y: 0.0, width: 100.0, height: 100.0))
arcView.backgroundColor = UIColor.whiteColor()

let arcForCompleted = bezierPath
let shape = SKShapeNode()
shape.path = arcForCompleted.CGPath