【问题标题】:Method `enumerateChildNodes` is not finding nodes方法`enumerateChildNodes`没有找到节点
【发布时间】:2021-09-29 21:25:04
【问题描述】:

我正在开发一个有 2 个按钮的 ARKit 应用:“播放”和“重置”。当我单击播放时,会显示一个金字塔节点。当我点击重置时,所有金字塔节点都应该从场景中删除:

另一个要求是我想在点击播放按钮时禁用它。

接下来是我的 contentView 代码:

import ARKit

struct ContentView: View {
    
    @State var node = SCNNode()
    @State var play: Bool = false
    @State var reset: Bool = false
    @State var isDisabled: Bool = false
    @State var showBanner:Bool = true
    
    var body: some View {
        ZStack {
            SceneKitView(node: $node, play: $play, reset: $reset, isDisabled: $isDisabled)
            VStack {
                Spacer()
                HStack {
                    Button(action: {
                        play = true
                    }) {
                        Image("Play")
                    }.padding()
                    .disabled(isDisabled) /// <<<<< THIS DISABLES THE BUTTON

                    Spacer()
                    
                    Button(action: {
                        play = false                        
                        reset = true
                    }) {
                        Image("Reset")
                    }.padding()
                }
            }
        }
    }
    
    
}

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

为了禁用播放按钮,我使用了.disabled 和布尔值isDisabled

我的代码在Coordinator 中将isDisabled 的值更改为TRUE

import ARKit
import SwiftUI

struct SceneKitView: UIViewRepresentable {
    let arView = ARSCNView(frame: .zero)
    let config = ARWorldTrackingConfiguration()
    @Binding var node: SCNNode
    @Binding var play: Bool
    @Binding var reset: Bool
    @Binding var isDisabled: Bool
    
    
    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }
    
    final class Coordinator: NSObject, ARSCNViewDelegate {
        var control: SceneKitView
        init(_ control: SceneKitView) {
            self.control = control
        }
        func renderer(_ renderer: SCNSceneRenderer, willRenderScene scene: SCNScene, atTime time: TimeInterval) {
            DispatchQueue.main.async {
                if self.control.play {
                    self.control.addNode()
                    self.control.play = false
                    self.control.isDisabled = true // HERE I UPDATE THE VALUE !!!!!!!!
                    self.control.reset = false
                }else{
                }
            }
        }
    }
    
    func removeNode(){
        self.arView.scene.rootNode.enumerateChildNodes { (node, _) in
            print("existing nodes = \(node)")
            node.removeFromParentNode()
        }
    }
    
    func updateUIView(_ uiView: ARSCNView,
     context: Context) {
        if self.reset {
            self.removeNode()
            print("game reseted")
        }
    }
    
    func makeUIView(context: Context) -> ARSCNView {
        arView.scene = SCNScene()
        arView.delegate = context.coordinator
        arView.autoenablesDefaultLighting = true
        //arView.debugOptions = [ARSCNDebugOptions.showFeaturePoints, ARSCNDebugOptions.showWorldOrigin]
        arView.showsStatistics = true
        arView.session.run(self.config)
        return arView
    }
    
    func addNode(){
        let pyramid = SCNNode(geometry: SCNPyramid(width: 0.1, height: 0.1, length: 0.1))
        pyramid.geometry?.firstMaterial?.diffuse.contents = UIColor.red
        pyramid.geometry?.firstMaterial?.specular.contents = UIColor.white
        pyramid.position = SCNVector3(0,0,-0.5)
        self.arView.scene.rootNode.addChildNode(pyramid)
    }
    
}

问题:当应用程序运行时,我点击RESET,方法removeNode被调用,这个方法使用enumerateChildNodes查找节点并使用removeFromParentNode删除它们但是金字塔节点没有被移除! :(

疯狂的事情(对我来说)是,如果我不更改CoordinatorisDisabled 的值,即我注释该行,removeNode 工作,并且节点被删除! !!

欢迎任何意见,建议:)

【问题讨论】:

    标签: swift swiftui augmented-reality scenekit arkit


    【解决方案1】:

    这解决了问题:

    func updateUIView(_ uiView: ARSCNView, context: Context) {
        if self.reset {
            DispatchQueue.main.async {
                context.coordinator.control.removeNode()
                context.coordinator.control.isDisabled = false
            }
            print("Game Reset")
        }
    }
    

    【讨论】:

    • 在我看来根本原因是上下文/范围问题。如果我理解正确,在您的修复中,您正在从context.coordinator.control 调用removeNode。我说的对吗?
    猜你喜欢
    • 2018-07-15
    • 1970-01-01
    • 1970-01-01
    • 2014-07-21
    • 2019-01-20
    • 1970-01-01
    • 2017-06-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多