ios - 如何在点击 SCNNode 时删除它?

标签 ios objective-c xcode scenekit scnnode

我有使用 SceneView 的 Creat Cubes,我想在点击操作时消失立方体。如何实现?

这是我创建 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]; 删除您正在触摸的节点

代码

- (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];
}

关于ios - 如何在点击 SCNNode 时删除它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47977468/

相关文章:

ios - 继承 MKAnnotationView 并从 initWithFrame : overloads 调用 init 时无限循环

asp.net - 无法从 JSON Webservice 获取值

ios - 如何在ios中点击内圈时检测区域

ios - 找不到GoogleUtilities/GULSceneDelegateSwizzler.h文件

iphone - 如何确认 HTTP POST 调用了我的代码?

iphone - 运行循环结束——自动释放池恢复

iPhone 开发人员与任何有效证书不匹配 (Xcode)

iphone - iOS : Getting this message (not an error) on console- Application windows are expected to have a root view controller at the end of application launch

iphone - 在不使用 UITabBarController 创建项目的情况下使用 UITabBar

ios - 无法在 Collection View 中创建单元格?