How to handle menu button action in tvOS remote

I ran into a weird problem with my application. In fact, when I present a view controller for playing videos. At the time of downloading the video, the user presses the menu button, the application goes into the background. While I am rewriting the action of the menu button.

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() } 
+3
source share
6 answers

This is my code and works 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) } 
+8
source

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

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

+4
source

Swift 4 solution (based on anasaman_p answer ):

 class MyViewController : UIViewController { override func viewDidLoad() { super.viewDidLoad() let menuPressRecognizer = UITapGestureRecognizer() menuPressRecognizer.addTarget(self, action: #selector(MyViewController.menuButtonAction(recognizer:))) menuPressRecognizer.allowedPressTypes = [NSNumber(value: UIPressType.menu.rawValue)] self.view.addGestureRecognizer(menuPressRecognizer) } @objc func menuButtonAction(recognizer:UITapGestureRecognizer) { // Add any code here you want to run after the menu button pressed } } 
+1
source

If you want to add multiple values โ€‹โ€‹several times, you can do this simply:

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

This can also be done at IB. TapGestureRecognizer has an option for buttons that includes a menu button.

0
source

For more detail, you can also use 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); 

Be sure to override all of these methods if you decide to use any of them in accordance with Apple's recommendations below:

Generally, all respondents who perform custom press processing should override all four of these methods.

Your respondent will receive either pressesEnded: withEvent or pressesCancelled: withEvent: for each

the press processes this (those presses that he received in pressesBegan: withEvent :)

. pressesChanged: withEvent: will be called for presses that provide an analog value (e.g. buttons or analog buttons)

*** You must handle canceled prints to ensure correct behavior in your application. Failure to do so may result in abnormal behavior or malfunctions.

0
source

Source: https://habr.com/ru/post/1233076/


All Articles