SKNode上的UITapGestureRecognizer:将坐标从UIView转换为SKNode

gle*_*rey 6 objective-c ios uitapgesturerecognizer sprite-kit

我有一个UITapGestureRecognizerUIPanGestureRecognizerUIView一个SKScene就可以了.平移手势识别器从左向右移动SKNode,并且我希望Tap手势识别器检测到平移的SKNode的孩子.平移工作正常,但我在检测水龙头时遇到问题 - Tap Gesture会触发相关方法,但我不确定如何将坐标从视图转换到场景到节点以检测水龙头是否在其中一个子节点.

UIView(带手势)→SKScene→平移节点→平移节点的子节点

如何检查轻击手势的触摸坐标是否为任何给定的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);

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

}

gle*_*rey 8

最后,我需要从建议的步骤中做更多的步骤 - 从SKView→SKScene转换到包含我正在测试的节点的SKNode.

    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];
Run Code Online (Sandbox Code Playgroud)


rak*_*hbs 6

您应该使用查看坐标转换为场景坐标 convertPointFromView:

CGPoint touchLocationInView = [sender locationOfTouch:0 inView:sender.view];
CGPoint touchLocationInScene = [self convertPointFromView:touchLocationInView];
Run Code Online (Sandbox Code Playgroud)

然后,您可以检测使用哪个标签节点,

for (SKLabelNode *node in self.children) {

    if ([node containsPoint:touchLocationInScene]) {
        //This is where I want the tap to be detected.
    }

}
Run Code Online (Sandbox Code Playgroud)