gpt4 book ai didi

swift - 如何删除最后放置的子节点?

转载 作者:搜寻专家 更新时间:2023-11-01 07:08:31 25 4
gpt4 key购买 nike

当用户点击屏幕时,我有 touchesBegan 发现并放置一个节点。

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

if let touchLocation = touches.first?.location(in: sceneView)
{
let hitTestResults = sceneView.hitTest(touchLocation, types: .featurePoint)

if let hitResult = hitTestResults.first
{
addDot(at: hitResult)
}
}
}

func addDot(at hitResult : ARHitTestResult)
{

let imagePlane = SCNPlane(width: 0.1, height: 0.1)
imagePlane.firstMaterial?.diffuse.contents = UIImage(named: "helmet_R.png")
let planeNode = SCNNode(geometry: imagePlane)

planeNode.position = SCNVector3(hitResult.worldTransform.columns.3.x, hitResult.worldTransform.columns.3.y, hitResult.worldTransform.columns.3.z)

planeNode.constraints = [SCNBillboardConstraint()]


sceneView.scene.rootNode.addChildNode(planeNode)

但是,我对删除 View 中最后放置的节点感到困惑。

我尝试了一个简单的

planeNode.removeFromParentNode()

但这并没有达到我希望的结果。因此,我将如何达到预期的结果?我知道删除父节点是任意的,但我不确定如何调用最后放置的子节点。

最佳答案

您应该创建对类中最后添加的节点的引用。

var lastNode: SCNNode?

然后,假设您希望在每次添加新节点时删除最后一个节点。在这种情况下,您将创建并添加一个新节点,删除 lastNode 并将新创建的节点分配为最后添加的节点。因此,在您的情况下,在方法 addDot 中:

 sceneView.scene.rootNode.addChildNode(planeNode)
lastNode?.removeFromParentNode()
lastNode = planeNode

关于swift - 如何删除最后放置的子节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46530885/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com