tvOS:间歇性调用 pressesEnded

tvOS: pressesEnded is called intermittently

我正在创建 UIButton 的子类,我试图拦截触摸的原因是因为我似乎无法找到另一种方法来接收 'press up' 或 'press ended' 事件tvOS 中的标准 UIButton。如果我能找到一种方法来做到这一点,那么我就不需要为下面的解决方案而烦恼了。

pressesEnded(_ presses: Set<UIPress>, with event: UIPressesEvent?) 似乎不会在我每次松开 Apple TV Remote 上的 'select' 按钮时被调用。

pressesBegan(_ presses: Set<UIPress>, with event: UIPressesEvent?) 每次都被调用,没有任何问题。我在下面附上了我的代码。有什么想法可能导致此问题吗?

class EVLPTZButton: UIButton
{
  var command: PTZCommand!
  var delegate: EVLPTZButtonCommandDelegate?

  override func pressesBegan(_ presses: Set<UIPress>, with event: UIPressesEvent?)
  {
    super.pressesBegan(presses, with: event)

    delegate?.ptzButton(pressesBeganFor: self, with: command)
  }

  override func pressesEnded(_ presses: Set<UIPress>, with event: UIPressesEvent?)
  {
    super.pressesEnded(presses, with: event)

    delegate?.ptzButton(pressesEndedFor: self)
  }
}

经过更多测试后,似乎当释放 'select' 按钮时,tvOS 会调用 pressesEnded(_ presses: Set<UIPress>, with event: UIPressesEvent?) pressesCancelled(_ presses: Set<UIPress>, with event: UIPressesEvent?).

我通过跳转到 pressesEnded(_ presses: Set<UIPress>, with event: UIPressesEvent?) 的定义并找到这条评论找到了这个解决方案:

Your responder will receive either pressesEnded:withEvent or pressesCancelled:withEvent: for each press it is handling (those presses it received in pressesBegan:withEvent:).