There's something I wanted to do in swift, but I couldn't figure out how to achieve it, that is to remove gesture recognisers given a Class Type, here's my code (and example), i'm using swift 2.0 in Xcode 7 beta 5:
I have 3 classes that inherits from UITapGestureRecognizer
class GestureONE: UIGestureRecognizer { /*...*/ }
class GestureTWO: UIGestureRecognizer { /*...*/ }
class GestureTHREE: UIGestureRecognizer { /*...*/ }
Add them to a 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 print the 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>>]"
Have this extension I made with 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 // Prints 6
myView.removeGestureRecognizers(GestureTWO)
myView.gestureRecognizers?.count // Prints 0
Is removing all of the gestures D:
And here's 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's actually removing the classes it should :D
What am I missing with the UIGestureRecognizer's? I ended up with a workaround making a function that has no generics (boring) like this:
extension UIView {
func removeActionsTapGestureRecognizer() {
if let gestures = self.gestureRecognizers {
gestures.map({
if $0 is ActionsTapGestureRecognizer {
self.removeGestureRecognizer($0)
}
})
}
}
}
This works of course, but still I would like to have a real solution
I appreciate your help!!
Note: First question I ask here