Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 当状态发生变化时,如何防止SceneKit场景重新渲染效果不佳?_Ios_Swift_Swiftui_Scenekit - Fatal编程技术网

Ios 当状态发生变化时,如何防止SceneKit场景重新渲染效果不佳?

Ios 当状态发生变化时,如何防止SceneKit场景重新渲染效果不佳?,ios,swift,swiftui,scenekit,Ios,Swift,Swiftui,Scenekit,“我的父视图”有一个子视图,其中包含SceneKit场景,但当父视图中发生任何状态更改时,它会重置SceneKit动画,更改模型的纹理,并使模型变大 有没有办法防止SceneKit场景以这种方式受到状态更改的影响 以下是父视图的代码: struct ContentView: View { @State private var color: Color = .sBlue var body: some View { VStack { But

“我的父视图”有一个子视图,其中包含SceneKit场景,但当父视图中发生任何状态更改时,它会重置SceneKit动画,更改模型的纹理,并使模型变大

有没有办法防止SceneKit场景以这种方式受到状态更改的影响

以下是父视图的代码:

struct ContentView: View {
    @State private var color: Color = .sBlue

    var body: some View {
        VStack {
            Button(action: { self.color = .sOrange }) {
               self.color
            }
            .frame(height: 240)

            ModelView()
        }
    }
}
下面是SceneKit视图的代码:

struct ModelView: UIViewRepresentable {
    let model = SCNScene(named: "art.scnassets/3D Models/yBotIdle.scn")!

    func makeUIView(context: Context) -> SCNView {
        model.rootNode.childNode(withName: "yBot", recursively: true)?
            .scale = SCNVector3(x: 0.03, y: 0.03, z: 0.03)

        let cameraNode = SCNNode()
        let camera = SCNCamera()
        camera.focalLength = 120
        cameraNode.camera = camera
        cameraNode.position = SCNVector3(x: 0, y: 2.8, z: 35)
        model.rootNode.addChildNode(cameraNode)

        let directionalLightNode = SCNNode()
        directionalLightNode.light = SCNLight()
        directionalLightNode.light?.type = SCNLight.LightType.directional
        directionalLightNode.light?.intensity = 1500
        directionalLightNode.position = SCNVector3(x: 0, y: 6, z: 10)
        directionalLightNode.eulerAngles = SCNVector3(x: -0.4, y: 0, z: 0)
        model.rootNode.addChildNode(directionalLightNode)

        let modelView = SCNView()
        modelView.antialiasingMode = SCNAntialiasingMode.multisampling4X
        modelView.backgroundColor = UIColor(ciColor: .clear)

        return modelView
    }

    func updateUIView(_ modelView: SCNView, context: Context) {
        modelView.scene = model
    }
}

谢谢

找到了!创建SceneKit视图的实例以在父视图中使用,而不是直接使用SceneKit视图,可以解决这些问题。我不知道为什么会这样,如果有人能解释的话,我很想听

struct ContentView: View {
    @State private var color: Color = .sBlue
    let modelView = ModelView()

    var body: some View {
        VStack {
            Button(action: { self.color = .sOrange }) {
                self.color
            }
            .frame(height: 240)

            modelView
        }
    }
}

我们需要看看你写了什么来帮助你。请点击编辑按钮并添加格式化代码。如果你不这样做,你可能会被否决。@Mozahler谢谢你的提示,我是个笨蛋。我现在已经包括了代码。太棒了!我已经向上投票来帮助你获得一些知名度。