How to handle Menu Button action in tvOS remote

最后都变了- 提交于 2019-12-23 18:24:23

问题


I am facing the strange problem with my application. Actually when i am presenting a view controller for play the video. At the video load time user press the menu button the application goes to background. While i have overwrite the Menu Button Action.

This is my code.

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()
 }

回答1:


This is my code and working for me.

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)
}



回答2:


You should use enum's rawValue instead of hash when you specify allowedPressTypes:

menuPressRecognizer = [NSNumber(value: UIPressType.menu.rawValue)]




回答3:


When you want to multiple add multiple values you can do simple:

let pressTypes: [NSNumber] = [UIPressType.select, UIPressType.playPause, UIPressType.rightArrow].map{ NSNumber(value: $0.rawValue)}



回答4:


This can also be done in IB. There's an option on TapGestureRecognizer for buttons which includes the menu button.




回答5:


For more granularity, you can also make use of the following methods:

- (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);

Please make sure to override all of these methods if you choose to use any of them as per Apple's suggestions given below:

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.



来源:https://stackoverflow.com/questions/40634638/how-to-handle-menu-button-action-in-tvos-remote

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!