ios - 检测 UIView 上的触摸

标签 ios objective-c uiview touch sprite-kit

我有一个类似于 UIButton 的节点和一个在触摸屏幕时执行操作的节点。
问题是狮子跳跃 Action 没有被触发。
另外:两个节点的名字都是正确的。

我的 UIEvent 代码:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UITouch *touch = [touches anyObject];
        CGPoint location = [touch locationInNode: self];
        SKNode *node = [self nodeAtPoint:location];

        UITouch *lionTouch = [touches anyObject];
        CGPoint locationLion = [lionTouch locationInView:[self view]];
        SKNode *lionNode = [self nodeAtPoint:locationLion];

        if ([lionNode.name isEqualToString:@"veggieLion"]) {
            if (!lionIsJumping) {
                [self lionJump];

                lionIsJumping = YES;
            }
        }

        if ([node.name isEqualToString:@"repalyButton"]) {
            // Button action
        }
    }

最佳答案

Apple documentation for SKNode说方法nodeAtPoint “返回与点相交的最深后代。”

我过去也遇到过这个问题。该方法将返回我用于背景的 SKSpriteNode,即使我肯定是在点击目标 Sprite /节点!

由于您正在检查两个节点,我将通过以下方式处理它:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  UITouch *touch = [touches anyObject];
  CGPoint location = [touch locationInNode: self];

  SKNode * lionNode = [self childNodeWithName:@"veggieLion"];
  SKNode * replayButton = [self childNodeWithName:@"repalyButton"];

  if ([self.lionNode containsPoint:location]) {
    if (!lionIsJumping) {
      [self lionJump];
      lionIsJumping = YES;
    }
    return;
  }

  if ([self.replayButton containsPoint:location]) {
    // Button action
    return;
  }

  /* test for other targets */
}

关于ios - 检测 UIView 上的触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21957429/

相关文章:

ios - 如何根据其中的 UILabel 使 UIView 自动高度?

iphone - 在 iPhone 上生成 SHA1(图像)? (不正确的结果)

ios - 警告-[UIApplication delegate] 只能在主线程中使用

objective-c - 带有 png 叠加层的 xCode 视频播放器

objective-c - 在 C 和 Objective C 中,我们是否应该几乎永远不要仅使用 (int) 将 float 或 double 值转换为整数?

ios - 背景图像在窗口/ View 中太大

ios - 使用 CoreBluetooth 在 iOS 上更改间隔时间读取信标 RSSI

iOS代码组织模式

ios - 核心数据迁移: Could not cast value of type 'NSManagedObject_MyType' to 'MyModule.MyType'

ios - 不同动画 block 中的 CGAffineTransform 属性