Search code examples
iosswiftuigesturerecognizervoiceoveruiaccessibility

Issues with custom UILongPressGestureRecognizer not Working with VoiceOver


I'm having issues getting a custom long press handler to work when VoiceOver is running.

In the custom UITableViewCell code below, I am adding a long press gesture recognizer with a UITableViewCell and calling handleVoiceOverLongPress() upon a long press.

// called when this custom UITableViewCell class is initialized
func initializeVoiceoverCellView() {
    NotificationCenter.default.addObserver(self,
                                           selector: #selector(voiceOverStatusChanged),
                                           name: UIAccessibility.voiceOverStatusDidChangeNotification,
                                           object: nil)
    if UIAccessibility.isVoiceOverRunning {
        addLongPressGestureRecognizer(withTarget: self, action: #selector(handleVoiceOverLongPress), duration: 1.0)
    }
}

@objc func voiceOverStatusChanged() {
    if UIAccessibility.isVoiceOverRunning {
        addLongPressGestureRecognizer(withTarget: self, action: #selector(handleVoiceOverLongPress), duration: 1.0)
    } else {
        removeLongPressGestureRecognizer()
    }
}

With the code above, handleVoiceOverLongPress() never gets called when long pressing while VoiceOver is on. I tried a single long press, and a


Solution

  • I'm having issues getting a custom long press handler to work when VoiceOver is running.

    Try the code hereafter to implement a long press gesture on you table view cell:

    //Gesture definition in your cell implementation for instance.
    var yourLongPressGesture: UILongPressGestureRecognizer
    yourLongPressGesture = UILongPressGestureRecognizer(target: self,
                                                        action: #selector(handleVoiceOverLongPress))
    yourLongPressGesture.minimumPressDuration = 0.5
    yourLongPressGesture.delaysTouchesBegan = true
    yourLongPressGesture.delegate = yourCell
    yourCell.addGestureRecognizer(yourLongPressGesture)
    
    
    //Define what the long press triggers in the following function.
    @objc func handleVoiceOverLongPress(gestureReconizer: UILongPressGestureRecognizer) {
        if gestureReconizer.state == UIGestureRecognizer.State.ended {
            print("LONG PRESS ENDED")
        }
        else {
            print("LONG PRESS IN PROGRESS...")
        }
    }
    

    Don't forget that a long press in the VoiceOver world isn't the same as the one when this feature isn't running: double tap and hold the pressure to get the same result, that's very important.

    Following this rationale with the code snippet above should help you make UILongPressGestureRecognizer work with VoiceOver.