已关注 UIBezierPath引用,我尝试绘制一条虚线路径,该路径最终应为虚线弧。但是绘图方向错误。 顺时针
设置为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 有不同的坐标系。
最佳答案
UIBezierPath 是根据 UIKit 编写的,因此它使用 UIKit 坐标系,(0,0) 位于左上角,y 值向下延伸。对于 SKNode,它有不同的坐标系,(0,0) 位于中心,y 值向上。绘制圆弧时应记住这一点,因为它会影响顺时针参数。可以找到SKNode坐标系的讨论here .
您也可以将此代码粘贴到 Playground 中以查看差异
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
关于ios - UIBezierPath 中的圆在 SKShapeNode 中以错误方向绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36666970/