【问题标题】:How to remove SCNNode when Tap on it?点击它时如何删除SCNNode?
【发布时间】:2018-06-07 05:31:35
【问题描述】:

我有使用 SceneView 创建多维数据集,我想消失点击动作的多维数据集。怎么实现呢?

这是我创建 Cube 的代码

     SCNBox *Box = [SCNBox boxWithWidth:2.0 height:2.0 length:2.0 
     chamferRadius:Radius];


     Box.firstMaterial.diffuse.contents = [UIColor whiteColor];
     SCNNode *cubeNode = [SCNNode nodeWithGeometry:Box];
     [ArrBoxNode addObject:cubeNode];

     self.sceneView.backgroundColor = [UIColor redColor];
     self.view.backgroundColor  = [UIColor grayColor];

     cubeNode.position = SCNVector3Make(4,0,0);

     [scene.rootNode addChildNode:cubeNode];
     self.sceneView.scene = scene;
     [self.sceneView sizeToFit];




 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  {
      UITouch *touch = [touches anyObject];
      CGPoint touchPoint = [touch locationInView:self.sceneView];
      SCNHitTestResult *hitTestResult = [[self.sceneView 
      hitTest:touchPoint options:nil] firstObject];
      SCNNode *hitNode = hitTestResult.node;

      for (SCNNode *node in ArrBoxNode) {
         [node removeFromParentNode];
   } 
 }

但我无法从 Tap 操作中删除节点。能否请您帮助我,并提出更好的建议,谢谢... :)

【问题讨论】:

  • 试试[hitNode removeFromParentNode]; 告诉我
  • 这可行,但是当我添加新多维数据集时它不起作用
  • 首先检查你是否触摸了那个物体,也许是发生了什么你需要触摸你需要移除的物体,让我知道
  • 工作的兄弟。非常感谢。我发现了一个问题。你能告诉我吗?
  • 我在点击事件上做爆炸动画。当我删除节点时,动画不起作用。请检查

标签: ios objective-c xcode scenekit scnnode


【解决方案1】:

您需要使用[hitNode removeFromParentNode]; 删除您正在触摸的节点

代码

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  {
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self.sceneView];
    SCNHitTestResult *hitTestResult = [[self.sceneView hitTest:touchPoint options:nil] firstObject];
    SCNNode *hitNode = hitTestResult.node;
    [hitNode removeFromParentNode];
}

【讨论】:

    猜你喜欢
    • 2019-10-28
    • 1970-01-01
    • 1970-01-01
    • 2019-06-26
    • 2017-11-14
    • 2014-10-07
    • 1970-01-01
    • 2011-12-29
    • 2018-06-20
    相关资源
    最近更新 更多