I am trying to disable 1 of 3 gesture recognizers I have added to a view, does anyone know what the correct code is?
Here's what i have so far:
// the gesture recognizer i'm trying to disable
let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
myImageView1.addGestureRecognizer(tap)
// the code to disable it
let allGRs = [currentView.gestureRecognizers]
for g in allGRs {
if let g as? UITapGestureRecognizer {
g.isEnabled = false
}
}
Thankyou!
Hope this can help you!
Add unique name to your UITapGestureRecogniser
let tapName = "100" //Any unique name
func addGestureRecognisers() {
let view = UIView() // Your view in your context. I just added to test my code
let tap = UITapGestureRecognizer(target: self, action: action: #selector(self.handleTap(_:)))
tap.name = tapName
view.addGestureRecognizer(tap)
}
Filter UITapGestureRecognisers and then Filter the result by name.
func disableTapGesture(from view: UIView) {
view.gestureRecognizers?.filter({$0.name == tapName}).first?.isEnabled = false
}
You should assign the name property to your gesture recognizer.
tap.name = "myTapGesture"
And later you can cycle through the recozniers and only disable the one that you want.
for aRecognizer in view.gestureRecognizers {
if let name = aRecognizer.name {
if name == "myTapGesture" {
aRecognizer.isEnabled = false
}
}
}