H.N. H.N. - 1 month ago 18
iOS Question

Disable a longpress

How can I disable a longpress?

I have set a longpress in a viewcontroller and it's working ok but i'd like it to stop working after I press another button.

I can add a flag and set it to false after I press button B and than the long press stops workingm like this:

func longpress(gestureRecognizer: UIGestureRecognizer) {
if flag = true {
// action
}
}


But I think it's not the right way. SO, What's the right way to do this?

Answer

You need to look at the superclass of UILongPressGestureRecognizer, UIGestureRecognizer. It has a property isEnabled that can be used to turn off recognition and turn it back on again.