Xcode tvos application gets out of problem when redefining menu button

I am currently writing a tvOS application. I discovered and redefined the menu button using tapRecognizer to switch between storyboards and other features. My problem is that when I am on my home screen and press the menu, it does not exit the application. Instead, it remembers the last function that I used when overriding the menu button and performs that function. Any thoughts on how to clear tapRecognizer? Or a function that exits the application?

I override the menu button with

at storyboard1

tapRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(home)]; tapRecognizer.allowedPressTypes = @[[NSNumber numberWithInteger:UIPressTypeMenu]]; [self.view addGestureRecognizer:tapRecognizer]; 

in my home routine, I send the user back to my storyboard home page. But from now on, the menu button will not exit the application, but will send me back to the storyboard1. thanks, SW

+6
source share
6 answers

Instead of using your own gesture recognizer, override pressesBegan:

 override func pressesBegan(presses: Set<UIPress>, withEvent event: UIPressesEvent?) { if(presses.first?.type == UIPressType.Menu) { // handle event } else { // perform default action (in your case, exit) super.pressesBegan(presses, withEvent: event) } } 
+13
source

You need to override 2 methods to prevent the application from exiting by pressing the "Menu" button.

Here is a ready-to-use template:

 override func pressesBegan(presses: Set<UIPress>, withEvent event: UIPressesEvent?) { for press in presses { switch press.type { case .Menu: break default: super.pressesBegan(presses, withEvent: event) } } } override func pressesEnded(presses: Set<UIPress>, withEvent event: UIPressesEvent?) { for press in presses { switch press.type { case .Menu: //Do some staff there! self.menuButtonPressed() default: super.pressesEnded(presses, withEvent: event) } } } 
+4
source

If you rewrite the menu button, the application will not be accepted:

EDIT: you can overwrite, but the menu button should work like the "Back" button on the main screen from the application entry point.

10.1 Details

The Menu button on Siri Remote does not behave as expected in your application.

In particular, when the user launches the application and presses the "Menu" button on the remote Siri device, the application does not go to the Apple TV main screen.

Next steps

Please review your app to make sure the Siri remote is as expected and follow the Apple TV Interface Guidelines.

+2
source

If you use UIGestureRecognizer instead of responding to clicks, all you have to do is turn off the recognizer:

 tapRecognizer.enabled = NO; 

So, if the recognizer is not recognized with UIPressTypeMenu , tvOS pauses the application and displays the main screen. (I tested this)

+1
source

This may help you ... this is quick code.

 let menuPressRecognizer = UITapGestureRecognizer() menuPressRecognizer.addTarget(self, action: #selector(YourViewController.menuButtonAction(_:))) menuPressRecognizer.allowedPressTypes = [NSNumber(integer: UIPressType.Menu.hashValue)] self.view.addGestureRecognizer(menuPressRecognizer) 
0
source

According to Apple documentation, for custom print processing, we must override all four of these 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); 

This is the official documentation from Xcode:

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

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

click this processes (those presses that he received in pressesBegan: withEvent :).

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

*** You must handle canceled clicks to ensure the correct behavior in your application. Inability This may result in abnormal behavior or malfunction.

0
source

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


All Articles