您可以使用的一种方法是为要添加的每个节点创建一个全局变量,例如:
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
}
}