Swift:有选择地防止SpriteKit场景的自动屏幕旋转

发布于 2025-02-11 16:57:38 字数 275 浏览 1 评论 0原文

我有一个iOS应用程序,该应用程序将有许多SpriteKit场景。这些场景应保持肖像和景观的屏幕上的相同方向。例如,一个场景将在屏幕上有一条双车道路,即 ie。它将在肖像中向上/向下奔跑,并在景观中向左/右行。 。我不希望它可以自动与设备自动旋转,尽管我会在屏幕上旋转文本以使其可读(我可以这样做)。

还有许多其他视图(主要是文本)仍应自动旋转,因此我不想在“部署信息”中取消选择选项。

有什么想法如何选择/防止设备自动旋转?我希望计算车辆位置之间的方向不变。理想情况下,只需旋转场景即可。

I have an iOS application that will have a number of SpriteKit scenes. These scenes should remain in the same orientation on the screen in both portrait and landscape. For example one scene will have a dual carriageway road running lengthways on the screen ie.it will run up/down in portrait and left/right in landscape. I don't want it to auto rotate with the device, although I will be rotating text on the screen to keep it readable (I can do this).

There are a number of other views (mainly text) that should still auto rotate so I don't want to deselect options in 'Deployment Info'.

Any ideas how to selectively allow/prevent auto rotation with the device? I want calculations for the vehicle positions to be unchanged between orientations. Ideally just rotate the scene.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水染的天色ゝ 2025-02-18 16:57:38

这是一种允许设备在最高级别旋转的方法,同时模拟在Skscene级别没有旋转的方法。您可以在SwiftUI中接收设备旋转事件,然后将该信息传递到您的场景中,

struct GameSceneView: View {
    @State private var scene = TestScene()

    var body: some View {        
        SpriteView(scene: scene)
            .overlay(alignment: .topLeading) {
                Text("SwiftUI Text").font(.system(size:36))
            }
            .onReceive(NotificationCenter.default.publisher(for: UIDevice.orientationDidChangeNotification)) { _ in
                let isLandscape = UIDevice.current.orientation == .landscapeLeft || UIDevice.current.orientation == .landscapeRight
                let rotation = isLandscape ? CGFloat.pi/2 : 0
                scene.set(containerZRotation:rotation)
            }
    }
}

相应的Skscene只需要具有一个包含所有图形元素的顶级Sknode。您在该容器上相应地旋转

class TestScene: SKScene {

    //property and function for handling rotation
    let container = SKNode()
    func set(containerZRotation:CGFloat) {
        container.zRotation = containerZRotation
    }
    
    override func didMove(to view: SKView) {
        self.addChild(container) //add container

        //add all child nodes to *container* rather than self
        let sprite = SKSpriteNode(imageNamed: "road")
        container.addChild(sprite)        
    }
}

here is a way to allow device rotation at top level, while simulating no rotation at SKScene level. you can receive device rotation events in SwiftUI and then pass that information down to your scene

struct GameSceneView: View {
    @State private var scene = TestScene()

    var body: some View {        
        SpriteView(scene: scene)
            .overlay(alignment: .topLeading) {
                Text("SwiftUI Text").font(.system(size:36))
            }
            .onReceive(NotificationCenter.default.publisher(for: UIDevice.orientationDidChangeNotification)) { _ in
                let isLandscape = UIDevice.current.orientation == .landscapeLeft || UIDevice.current.orientation == .landscapeRight
                let rotation = isLandscape ? CGFloat.pi/2 : 0
                scene.set(containerZRotation:rotation)
            }
    }
}

the corresponding SKScene simply needs to have a top level SKNode that contains all graphical elements as children. you set rotation accordingly on that container

class TestScene: SKScene {

    //property and function for handling rotation
    let container = SKNode()
    func set(containerZRotation:CGFloat) {
        container.zRotation = containerZRotation
    }
    
    override func didMove(to view: SKView) {
        self.addChild(container) //add container

        //add all child nodes to *container* rather than self
        let sprite = SKSpriteNode(imageNamed: "road")
        container.addChild(sprite)        
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文