tvOS UIPress手势无法在SKScene的子类中识别

时间:2015-09-19 05:28:13

标签: swift tvos

我正在尝试回应SKScene子类中的新闻。我可以在我的ViewController内部覆盖响应者pressEnded,但是当我将pressEnded覆盖移动到我的SKScene子类中时,我不再接收任何调用。

下面是我的pressEnabled覆盖,它在ViewController内部按预期工作

override func pressesEnded(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
    print("press")
}

有人知道如何在SKScene内部接收按钮吗?

2 个答案:

答案 0 :(得分:7)

您需要将您的新闻事件从ViewController转发到SKScene,就像这样......

override func pressesBegan(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {   
  gameScene.pressesBegan(presses, withEvent: event)
}

override func pressesEnded(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
  gameScene.pressesEnded(presses, withEvent: event)
}

然后在gameScene(SKScene)做类似的事情:

override func pressesBegan(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
  for press in presses {
    switch press.type {
    case .UpArrow:
      print("Up Arrow")
    case .DownArrow:
      print("Down arrow")
    case .LeftArrow:
      print("Left arrow")
    case .RightArrow:
      print("Right arrow")
    case .Select:
      print("Select")
    case .Menu:
      print("Menu")
    case .PlayPause:
      print("Play/Pause")
    }
  }
}

override func pressesEnded(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
  print("Presses Ended.")
}

当用户点击触控板的边缘时会发送“箭头”按钮,但在模拟器中无法识别。点击触摸板的中心时会发送“选择”。

我希望这有帮助!

答案 1 :(得分:1)

使用pressesBegan为Apple TV遥控器的触控板(左,右等)映射物理按钮(菜单,PlayPause)和pressesBeganpressesEnded。 如果您按下触控板的“按钮”并移动到另一个位置并释放,则不会调用方法pressesEnded,您无法使用物理按钮执行此操作。

新闻发布:

override func pressesBegan(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
        print("Press began")
        for item in presses {
            if item.type == .PlayPause {
                    print("PlayPause")
            }
        }
}

按结束:

override func pressesEnded(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
        print("Press Ended")
        for item in presses {
            if item.type == .RightArrow {
                print("Right Arrow")
            }
        }
    }