tvOS - pressEnded在启动时不起作用

时间:2016-02-17 17:09:41

标签: swift tvos apple-tv

我正在尝试为应用提供与YouTuBe相同的行为,当顶部TabBar可见且用户按下遥控器上的菜单按钮时,应用应该退出。

所以我覆盖了pressesBeganpressesEnded

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

}

没有任何内容,否则无论条件是什么,该应用都会退出 pressesEnded

override func pressesEnded(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
    if let type = presses.first?.type where type == .Menu {
        if navigationController.viewControllers.count == 1 {
            if isTabBarOpen {
                super.pressesEnded(presses, withEvent: event)
            } else {
                self.showTabBar()
                self.setNeedsFocusUpdate()
                self.updateFocusIfNeeded()
            }
        }
    } else {
        super.pressesEnded(presses, withEvent: event)
    }
}

所以我的问题是在应用程序启动时(从已终止状态,而不是后台状态),如果我按下遥控器上的菜单按钮,if isTabBarOpen{ super.pressesEnded(presses, withEvent: event) }会被执行但应用程序永远不会退出,除非我移动焦点。

修改

我首先关注TabBarRootViewController

如果您需要更多信息,请告诉我:)谢谢

1 个答案:

答案 0 :(得分:1)

您是否尝试使用手势识别器来捕捉菜单按钮?

    // add the hook to detect menu button press
    let tap = UITapGestureRecognizer(target: self, action: "menuPressed:")
    tap.allowedPressTypes = [NSNumber(integer: UIPressType.Menu.rawValue)]
    window.rootViewController?.view.addGestureRecognizer(tap) // add it to your view controller

    func menuPressed(gesture: UITapGestureRecognizer){
       print("menu press")   
    }