Reputation:
May be it has been answered before but, I was unable to find it.
I am creating random UIImageView
instances and adding them as subViews to the view of a viewController
.
In the same way I am creating UITapGestureRecognizer
instances and adding them to the UIImageView
instances.
But the Tap doesn't work.
for _ in 0..<20 {
let iv = UIImageView()
iv.isUserInteractionEnabled = true
iv.frame.origin = CGPoint(x: randomXGenerator(), y: randomYGenerator())
iv.frame.size.width = randomGenerator()
iv.frame.size.height = iv.frame.width
iv.tintColor = UIColor.random() //To color the image inside the imageView
iv.image = bubble
view.addSubview(iv)
animateBubblesRandom(bubble: iv)
let tap = UITapGestureRecognizer(target: self, action: #selector(tappedOnBubble(object:)))
iv.addGestureRecognizer(tap)
}
And here are the instances of TapGesture created:
<UITapGestureRecognizer: 0x6000012b5b00; state = Possible; view = <UIImageView 0x7fdf4e507660>; target= <(action=tappedOnBubbleWithObject:, target=<FloatingBubbles.ViewController 0x7fdf4e605440>)>>
<UITapGestureRecognizer: 0x6000012bed00; state = Possible; view = <UIImageView 0x7fdf4e70b0b0>; target= <(action=tappedOnBubbleWithObject:, target=<FloatingBubbles.ViewController 0x7fdf4e605440>)>>
<UITapGestureRecognizer: 0x6000012bf000; state = Possible; view = <UIImageView 0x7fdf4e6041f0>; target= <(action=tappedOnBubbleWithObject:, target=<FloatingBubbles.ViewController 0x7fdf4e605440>)>>
<UITapGestureRecognizer: 0x6000012bf100; state = Possible; view = <UIImageView 0x7fdf4e605fa0>; target= <(action=tappedOnBubbleWithObject:, target=<FloatingBubbles.ViewController 0x7fdf4e605440>)>>
<UITapGestureRecognizer: 0x6000012bf200; state = Possible; view = <UIImageView 0x7fdf4e6079c0>; target= <(action=tappedOnBubbleWithObject:, target=<FloatingBubbles.ViewController 0x7fdf4e605440>)>>
<UITapGestureRecognizer: 0x6000012bf300; state = Possible; view = <UIImageView 0x7fdf4e607d20>; target= <(action=tappedOnBubbleWithObject:, target=<FloatingBubbles.ViewController 0x7fdf4e605440>)>>
<UITapGestureRecognizer: 0x6000012bf500; state = Possible; view = <UIImageView 0x7fdf4e608080>; target= <(action=tappedOnBubbleWithObject:, target=<FloatingBubbles.ViewController 0x7fdf4e605440>)>>
<UITapGestureRecognizer: 0x6000012bf600; state = Possible; view = <UIImageView 0x7fdf4e6083e0>; target= <(action=tappedOnBubbleWithObject:, target=<FloatingBubbles.ViewController 0x7fdf4e605440>)>>
<UITapGestureRecognizer: 0x6000012bf700; state = Possible; view = <UIImageView 0x7fdf4e414520>; target= <(action=tappedOnBubbleWithObject:, target=<FloatingBubbles.ViewController 0x7fdf4e605440>)>>
<UITapGestureRecognizer: 0x6000012b5f00; state = Possible; view = <UIImageView 0x7fdf4e414750>; target= <(action=tappedOnBubbleWithObject:, target=<FloatingBubbles.ViewController 0x7fdf4e605440>)>>
<UITapGestureRecognizer: 0x6000012b6000; state = Possible; view = <UIImageView 0x7fdf4e70b2e0>; target= <(action=tappedOnBubbleWithObject:, target=<FloatingBubbles.ViewController 0x7fdf4e605440>)>>
<UITapGestureRecognizer: 0x6000012bef00; state = Possible; view = <UIImageView 0x7fdf4e608b40>; target= <(action=tappedOnBubbleWithObject:, target=<FloatingBubbles.ViewController 0x7fdf4e605440>)>>
<UITapGestureRecognizer: 0x6000012bee00; state = Possible; view = <UIImageView 0x7fdf4e608ea0>; target= <(action=tappedOnBubbleWithObject:, target=<FloatingBubbles.ViewController 0x7fdf4e605440>)>>
<UITapGestureRecognizer: 0x6000012bf800; state = Possible; view = <UIImageView 0x7fdf4e609200>; target= <(action=tappedOnBubbleWithObject:, target=<FloatingBubbles.ViewController 0x7fdf4e605440>)>>
<UITapGestureRecognizer: 0x6000012bf900; state = Possible; view = <UIImageView 0x7fdf4e6096b0>; target= <(action=tappedOnBubbleWithObject:, target=<FloatingBubbles.ViewController 0x7fdf4e605440>)>>
<UITapGestureRecognizer: 0x6000012bfa00; state = Possible; view = <UIImageView 0x7fdf4e609a10>; target= <(action=tappedOnBubbleWithObject:, target=<FloatingBubbles.ViewController 0x7fdf4e605440>)>>
<UITapGestureRecognizer: 0x6000012bfb00; state = Possible; view = <UIImageView 0x7fdf4e609d70>; target= <(action=tappedOnBubbleWithObject:, target=<FloatingBubbles.ViewController 0x7fdf4e605440>)>>
<UITapGestureRecognizer: 0x6000012bfc00; state = Possible; view = <UIImageView 0x7fdf4e60a0d0>; target= <(action=tappedOnBubbleWithObject:, target=<FloatingBubbles.ViewController 0x7fdf4e605440>)>>
<UITapGestureRecognizer: 0x6000012bf400; state = Possible; view = <UIImageView 0x7fdf4e60a630>; target= <(action=tappedOnBubbleWithObject:, target=<FloatingBubbles.ViewController 0x7fdf4e605440>)>>
<UITapGestureRecognizer: 0x6000012bfd00; state = Possible; view = <UIImageView 0x7fdf4e60a990>; target= <(action=tappedOnBubbleWithObject:, target=<FloatingBubbles.ViewController 0x7fdf4e605440>)>>
I do not understand why this is happening. Please help!
Upvotes: 1
Views: 72
Reputation: 535566
A UIImageView cannot be touched unless you set its isUserInteractionEnabled
to true.
An animated view cannot be touched unless you use the .allowUserInteraction
animation option.
Plus you must subclass and implement special hit testing (because an animated view may not actually be located at the place the user sees it and tries to click):
override func hitTest(_ point: CGPoint, with e: UIEvent?) -> UIView? {
let pres = self.layer.presentation()!
let suppt = self.convert(point, to: self.superview!)
let prespt = self.superview!.layer.convert(suppt, to: pres)
return super.hitTest(prespt, with: e)
}
Upvotes: 3