The Siri remote is a great way to control the Apple TV. The remote has a simplistic look but is still extremely powerful. When creating an app for tvOS, users will love your app when you adopt the Siri Remote functionalities.

For example, when you scroll through a list of movies, users can press the Touch Surface button to read the details of a movie. If you adopt the play button on the Siri Remote, users can play the movie directly from the list!

Buttons on the Siri Remote.

Detecting Button Presses

Detecting a press on the Siri Remote can be done in two different functions. Each ViewController contains a pressesBegan and pressesEnded function, which you can override and write your own implementation.

There are two buttons of the Siri Remote that can be detected, which are the menu and play/pause button. You can also detect a press on the Touch Surface, which is a select action. All the buttons that can be detected are listed in the UIPress.PressType enum.

The code example below shows how a button can be detected. As soon as the user presses on a button, the pressesBegan function will be triggered. Inside this function, you are able to get the type of the button and implement your own code to act on this button press!

override func pressesBegan(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
   guard let buttonPress = presses.first?.type else { return }
        
   switch(buttonPress) {
   case .menu:
      print("Menu")
   case .playPause:
      print("Play/Pause")
   case .select:
      print("select")
   case .upArrow:
      print("Up arrow")
   case .downArrow:
      print("Down arrow")
   case .leftArrow:
      print("Left arrow")
   case .rightArrow:
      print("right arrow")
   }
}

You’re not only able to detect presses on the Siri Remote, but also the arrow up, down, left and right if a user connected a keyboard to the Apple TV.

Detecting Gestures

Gestures can be recognized easily using an UISwipeGestureRecognizer object. First, initialize the UISwipeGestureRecognizer object and set a target and a function that should be triggered when the gesture is recognized.

Then, you can set a specific direction for the swipe gesture. By default, the direction is set to right. Other options are left, bottom and top.

After creating the gesture recognizer, add the gesture recognizer to your view and the function will be triggered as soon as your user performs the gesture.

func setupRightSwipedGestureRecognizer() {
   let swipeRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(swipedRight))
   swipeRecognizer.direction = .right
   view.addGestureRecognizer(swipeRecognizer)
}
    
@objc func swipedRight() {
   print("Swiped right")
}

Conclusion

Detecting a button press or gesture on the Siri Remote isn’t hard, but will give your users a better experience when using your app on the Apple TV.

The menu and select button can be detected, but most of the time there are no logic situations to detect a press for those buttons. Only add actions for those buttons if needed, to prevent you don’t comply with Apple’s Human Interface Guidelines.

🚀 Like this article? Follow me on Twitter for more tvOS related news!

Categories:tvOS