How to remove scnplane when touching again?

时光毁灭记忆、已成空白 提交于 2020-01-03 06:28:06

问题


I can able to add new scnplane when user tap on plane. But now, tap on same scnplane it adds again new scnplane instead of removing.

Initially when reference image is detected , It will list cake in side of menu card with image, name and ratings. And user tap on rating, it will list user ratings.

here is the code which i tried:

  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:


One approach you can use, is to create a global variable for each of the nodes you are adding e.g:

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

Then you can perform a test to see if they are nil. If the node is nil, then create it, else remove it e.g:

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
    }

}

Please note that the Touches method should probably be refactored, but it should be more than enough to point you in the right direction...



来源:https://stackoverflow.com/questions/52584462/how-to-remove-scnplane-when-touching-again

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!