本文介绍了如何检查在scenekit iOS中点击了哪个SCNNode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我在 SceneView 中有三个 SCNNode.我想获得索引路径,比如点击哪个立方体.

Currently i have three SCNNode in SceneView.i want to get indexpath like which cube is Tapped.

这是我的点击事件代码

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


SCNNode *hitNode = hitTestResult.node;
}

这是我创建 SCNNode 的代码.

Here is my code to create SCNNodes.

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

我想如果我点击第一个立方体而不是我应该得到索引路径零.如何实现这一点?

I want like if i am tapping first cube than i should get indexpath Zero.how to achieve this ?

推荐答案

我假设 ArrBoxNode 是您存储多维数据集的数组.在这种情况下,您可以使用简单的 for 循环检查哪些节点被命中.

I assume ArrBoxNode is the array in which you store your cubes. In that case, you can just check which of the nodes was hit with a simple for loop.

- (NSIndexPath*) indexPathFor:(SCNNode*) hitNode {
  for (int i = 0; i < [ArrBoxNode count]; i++) {
    SCNNode* node = ArrBoxNode[i];
    if (node == hitNode) {
      return [NSIndexPath indexPathForItem:i inSection:0];
    }
  }
  return nil;
}

这篇关于如何检查在scenekit iOS中点击了哪个SCNNode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 23:17