1
我想利用CAShapeLayer中的路徑動畫功能,該功能在SpriteKit中不可用,因此必須組合在同一視圖中將CAShapeLayer與SpriteKit對象進行繪製的對象。CAShapeLayer和SpriteKit
座標系似乎是相反的:CAShapeLayer似乎有+ ve y軸向下指向,而SKScene則指向上。
下面是一個簡單的XCODE操場,試圖從0,0到200,100繪製一條黃線,並用較粗的紅線將其遮蔽。
import SpriteKit
import PlaygroundSupport
let bounds = CGRect(x: 0, y: 0, width: 400, height: 200)
let view = SKView(frame: bounds)
view.backgroundColor = UIColor.lightGray
PlaygroundPage.current.liveView = view
// Create SK Scene
let scene = SKScene(size: CGSize(width: 400, height: 200))
scene.scaleMode = SKSceneScaleMode.aspectFit
view.presentScene(scene);
// Define the path
let path: CGMutablePath = CGMutablePath();
path.move(to: CGPoint(x:0, y:0))
path.addLine(to: CGPoint(x:200, y:100))
// Use CAShapeLayer to draw the red line
var pathLayer: CAShapeLayer = CAShapeLayer()
pathLayer.path = path
pathLayer.strokeColor = UIColor.red.cgColor
pathLayer.fillColor = nil
pathLayer.lineWidth = 4.0
pathLayer.lineJoin = kCALineJoinBevel
pathLayer.zPosition = 1;
view.layer.addSublayer(pathLayer);
//Use SKShapeNode to draw the yellow line
let pathShape: SKShapeNode = SKShapeNode(path: path);
pathShape.strokeColor = .yellow;
pathShape.lineWidth = 1.0;
pathShape.zPosition = 10;
scene.addChild(pathShape);
我預計黃線與紅線重合。而黃色和紅色線條則以鏡像形式出現。
無論如何重新定義CAShapeLayer座標系指向+ ve Y軸向上?
我能夠通過反轉CAShapeLayer與仿射變換使行重合:'var mirror = CGAffineTransform(scaleX:1.0,y:-1.0); var inverse = path.copy(using:&mirror)!;' –