swiftui - 具有 SpriteView 和 SwiftUI 的场景不会填满整个屏幕

标签 swiftui sprite-kit scenekit

我正在尝试处理项目 1 并使用 SpriteKit 和 SwiftUI,但由于某种原因,场景从未填满我的 iPad 上的整个屏幕。

import SwiftUI
import SpriteKit

struct ContentView: View {
    var scene: SKScene {
        let screenWidth  = UIScreen.main.bounds.width
        let screenHeight = UIScreen.main.bounds.height
        
        let scene = GameScene()
        scene.size = CGSize(width: screenHeight, height: screenWidth)
        scene.scaleMode = .fill
        scene.backgroundColor = .green
        return scene
    }
    
    var body: some View {
        let screenWidth  = UIScreen.main.bounds.width
        let screenHeight = UIScreen.main.bounds.height
        
        print(screenWidth)
        
        return SpriteView(scene: scene)
            .frame(width: screenWidth, height: screenHeight)
            .ignoresSafeArea()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

现在看起来像这样:

我在这里缺少什么想法吗?

最大

最佳答案

最后通过将背景放入ContentView的ZStack()中修复了这个问题

var body: some View {
        let screenWidth  = UIScreen.main.bounds.width
        let screenHeight = UIScreen.main.bounds.height
        
        ZStack{
            EmptyView()
            Image("road")
                .resizable()
                .aspectRatio(contentMode: .fill)
                .ignoresSafeArea()
            
            SpriteView(scene: scene, options: [.allowsTransparency])
                .frame(width: screenWidth, height: screenHeight)
                .ignoresSafeArea()
        }
    }
}

出于某种原因,当我将纹理添加为 GameScene 类的一部分时,无论我尝试什么比例,它都不会将纹理放大到全屏尺寸:

let background = SKSpriteNode(imageNamed: "road")
        
        background.zPosition = -1
//      background.scale(to: CGSize(width: 1180, height: 700))
//      background.scale(to: CGSize(width: CGFloat(1180), height: CGFloat(820)))
        addChild(background)

关于swiftui - 具有 SpriteView 和 SwiftUI 的场景不会填满整个屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67594431/

相关文章:

ios - SKVideoNode 仅在节点或相机移动时在 SCNScene 中渲染

scenekit - AVDepthData(来自 TrueDepth 相机)中的深度值是否表示距相机或相机平面的距离?

SwiftUI:在 map View 上更改 map 类型?

swift - SpriteNode 动画失败

swift3 - 在 Swift Spritekit 中,每当我从一个场景转换到另一个场景时,我的 sks 文件没有传输?

ios - Scene Kit 联系 catagoryBitMasks 总是返回 1

ios - 如何使 TextField 右对齐(尾随)

ios - 如何删除 SwiftUI 中列表顶部的空白

ios - SwiftUI:将 LazyVStack 或 LazyHStack 放入 ScrollView 会导致卡顿(Apple 错误??)

ios - SpriteKit 中设备的基本定位