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.
let tapRecognizer = UITapGestureRecognizer(target: self, action: "tapped:")
tapRecognizer.allowedPressTypes = [NSNumber(integer: UIPressType.PlayPause.rawValue)];
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.
let swipeRecognizer = UISwipeGestureRecognizer(target: self, action: "swiped:")
swipeRecognizer.direction = .Right
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.
override func pressesBegan(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
for item in presses {
if item.type == .Select {
self.view.backgroundColor = UIColor.greenColor()
}
}
}
override func pressesEnded(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
for item in presses {
if item.type == .Select {
self.view.backgroundColor = UIColor.whiteColor()
}
}
}
override func pressesChanged(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
// ignored
}
override func pressesCancelled(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
for item in presses {
if item.type == .Select {
self.view.backgroundColor = UIColor.whiteColor()
}
}
}
Controlling the User Interface with the Apple TV Remote
Copyright © 2018 Apple Inc. All rights reserved. Terms of Use | Privacy Policy | Updated: 2017-01-12