ios - SKNode : Converting Coordinates from UIView to SKNode 上的 UITapGestureRecognizer

标签 ios objective-c sprite-kit uitapgesturerecognizer

我在带有 SKSceneUIView 上有一个 UITapGestureRecognizer 和一个 UIPanGestureRecognizer。平移手势识别器从左到右移动一个 SKNode,我希望 Tap 手势识别器检测平移的 SKNode 的子节点。平移工作正常,但我在检测点击时遇到问题 - Tap Gesture 触发了相关方法,但我不确定如何将坐标从 View 转换为场景到节点以检测点击是否在其中之一子节点。

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 转换为包含我进行 HitTest 的节点的 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];

关于ios - SKNode : Converting Coordinates from UIView to SKNode 上的 UITapGestureRecognizer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29110944/

相关文章:

ios - 如何清除 iOS ADAL 库中的缓存

objective-c - 在 Swift 中将后台任务作为循环运行

ios - SpriteKit - SKCameraNode 约束在顶部和底部留有空间

position - SKSpriteNode 上的 CGPoint x 无法正常工作(一半节点超出屏幕)

ios - 我的 json 响应的格式与我预期的不同

ios - 如何翻转 iOS UITableViewCell?

ios - Firebase FIRStorageUploadTask 在应用程序处于后台状态时在 Swift 上

ios - 使用 DJI SDK 创建 VR 应用

iphone - 如何判断自 NSDate 以来的小时数

swift - SpriteKit : stuck trying to use SKNode. contains() 缩放 Sprite