我正面临着我的应用程序的奇怪问题。实际上,当我呈现视频控制器播放视频时。在视频加载时,用户按下菜单按钮,应用程序转到后台。虽然我已经覆盖菜单按钮操作。
这是我的代码。
override func viewWillAppear(animated: Bool) {
let menuPressRecognizer = UITapGestureRecognizer()
menuPressRecognizer.addTarget(self, action: #selector(VideoPlayerViewController.menuButtonAction(_:)))
menuPressRecognizer.allowedPressTypes = [NSNumber(integer: UIPressType.Menu.hashValue)]
self.playerController.view.addGestureRecognizer(menuPressRecognizer)
}
func menuButtonAction(ges:UITapGestureRecognizer) {
self.dismissView()
}
答案 0 :(得分:6)
这是我的代码并为我工作。
Swift 3
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let menuPressRecognizer = UITapGestureRecognizer()
menuPressRecognizer.addTarget(self, action: #selector(YourViewController.menuButtonAction(recognizer:)))
menuPressRecognizer.allowedPressTypes = [NSNumber(value: UIPressType.menu.rawValue)]
self.view.addGestureRecognizer(menuPressRecognizer)
}
func menuButtonAction(recognizer:UITapGestureRecognizer) {
self.dismiss(animated: true, completion: nil)
}
Swift 4
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let menuPressRecognizer = UITapGestureRecognizer()
menuPressRecognizer.addTarget(self, action: #selector(YourViewController.menuButtonAction(recognizer:)))
menuPressRecognizer.allowedPressTypes = [NSNumber(value: UIPressType.menu.rawValue)]
self.view.addGestureRecognizer(menuPressRecognizer)
}
@objc func menuButtonAction(recognizer:UITapGestureRecognizer) {
self.dismiss(animated: true, completion: nil)
}
Swift 4.2& 5 强>
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let menuPressRecognizer = UITapGestureRecognizer()
menuPressRecognizer.addTarget(self, action: #selector(YourViewController.menuButtonAction(recognizer:)))
menuPressRecognizer.allowedPressTypes = [NSNumber(value: UIPress.PressType.menu.rawValue)]
self.view.addGestureRecognizer(menuPressRecognizer)
}
@objc func menuButtonAction(recognizer:UITapGestureRecognizer) {
self.dismiss(animated: true, completion: nil)
}
答案 1 :(得分:4)
指定rawValue
时,您应该使用枚举hash
代替allowedPressTypes
:
menuPressRecognizer = [NSNumber(value: UIPressType.menu.rawValue)]
答案 2 :(得分:0)
当您要添加多个值时,可以执行以下操作:
let pressTypes: [NSNumber] = [UIPressType.select, UIPressType.playPause, UIPressType.rightArrow].map{ NSNumber(value: $0.rawValue)}
答案 3 :(得分:0)
这也可以在IB中完成。 TapGestureRecognizer上有一个按钮选项,其中包括菜单按钮。
答案 4 :(得分:0)
为获得更详细的信息,您还可以使用以下方法:
- (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);
如果您按照以下Apple的建议选择使用任何一种方法,请确保覆盖所有这些方法:
通常,所有进行自定义新闻处理的响应者都应 覆盖所有这四种方法。
您的响应者将收到pressesEnded:withEvent或 对于每个pressesCancelled:withEvent:
正在处理的新闻(收到的新闻 pressesBegan:withEvent:)。
pressesChanged:withEvent:将为提供以下内容的印刷机调用 模拟值(如指尖或模拟按钮)
***您必须处理取消的印刷机,以确保应用程序中的正确行为。否则很可能导致错误 行为或崩溃。
答案 5 :(得分:0)
斯威夫特 5:
private func setupRemoteGesture() {
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleMenuPress))
tapRecognizer.allowedPressTypes = [NSNumber(value: UIPress.PressType.menu.rawValue)]
view.addGestureRecognizer(tapRecognizer)
}
@objc private func handleMenuPress() {
// Menu button pressed
}