VoiceOver Z gesture won't trigger when UIAlert

2019-03-01 14:56发布

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条回答
迷人小祖宗
2楼-- · 2019-03-01 15:01

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.

查看更多
登录 后发表回答