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

我在UIView上有一个UITapGestureRecognizer和一个UIPanGestureRecognizer,上面有一个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);

        }
    }
}

}

解决方法

最后,我需要从建议的步骤中做更多的步骤 – 从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];

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...