8

我的 tvOS 应用程序中有一个 UIViewController,它只会出现几秒钟,并且需要完全可自定义的 MENU 按钮处理。我像这样创建它:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Add a tap gesture recognizer to handle MENU presses.
    UITapGestureRecognizer *tapGestureRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    tapGestureRec.allowedPressTypes = @[@(UIPressTypeMenu)];
    [self.view addGestureRecognizer:tapGestureRec];
}

- (void)handleTap:(UITapGestureRecognizer *)sender
{
    // Code to process the MENU button is here.
}

我使用以下方式显示视图控制器pushViewController:animated:

UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:identifier];
[self pushViewController:controller animated:isAnimated];

我发现如果用户在屏幕开始出现时立即按下 MENU,而交叉淡入淡出过渡效果仍在显示,他们能够躲避 UITapGestureRecognizer 并返回到前一个屏幕,这不是故意的。他们也可能通过一遍又一遍地捣碎 MENU 来引发问题——最终他们会摆脱他们不应该做的事情。

如何确保 MENU 按下总是达到我的覆盖范围?有没有办法指定一个包含应用程序的 MENU 按钮处理程序并仍然使用 UINavigationController?

4

2 回答 2

3

一个对我有用的解决方案是将 UITapGestureRecognizer 安装到self.navigationController.view. 常规 UIViewControllers 错过的任何点击最终都会被导航控制器的识别器捕获。如果您在创建的每个视图控制器上安装 UITapGestureRecognizers,唯一会通过裂缝落入导航控制器的点击是在转换中间发生的点击,完全忽略这些点击是安全的。

请注意,当您希望 MENU 返回主屏幕时,您需要暂时删除此点击识别器并让 tvOS 自行处理点击,因为我在我的代码中找不到逃到主屏幕的干净方法(短exit(0))。

于 2016-02-05T01:21:40.767 回答
0

由于这种行为,我有非常相似的问题。在我的情况下,我有自定义焦点管理,具体取决于 MENU 按钮单击检测到pressesEnded并由preferredFocusedView. 但是,当导航控制器弹出 ViewController 时,此时用户再次单击 MENU 按钮,然后UINavigationController类检测pressesEnded然后调用preferredFocusedView我的目标 ViewController,我的管理在哪里,但pressesEnded不会被调用,因为被UINavigationController.

在我的情况下,此问题的解决方案是创建 UINavigationController 将使用的“虚拟”类,如下所示:

class MenuPhysicalHackNavigationController: UINavigationController {

    override func pressesBegan(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {

    }

    override func pressesEnded(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {

    }

    override func pressesCancelled(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {

    }

    override func pressesChanged(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {

    }

}
于 2016-02-23T13:01:41.490 回答