-->

VoiceOver Z gesture won't trigger when UIAlert

2019-03-01 14:27发布

问题:

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...

回答1:

To get the desired result on your alert view thanks to the scrub gesture, you could 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.