Documentation Archive

Developer

App Programming Guide for tvOS

On This Page

Detecting Gestures and Button Presses

Most UIKit views react appropriately when the user presses a button on the remote or makes a gesture on the touchpad. For example, a UIButton object sends its action message when it has focus and the user presses the select button. However, you may also want to perform custom actions in your app when the user presses a button or makes a gesture, just as you would with touch events on iOS. Button press events are handled through the responder chain, just like other events. The UIGestureRecognizer and UIResponder classes include new methods to respond when buttons on the remote are pressed or released. In addition, gesture recognizers that work with movement gestures, such as pan and swipe recognizers, automatically work when the gesture is performed on a Siri Remote’s touchpad.

Using Gesture Recognizers

Tap gesture recognizers can be used to detect button presses. By default, a tap gesture recognizer is triggered when the Select button is pressed. The allowedPressTypes property is used to specify which buttons trigger the recognizer.

Listing 4-1 creates a gesture recognizer that is triggered when the Play/Pause button is pressed.

Listing 4-1Detecting the Play/Pause button
  1. let tapRecognizer = UITapGestureRecognizer(target: self, action: "tapped:")
  2. tapRecognizer.allowedPressTypes = [NSNumber(integer: UIPressType.PlayPause.rawValue)];
  3. self.view.addGestureRecognizer(tapRecognizer)

Similarly, a swipe or pan gesture recognizer can be used to detect motions across the remote’s touchpad. Listing 4-2 shows how to detect a left-to-right gesture across the touchpad.

Listing 4-2Detecting a swipe gesture
  1. let swipeRecognizer = UISwipeGestureRecognizer(target: self, action: "swiped:")
  2. swipeRecognizer.direction = .Right
  3. self.view.addGestureRecognizer(swipeRecognizer)

Working with Low-Level Event Handling

A UIPress object is analogous to a UITouch object, but provides information about buttons on a remote or on other devices, such as a game controller. A UIPress object tells you which button is being described and the current state of the button, such as whether the button was just pressed, just released, or is still being held down. For analog buttons, a UIPress object provides information about how much force is being applied to the button. The type property specifies which physical button’s state has changed, and the other properties of the UIPress object describe the change.

UIGestureRecognizer and UIResponder objects can implement methods to be called when press events are triggered and delivered to the responder chain. These methods all receive a UIPressesEvent object that describes the event as well as a set that describes the buttons that just changed state. Listing 4-3 shows how a view controller might implement low-level handling for a select event. As with touch event handling, when you implement handling for press events, if you implement any of the press method handlers, you should implement all four of them.

Listing 4-3Responding to low-level press events
  1. override func pressesBegan(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
  2. for item in presses {
  3. if item.type == .Select {
  4. self.view.backgroundColor = UIColor.greenColor()
  5. }
  6. }
  7. }
  8. override func pressesEnded(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
  9. for item in presses {
  10. if item.type == .Select {
  11. self.view.backgroundColor = UIColor.whiteColor()
  12. }
  13. }
  14. }
  15. override func pressesChanged(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
  16. // ignored
  17. }
  18. override func pressesCancelled(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
  19. for item in presses {
  20. if item.type == .Select {
  21. self.view.backgroundColor = UIColor.whiteColor()
  22. }
  23. }
  24. }