我试图为应用程序提供与 YouTuBe 相同的行为,其中当顶部 TabBar
可见且用户按下 Remote 上的菜单按钮时,应用程序应退出。
所以我重写了 pressesBegan
和 pressesEnded
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)
}
}
所以我的问题是应用程序启动(从终止状态,而不是后台状态),如果我按下 Remote 上的菜单按钮,if isTabBarOpen{ super.pressesEnded(presses, withEvent: event) }
被执行,但应用程序永远不会退出,除非我移动焦点。
编辑
我尝试将初始焦点集中在 TabBar
或 RootViewController
如果您需要更多信息,请告诉我:) 谢谢
最佳答案
您是否尝试过使用手势识别器来捕捉菜单按钮按下的情况?
// 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")
}
关于swift - tvOS - pressesEnded 在启动时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35463221/