我试图利用CAShapeLayer中无法在SpriteKit中使用的path动画功能,因此必须将绘制的CAShapeLayer对象与同一视图中的SpriteKit对象组合起来。
坐标系似乎是逆的: CAShapeLayer似乎有+ve轴指向向下,而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轴向上?
https://stackoverflow.com/questions/47616047
复制相似问题