覆盖菜单按钮时 xcode tvos 应用程序退出问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32975420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:00:16  来源:igfitidea点击:

xcode tvos app exiting issue when overriding menu button

xcodemenutvos

提问by Swick

I am currently writing a tvOS app. I've been detecting and overriding the menu button with tapRecognizer to switch between storyboards and other functions. My issue is when I am on my home screen and press menu it does not exit the app. Instead it remembers the last function I used when overriding the menu button and performs that function. Any thoughts on how to clear the tapRecognizer? Or a function that will exit the app?

我目前正在编写一个 tvOS 应用程序。我一直在使用 tapRecognizer 检测和覆盖菜单按钮,以在故事板和其他功能之间切换。我的问题是当我在主屏幕上按菜单时,它不会退出应用程序。相反,它会记住我在覆盖菜单按钮并执行该功能时使用的最后一个功能。关于如何清除 tapRecognizer 的任何想法?或将退出应用程序的功能?

I'm overriding the menu button with

我覆盖菜单按钮

in Storyboard1

在故事板 1 中

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

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

在我的 home 子程序中,我将用户发送回我的主页故事板。但从那时起,菜单按钮将不会退出应用程序,而是将我送回 storyboard1。谢谢,SW

回答by ehynds

Instead of using your own gesture recognizer, override pressesBegan:

不使用您自己的手势识别器,而是覆盖 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)
  }
}

回答by skywinder

You have to override 2 methods to prevent exiting app by pressing Menu button.

您必须覆盖 2 种方法以防止通过按菜单按钮退出应用程序。

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

回答by matthiaswitt

If you overwrite the menu button, the app won't be accepted:

如果您覆盖菜单按钮,则不会接受该应用程序:

EDIT: You can overwrite, but the menu button has to work as a back button to homescreen from the entry point of the app.

编辑:您可以覆盖,但菜单按钮必须作为从应用程序入口点返回到主屏幕的后退按钮。

10.1 Details

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

Specifically, when the user launches the app and taps the Menu button on the Siri remote, the app does not exit to the Apple TV Home screen.

Next Steps

Please revise your app to ensure that the Siri remote buttons behave as expected and comply with the Apple TV Human Interface Guidelines.

10.1 详情

Siri Remote 上的“菜单”按钮在您的应用中未按预期运行。

具体来说,当用户启动应用程序并点击 Siri 遥控器上的菜单按钮时,应用程序不会退出到 Apple TV 主屏幕。

下一步

请修改您的应用程序以确保 Siri 遥控器按钮的行为符合预期并符合 Apple TV 人机界面指南。

回答by bio

If you are using UIGestureRecognizerinstead of responding to presses, all you need to do is to disable the recognizer:

如果您使用UIGestureRecognizer而不是响应按下,您需要做的就是禁用识别器:

tapRecognizer.enabled = NO;

So if no recognizer with UIPressTypeMenuis listening, tvOS suspends the app and displays the home screen. (I've tested this)

因此,如果没有识别器UIPressTypeMenu正在侦听,tvOS 会暂停应用程序并显示主屏幕。(我已经测试过了)

回答by Sankalap Yaduraj Singh

It may be help you... it is swift code.

它可能对你有帮助......它是快速代码。

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

回答by atulkhatri

As per Apple's documentation, for custom press handling, we should override all four of these methods-

根据 Apple 的文档,对于自定义新闻处理,我们应该覆盖所有这四种方法 -

- (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:

这是来自 XCode 的官方文档:

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.

通常,所有进行自定义新闻处理的响应者都应该覆盖所有这四种方法。

您的响应者将收到 pressesEnded:withEvent 或 pressesCancelled:withEvent: 对于每个

按它正在处理(在 pressesBegan:withEvent: 中收到的那些按)。

pressesChanged:withEvent: 将为提供模拟值的按下调用(如拇指操纵杆或模拟按钮)

*** 您必须处理取消的印刷机以确保您的应用程序中的正确行为。如果不这样做,很可能会导致不正确的行为或崩溃。