UITapGestureRecognizer in SKNode: coordinate conversion from UIView to SKNode

I have a UITapGestureRecognizer and a UIPanGestureRecognizer on a UIView with SKScene on it. The gesture recognition drive moves the SKNode from left to right, and I want the Tap gesture recognizer to detect a child SKNode that works. Panning works fine, but I'm having trouble detecting taps - Tap Gesture launches the appropriate method, but I'm not sure how to convert the coordinates from the view to the scene in node to detect one of the child nodes.

UIView (with gestures) → SKScene → Panning node → Children panning node

How to check if the touch coordinate of the hard disk matches any given SKNode?

 -(void)tapAction:(UITapGestureRecognizer*)sender{ if (sender.state == UIGestureRecognizerStateEnded) { // handling code CGPoint touchLocation = [sender locationOfTouch:0 inView:sender.view]; NSLog(@"TAP %@", NSStringFromCGPoint(touchLocation) ); for (SKLabelNode *node in _containerNode.children) { if ([node containsPoint:[node convertPoint:touchLocation fromNode:self.parent]]) { //This is where I want the tap to be detected. } CGPoint checkPoint = [node convertPoint:touchLocation fromNode:self.scene]; NSLog(@"CheckPoint %@", NSStringFromCGPoint(checkPoint) ); //NSLog(@"iterating nodes"); if ([node containsPoint:checkPoint]) { NSLog(@"touch match %@", node); } } } 

}

+6
source share
3 answers

In the end, I needed to take a couple more steps from what was suggested - conversion from SKView → SKScene, and then to SKNode, which contained the nodes that I got to.

  CGPoint touchLocation = [sender locationOfTouch:0 inView:sender.view]; CGPoint touchLocationInScene = [[self.scene view] convertPoint:touchLocation toScene:self.scene]; CGPoint touchLocationInNode = [self.scene convertPoint:touchLocationInScene toNode:_containerNode]; 
+6
source

You must convert the view coordinates to scene coordinates using convertPointFromView:

 CGPoint touchLocationInView = [sender locationOfTouch:0 inView:sender.view]; CGPoint touchLocationInScene = [self convertPointFromView:touchLocationInView]; 

Then you can find out which node label was used using

 for (SKLabelNode *node in self.children) { if ([node containsPoint:touchLocationInScene]) { //This is where I want the tap to be detected. } } 
+4
source

I haven't used SceneKit before, but from the docs, it looks like you need to use the SKView convertPoint:toScene: method to convert the coordinates of the gesture recognition point from the coordinates of the view to the coordinates of the scene. Then you need to check the test nodes in your scene to find out which node was used by them.

+3
source

Source: https://habr.com/ru/post/983937/


All Articles