6

I am facing the strange problem with my application. Actually when i am presenting a view controller for play the video. At the video load time user press the menu button the application goes to background. While i have overwrite the Menu Button Action.

This is my code.

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()
 }
Martin Delille
  • 9,399
  • 11
  • 55
  • 118
Sankalap Yaduraj Singh
  • 1,007
  • 1
  • 12
  • 30

5 Answers5

17

This is my code and working for me.

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)
}
Roman Podymov
  • 3,416
  • 3
  • 28
  • 49
anas.p
  • 2,028
  • 15
  • 25
  • Keep in mind that the UITapGestureRecognizer solution is more ideal than overriding the presses withEvent methods if you're also overriding shouldUpdateFocus and preferredFocusEnvironments. Because UITapGestureRecognizer doesn't cancel the other focus events, where as the presses overrides do. – PostCodeism Feb 19 '21 at 23:05
5

You should use enum's rawValue instead of hash when you specify allowedPressTypes:

menuPressRecognizer = [NSNumber(value: UIPressType.menu.rawValue)]

Michał Ciuba
  • 7,611
  • 2
  • 29
  • 56
0

When you want to multiple add multiple values you can do simple:

let pressTypes: [NSNumber] = [UIPressType.select, UIPressType.playPause, UIPressType.rightArrow].map{ NSNumber(value: $0.rawValue)}
Ferenc Kiss
  • 870
  • 11
  • 12
0

This can also be done in IB. There's an option on TapGestureRecognizer for buttons which includes the menu button.

Joe Susnick
  • 5,298
  • 4
  • 35
  • 47
0

For more granularity, you can also make use of the following methods:

- (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);

Please make sure to override all of these methods if you choose to use any of them as per Apple's suggestions given below:

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.

atulkhatri
  • 9,359
  • 3
  • 45
  • 79