0

I am creating a subclass of a UIButton, the reason why I'm trying to intercept the touch is because I cant seem to find another way to receive 'press up' or 'press ended' events for the standard UIButton in tvOS. If I could find a way to do that then I wouldn't need to bother with the solution below.

pressesEnded(_ presses: Set<UIPress>, with event: UIPressesEvent?) doesn't seem to be getting called every time I release the 'select' button on the Apple TV Remote.

pressesBegan(_ presses: Set<UIPress>, with event: UIPressesEvent?) is being called every time without any issues. I've attached my code below. Any ideas what could be causing this issue?

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)
  }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Rhuari Glen
  • 497
  • 2
  • 5
  • Can you explain a bit about *why* you want the press events for the button? If you just want to know when the button is pressed, you can use the `addTarget(_:action:for:)` method to register your code for the `.primaryActionTriggered` event. – Justin Voss Nov 15 '17 at 19:18
  • You might have more success in watching the focus and/or highlighted states of the button. – picciano Dec 07 '17 at 19:10

1 Answers1

2

After some more testing it seems that when the 'select' button is released tvOS calls either pressesEnded(_ presses: Set<UIPress>, with event: UIPressesEvent?) OR pressesCancelled(_ presses: Set<UIPress>, with event: UIPressesEvent?).

I found this solution by jumping to the definition of pressesEnded(_ presses: Set<UIPress>, with event: UIPressesEvent?) and finding this comment:

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

Rhuari Glen
  • 497
  • 2
  • 5
  • 1
    `pressesCancelled` is called on your view if a gesture recognizer begins handling the button press, so this is totally normal and expected behavior. There are other reasons why a press may be reported as cancelled, but that's the most common reason. – Justin Voss Nov 15 '17 at 19:20