代码之家  ›  专栏  ›  技术社区  ›  PvDev

再次触摸时,如何去除屏幕?

  •  0
  • PvDev  · 技术社区  · 6 年前

    我可以添加新的屏幕,当用户点击飞机。但现在,在同一个屏幕上点击它会再次添加新的屏幕而不是删除。

    下面是我尝试的代码:

      override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    
        //1. Get The Current Touch Location & Perform An ARSCNHitTest To Check For Any Hit SCNNode's
        guard let currentTouchLocation = touches.first?.location(in: self.sceneView),
            let hitTestNode = self.sceneView.hitTest(currentTouchLocation, options: nil).first?.node else { return }
    
    
        if let lableName = hitTestNode.name {
            print("touch working")
            if lableName == "lableNode"{
    
    
                makeCakeOnNode(hitTestNode)
    
    
    
            } else if lableName == "AllLabelNode" {
    
                makeCakeOnNode1(hitTestNode)
    
            } else if lableName == "fruitNode" {
    
                makeCakeOnNode2(hitTestNode)
    
            }
    
        }
    }
    
    
    func makeCakeOnNode(_ node: SCNNode){
    
        let planeGeometry = SCNPlane(width: 0.18  , height: 0.15)
    
        planeGeometry.firstMaterial?.diffuse.contents = UIColor.black.withAlphaComponent(0)
    
    
    
    
        planeNode0 = SCNNode(geometry: planeGeometry)
        planeNode0?.runAction(SCNAction.moveBy(x: -0.2, y: -0.15, z: 0, duration: 0))
    
                        let overlayNode = self.getNode(withImageName: "menu")
                        print("overlay::\(overlayNode)")
    
        let newPlane = SCNPlane(width: 0.15, height: 0.10)
        newPlane.firstMaterial?.diffuse.contents = UIImage(named: "cake_detail")
    
        let newPlaneNode = SCNNode(geometry: newPlane)
        newPlaneNode.eulerAngles.x = -.pi / 2
        newPlaneNode.runAction(SCNAction.moveBy(x: -0.2, y: -0.15, z: 0, duration: 0))
    
        node.addChildNode(planeNode0!)
        planeNode0?.addChildNode(overlayNode)
        planeNode0?.addChildNode(newPlaneNode)
        if planeBool == true {
    
            planeNode1?.isHidden = true
            planeNode2?.isHidden = true
            planeNode0?.isHidden = false
            planeBool = false
    
        } else {
    
            print("plane removed")
            planeNode0?.isHidden = true
            planeNode1?.isHidden = true
            planeNode2?.isHidden = true
    
            planeBool = true
        }
    
    
    }
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   BlackMirrorz    6 年前

    您可以使用的一种方法是为要添加的每个节点创建一个全局变量,例如:

     var labelNode: SCNNode?
     var allLabelNode: SCNNode?
     var fruitNode: SCNNode?
    

    然后你可以进行一个测试,看看它们是否正确 nil . 如果节点是 ,然后创建,否则删除,例如:

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    
        //1. Get The Current Touch Location & Perform An ARSCNHitTest To Check For Any Hit SCNNode's
        guard let currentTouchLocation = touches.first?.location(in: self. self.sceneView),
            let hitTestNode = self.sceneView.hitTest(currentTouchLocation, options: nil).first?.node
            else { return }
    
        //2. Enumerate The Nodes We Have By Name
        switch hitTestNode.name {
        case "lableNode":
    
            //1. If The LabelNode Doesnt Exist Create It
            if labelNode == nil{
                //Create The Node Here
            }else{
               labelNode?.removeFromParentNode()
               labelNode = nil
            }
    
        case "AllLabelNode":
    
            //2. If The LabelNode Doesnt Exist Create It
            if allLabelNode == nil{
                //Create The Node Here
            }else{
                allLabelNode?.removeFromParentNode()
                allLabelNode = nil
            }
    
        case "fruitNode":
    
            //3. If The LabelNode Doesnt Exist Create It
            if fruitNode == nil{
                //Create The Node Here
            }else{
                fruitNode?.removeFromParentNode()
                fruitNode = nil
            }
    
        default:
            return
        }
    
    }