gpt4 book ai didi

ios - CAShapeLayer 和 SpriteKit

转载 作者:行者123 更新时间:2023-11-28 07:57:34 32 4
gpt4 key购买 nike

我正在尝试使用 CAShapeLayer 中的路径动画功能,这在 SpriteKit 中不可用,因此必须将使用 CAShapeLayer 绘制的对象与同一 View 中的 SpriteKit 对象组合起来。

坐标系似乎是相反的:CAShapeLayer 的 y 轴似乎指向下方,而 SKScene 的 y 轴指向上方。

下面是一个简单的 XCODE playground,它尝试绘制一条从 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 坐标系使 Y 轴向上?

最佳答案

不,正如文档中所写,这就是它的工作原理,您可以反转坐标以满足您的要求..

Sprite kit docs :

The unit coordinate system places the origin at the bottom left corner of the frame and (1,1) at the top right corner of the frame. A sprite’s anchor point defaults to (0.5,0.5), which corresponds to the center of the frame.

The rest of iOS :

iOS. The default coordinate system has its origin at the upper left of the drawing area, and positive values extend down and to the right from it. You cannot change the default orientation of a view’s coordinate system in iOS—that is, you cannot “flip” it.

关于ios - CAShapeLayer 和 SpriteKit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47616047/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com