2016-04-23 39 views
1

在SpriteKit中,clockwise方向與UIBezierPath相反,但不是CGPath。舉例來說,如果我有爲什麼CGPath和UIBezierPath在SpriteKit中定義「順時針」不同?

do { 
    let path = CGPathCreateMutable() 
    CGPathAddArc(path, nil, 0, 0, 10, 0, CGFloat(M_PI_2), true) 
    let node = SKShapeNode(path: path) 
    node.position = CGPoint(x: self.size.width/2, y: self.size.height/2) 
    self.addChild(node) 
} 
do { 
    let path = UIBezierPath() 
    path.addArcWithCenter(CGPoint(x: 0, y: 0), radius: 10, startAngle: 0, endAngle: CGFloat(M_PI_2), clockwise: true) 
    let node = SKShapeNode(path: path.CGPath) 
    node.position = CGPoint(x: self.size.width/2, y: self.size.height/2 - 100) 
    self.addChild(node) 
} 
GameScene.didMoveToView(view: SKView)

,第一點繪製3/4弧右上角缺失,但第二點繪製在右上四分之一圓弧。 UIBezierPath中的逆向「順時針」方向解釋爲in this post,但爲什麼CGPath表現不一樣?是不是UIBezierPath只是一個包裝CGPath

注意:Objective-C和Swift都會發生這種情況(所以不是語言特定的)。未嘗試使用NSBezierPath的Mac App。

回答

6

CGPathUIBezierPath使用不同的座標系。 UIBezierPath的原點位於繪圖區域的左上角,CGPath的原點位於左下角。因此,90度是圓圈上最低點UIBezierPath和最高點CGPath。由於0°位於兩個路徑的圓上相同的點(最右邊的點),所以從0º到90º的順時針圓弧的繪製方式不同。

隨着原點位於左上方,90º(π/ 2)位於圓的最低點,因此,如下圖所示繪製了一條順時針方向的圓弧UIBezierPath。由於SpriteKit的原點位於左下方,所以在用於創建SKShapeNode時,此弧線會翻轉(垂直)。

enter image description here

隨着在左下原點,90度是在圓的頂部,並沿順時針CGPath弧被繪製爲如下所示。由於SpriteKit的原點也位於左下角,所以形狀節點弧線以相同的方向出現。

enter image description here

+0

謝謝。我剛剛找到了關於['UIBezierView']的參考資料(https://developer.apple.com/library/ios/documentation/2DDrawing/Conceptual/DrawingPrintingiOS/BezierPaths/BezierPaths.html#//apple_ref/doc/uid/) TP40010156-CH11-SW5)和['CGPath'](https://developer.apple.com/library/mac/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_overview/dq_overview.html#//apple_ref/doc/uid/TP30001066 -CH202-CJBBAEEC)。 –

相關問題