I'm attaching a UITapGestureRecognizer to two different imageviews that will occupy the same space but at different times. Currently I'm using a flip transition that when an image is tapped it flips to the image on the other side like a card flipping over. I can get the first image to flip just fine but once flipped the second image doesn't respond at all when being tapped. I used a print("tapped")
to see if the second image is even getting the tap gesture but it isn't. This is the function I used to add the tap gestures:
func addTapGestures() {
jokerImageView.isUserInteractionEnabled = true
vendettaImageView.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: #selector(flipView(_:)))
jokerImageView.addGestureRecognizer(tap)
vendettaImageView.addGestureRecognizer(tap)
}
The UITapGestureRecognizer
is a distinct object. It can only be added to one element at a time.
So in your code, you add it to jokerImageView
and then immediately remove it and add it to vendettaImageView
.
You either need to create 2 instances of UITapGestureRecognizer
, or you need to add it to joker
after it has been "used" by vendetta
and vice-versa.
Edit: Instead of creating a 2nd gesture recognizer, you could try this:
func flipView(_ sender: Any) -> Void {
// view flip code goes here
// then
if let g = sender as? UIGestureRecognizer {
if g.view == vendettaImageView {
jokerImageView.addGestureRecognizer(g)
} else {
vendettaImageView.addGestureRecognizer(g)
}
}
}