There, I wanted to do something quickly, but I could not figure out how to achieve it, that is, remove gesture recognizers based on the type of class, here is my code (and example), I use swift 2.0 in Xcode 7 beta 5:
I have 3 classes that inherit from UITapGestureRecognizer
class GestureONE: UIGestureRecognizer { } class GestureTWO: UIGestureRecognizer { } class GestureTHREE: UIGestureRecognizer { }
Add them to the view.
var gesture1 = GestureONE() var gesture11 = GestureONE() var gesture2 = GestureTWO() var gesture22 = GestureTWO() var gesture222 = GestureTWO() var gesture3 = GestureTHREE() var myView = UIView() myView.addGestureRecognizer(gesture1) myView.addGestureRecognizer(gesture11) myView.addGestureRecognizer(gesture2) myView.addGestureRecognizer(gesture22) myView.addGestureRecognizer(gesture222) myView.addGestureRecognizer(gesture3)
I am printing an object:
print(myView.gestureRecognizers!) // playground prints "[<__lldb_expr_224.TapONE: 0x7fab52c20b40; baseClass = UITapGestureRecognizer; state = Possible; view = <UIView 0x7fab52d259c0>>, <__lldb_expr_224.TapONE: 0x7fab52d21250; baseClass = UITapGestureRecognizer; state = Possible; view = <UIView 0x7fab52d259c0>>, <__lldb_expr_224.TapTWO: 0x7fab52d24a60; baseClass = UITapGestureRecognizer; state = Possible; view = <UIView 0x7fab52d259c0>>, <__lldb_expr_224.TapTWO: 0x7fab52c21130; baseClass = UITapGestureRecognizer; state = Possible; view = <UIView 0x7fab52d259c0>>, <__lldb_expr_224.TapTWO: 0x7fab52e13260; baseClass = UITapGestureRecognizer; state = Possible; view = <UIView 0x7fab52d259c0>>, <__lldb_expr_224.TapTHREE: 0x7fab52c21410; baseClass = UITapGestureRecognizer; state = Possible; view = <UIView 0x7fab52d259c0>>]"
This extension, which I made using a generic function
extension UIView { func removeGestureRecognizers<T: UIGestureRecognizer>(type: T.Type) { if let gestures = self.gestureRecognizers { for gesture in gestures { if gesture is T { removeGestureRecognizer(gesture) } } } } }
Then i use it
myView.gestureRecognizers?.count
Deletes all gestures D:
And here is an experiment with custom classes
//** TEST WITH ANIMALS*// class Animal { /*...*/ } class Dog: Animal { /*...*/ } class Cat: Animal { /*...*/ } class Hipo: Animal { /*...*/ } class Zoo { var animals = [Animal]() } var zoo = Zoo() var dog1 = Dog() var cat1 = Cat() var cat2 = Cat() var cat3 = Cat() var hipo1 = Hipo() var hipo2 = Hipo() zoo.animals.append(dog1) zoo.animals.append(cat1) zoo.animals.append(cat2) zoo.animals.append(cat3) zoo.animals.append(hipo1) zoo.animals.append(hipo2) print(zoo.animals) //playground prints "[Dog, Cat, Cat, Cat, Hipo, Hipo]" extension Zoo { func removeAnimalType<T: Animal>(type: T.Type) { for (index, animal) in animals.enumerate() { if animal is T { animals.removeAtIndex(index) } } } } zoo.animals.count // prints 6 zoo.removeAnimalType(Cat) zoo.animals.count // prints 3
It actually removes the classes that it should: D
What am I missing in UIGestureRecognizer? I ended up with a workaround creating a function that has no generics (boring), such as:
extension UIView { func removeActionsTapGestureRecognizer() { if let gestures = self.gestureRecognizers { gestures.map({ if $0 is ActionsTapGestureRecognizer { self.removeGestureRecognizer($0) } }) } } }
It works, of course, but still I would like to have a real solution
I appreciate your help !!
Note: The first question I ask here