2

I'm trying to use the Z gesture to dismiss a UIAlertController. I have a very simple app. It has a single view with 1 button. Tapping the button presents an alert. I have implemented

- (BOOL)accessibilityPerformEscape {
    NSLog(@"Z gesture");
    return YES;
}

With VoiceOver on, scrubbing the screen prints out "Z gesture," but when I press the button and the alert is visible, scrubbing the screen does nothing, the method is not called and nothing is printed. What do I have to do to get this to function while the alert is on screen?

Thanks...

XLE_22
  • 5,124
  • 3
  • 21
  • 72
SeanT
  • 1,741
  • 1
  • 16
  • 24
  • This [GitHub thread](https://github.com/material-components/material-components-ios/issues/3661#issuecomment-404301151) may help. Looks like you need the accessibility delegate method to be implemented in the custom view controller itself—in this case presumably the UIAlertController instance. – Nick K9 Aug 15 '18 at 13:07

1 Answers1

3

To get the desired result on your alert view thanks to the scrub gesture, override accessibilityPerformEscape() in the alert view itself.

A solution could be to implement this override in an UIView extension as follows :

extension UIView {

override open func accessibilityPerformEscape() -> Bool {

    if let myViewController = self.findMyViewController() as? UIAlertController {

        myViewController.dismiss(animated: true,
                                 completion: nil)
    }
    return true
}


private func findMyViewController() -> UIViewController? {

    if let nextResponder = self.next as? UIViewController {
        return nextResponder
    } else if let nextResponder = self.next as? UIView {
        return nextResponder.findMyViewController()
    } else {
        return nil
    }
}

}

The code is short enough to be understood without further explanation. If it's not clear, don't hesitate to ask.

The function findMyViewController has been found here.

XLE_22
  • 5,124
  • 3
  • 21
  • 72
  • Is there no way to attach the gesture handler to an alert without modifying UIView like that? – Grant Neufeld Jan 10 '20 at 03:22
  • @GrantNeufeld: this is just an example of implementation I found interesting. I think you can override the 'accessibilityPerformEscape' in a subclass of 'UIAlertController' you create as well. – XLE_22 Jan 10 '20 at 07:49
  • It is interesting that this needs to be overridden for a standard `UIAlertController`. Apple's documentation says: 'Dismiss an alert or return to the previous screen: Two-finger scrub (move two fingers back and forth three times quickly, making a “z”)' But they neglect to provide a default implementation? https://support.apple.com/en-kz/guide/iphone/iph3e2e2281/ios – slythfox Nov 08 '21 at 19:26