1

Is there any way to catch touches (touchdowns, touchups etc) of individual meshes, or really SCNNodes, in SceneKit?

Jonny
  • 15,955
  • 18
  • 111
  • 232

3 Answers3

3

An answer is in the SceneKit game template... :^) In handleTap:, there is an example on how to use hitTest:options: which is the way to go.

Jonny
  • 15,955
  • 18
  • 111
  • 232
1

If you open up the default SceneKit game template in Xcode you will see code that allows you do recognize if a node got touched. In viewDidLoad write the following code...

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(customMehtod:)];
NSMutableArray *gestureRecognizers = [NSMutableArray array];
[gestureRecognizers addObject:tapGesture];
[gestureRecognizers addObjectsFromArray:scnView.gestureRecognizers];
scnView.gestureRecognizers = gestureRecognizers;

Now create the custom method with the following code inside:

-(void)boardTapped:(UIGestureRecognizer*)gestureRecognize
{

 // check what nodes are tapped
CGPoint p = [gestureRecognize locationInView:scnView];
NSArray *hitResults = [scnView hitTest:p options:nil];

if([hitResults count] > 0){
    SCNHitTestResult *result = [hitResults objectAtIndex:0];
    if(result.node == myNode){
       //do something...
    }
}

And thats it! put whatever node you want in the if statement: if(result.node == myNode).

Harsh.C
  • 83
  • 8
-2

touchDown is best achieved using a longPress gesture with a very short duration, unless you want to override the touches.

Answered before: UITapGestureRecognizer - make it work on touch down, not touch up?

And the hit test works once you grab the x/y position of the tap and hitTest it. ( see the Xcode boilerplate code for this )

Community
  • 1
  • 1
mhill
  • 35
  • 2