如何处理 tvOS 遥控器中的菜单按钮操作
How to handle Menu Button action in tvOS remote
我的应用程序遇到了一个奇怪的问题。实际上,当我展示一个用于播放视频的视图控制器时。在视频加载时,用户按下菜单按钮,应用程序进入后台。虽然我已经覆盖了菜单按钮操作。
这是我的代码。
override func viewWillAppear(animated: Bool) {
let menuPressRecognizer = UITapGestureRecognizer()
menuPressRecognizer.addTarget(self, action: #selector(VideoPlayerViewController.menuButtonAction(_:)))
menuPressRecognizer.allowedPressTypes = [NSNumber(integer: UIPressType.Menu.hashValue)]
self.playerController.view.addGestureRecognizer(menuPressRecognizer)
}
func menuButtonAction(ges:UITapGestureRecognizer) {
self.dismissView()
}
当您指定 allowedPressTypes
:
时,您应该使用枚举的 rawValue
而不是 hash
menuPressRecognizer = [NSNumber(value: UIPressType.menu.rawValue)]
这是我的代码,为我工作。
Swift 3
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let menuPressRecognizer = UITapGestureRecognizer()
menuPressRecognizer.addTarget(self, action: #selector(YourViewController.menuButtonAction(recognizer:)))
menuPressRecognizer.allowedPressTypes = [NSNumber(value: UIPressType.menu.rawValue)]
self.view.addGestureRecognizer(menuPressRecognizer)
}
func menuButtonAction(recognizer:UITapGestureRecognizer) {
self.dismiss(animated: true, completion: nil)
}
Swift 4
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let menuPressRecognizer = UITapGestureRecognizer()
menuPressRecognizer.addTarget(self, action: #selector(YourViewController.menuButtonAction(recognizer:)))
menuPressRecognizer.allowedPressTypes = [NSNumber(value: UIPressType.menu.rawValue)]
self.view.addGestureRecognizer(menuPressRecognizer)
}
@objc func menuButtonAction(recognizer:UITapGestureRecognizer) {
self.dismiss(animated: true, completion: nil)
}
Swift 4.2 & 5
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let menuPressRecognizer = UITapGestureRecognizer()
menuPressRecognizer.addTarget(self, action: #selector(YourViewController.menuButtonAction(recognizer:)))
menuPressRecognizer.allowedPressTypes = [NSNumber(value: UIPress.PressType.menu.rawValue)]
self.view.addGestureRecognizer(menuPressRecognizer)
}
@objc func menuButtonAction(recognizer:UITapGestureRecognizer) {
self.dismiss(animated: true, completion: nil)
}
当你想多次添加多个值时,你可以做简单的:
let pressTypes: [NSNumber] = [UIPressType.select, UIPressType.playPause, UIPressType.rightArrow].map{ NSNumber(value: [=10=].rawValue)}
这也可以在 IB 中完成。 TapGestureRecognizer 上有一个按钮选项,其中包括菜单按钮。
为了更细化,您还可以使用以下方法:
- (void)pressesBegan:(NSSet<UIPress *> *)presses withEvent:(nullable UIPressesEvent *)event NS_AVAILABLE_IOS(9_0);
- (void)pressesChanged:(NSSet<UIPress *> *)presses withEvent:(nullable UIPressesEvent *)event NS_AVAILABLE_IOS(9_0);
- (void)pressesEnded:(NSSet<UIPress *> *)presses withEvent:(nullable UIPressesEvent *)event NS_AVAILABLE_IOS(9_0);
- (void)pressesCancelled:(NSSet<UIPress *> *)presses withEvent:(nullable UIPressesEvent *)event NS_AVAILABLE_IOS(9_0);
如果您按照 Apple 的以下建议选择使用其中任何方法,请确保覆盖所有这些方法:
Generally, all responders which do custom press handling should
override all four of these methods.
Your responder will receive either pressesEnded:withEvent or
pressesCancelled:withEvent: for each
press it is handling (those presses it received in
pressesBegan:withEvent:).
pressesChanged:withEvent: will be invoked for presses that provide an
analog value (like thumbsticks or analog push buttons)
*** You must handle cancelled presses to ensure correct behavior in your application. Failure to do so is very likely to lead to incorrect
behavior or crashes.
Swift 5:
private func setupRemoteGesture() {
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleMenuPress))
tapRecognizer.allowedPressTypes = [NSNumber(value: UIPress.PressType.menu.rawValue)]
view.addGestureRecognizer(tapRecognizer)
}
@objc private func handleMenuPress() {
// Menu button pressed
}
我的应用程序遇到了一个奇怪的问题。实际上,当我展示一个用于播放视频的视图控制器时。在视频加载时,用户按下菜单按钮,应用程序进入后台。虽然我已经覆盖了菜单按钮操作。
这是我的代码。
override func viewWillAppear(animated: Bool) {
let menuPressRecognizer = UITapGestureRecognizer()
menuPressRecognizer.addTarget(self, action: #selector(VideoPlayerViewController.menuButtonAction(_:)))
menuPressRecognizer.allowedPressTypes = [NSNumber(integer: UIPressType.Menu.hashValue)]
self.playerController.view.addGestureRecognizer(menuPressRecognizer)
}
func menuButtonAction(ges:UITapGestureRecognizer) {
self.dismissView()
}
当您指定 allowedPressTypes
:
rawValue
而不是 hash
menuPressRecognizer = [NSNumber(value: UIPressType.menu.rawValue)]
这是我的代码,为我工作。
Swift 3
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let menuPressRecognizer = UITapGestureRecognizer()
menuPressRecognizer.addTarget(self, action: #selector(YourViewController.menuButtonAction(recognizer:)))
menuPressRecognizer.allowedPressTypes = [NSNumber(value: UIPressType.menu.rawValue)]
self.view.addGestureRecognizer(menuPressRecognizer)
}
func menuButtonAction(recognizer:UITapGestureRecognizer) {
self.dismiss(animated: true, completion: nil)
}
Swift 4
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let menuPressRecognizer = UITapGestureRecognizer()
menuPressRecognizer.addTarget(self, action: #selector(YourViewController.menuButtonAction(recognizer:)))
menuPressRecognizer.allowedPressTypes = [NSNumber(value: UIPressType.menu.rawValue)]
self.view.addGestureRecognizer(menuPressRecognizer)
}
@objc func menuButtonAction(recognizer:UITapGestureRecognizer) {
self.dismiss(animated: true, completion: nil)
}
Swift 4.2 & 5
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let menuPressRecognizer = UITapGestureRecognizer()
menuPressRecognizer.addTarget(self, action: #selector(YourViewController.menuButtonAction(recognizer:)))
menuPressRecognizer.allowedPressTypes = [NSNumber(value: UIPress.PressType.menu.rawValue)]
self.view.addGestureRecognizer(menuPressRecognizer)
}
@objc func menuButtonAction(recognizer:UITapGestureRecognizer) {
self.dismiss(animated: true, completion: nil)
}
当你想多次添加多个值时,你可以做简单的:
let pressTypes: [NSNumber] = [UIPressType.select, UIPressType.playPause, UIPressType.rightArrow].map{ NSNumber(value: [=10=].rawValue)}
这也可以在 IB 中完成。 TapGestureRecognizer 上有一个按钮选项,其中包括菜单按钮。
为了更细化,您还可以使用以下方法:
- (void)pressesBegan:(NSSet<UIPress *> *)presses withEvent:(nullable UIPressesEvent *)event NS_AVAILABLE_IOS(9_0);
- (void)pressesChanged:(NSSet<UIPress *> *)presses withEvent:(nullable UIPressesEvent *)event NS_AVAILABLE_IOS(9_0);
- (void)pressesEnded:(NSSet<UIPress *> *)presses withEvent:(nullable UIPressesEvent *)event NS_AVAILABLE_IOS(9_0);
- (void)pressesCancelled:(NSSet<UIPress *> *)presses withEvent:(nullable UIPressesEvent *)event NS_AVAILABLE_IOS(9_0);
如果您按照 Apple 的以下建议选择使用其中任何方法,请确保覆盖所有这些方法:
Generally, all responders which do custom press handling should override all four of these methods.
Your responder will receive either pressesEnded:withEvent or pressesCancelled:withEvent: for each
press it is handling (those presses it received in pressesBegan:withEvent:).
pressesChanged:withEvent: will be invoked for presses that provide an analog value (like thumbsticks or analog push buttons)
*** You must handle cancelled presses to ensure correct behavior in your application. Failure to do so is very likely to lead to incorrect behavior or crashes.
Swift 5:
private func setupRemoteGesture() {
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleMenuPress))
tapRecognizer.allowedPressTypes = [NSNumber(value: UIPress.PressType.menu.rawValue)]
view.addGestureRecognizer(tapRecognizer)
}
@objc private func handleMenuPress() {
// Menu button pressed
}