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

- 15,955
- 18
- 111
- 232
3 Answers
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.

- 15,955
- 18
- 111
- 232
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).

- 83
- 8
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 )