也许它已经被回答过了,但是我找不到它。
我正在创建随机的UIImageView
实例,并将它们作为子视图添加到viewController
的视图中。
以同样的方式创建UITapGestureRecognizer
实例并将其添加到UIImageView
实例中。
但是Tap无法使用。
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)
}
这是创建的 TapGesture 的实例:
<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>)>>
我不明白为什么会这样。请帮忙!
答案 0 :(得分:3)
除非将UIImageView的isUserInteractionEnabled
设置为true,否则无法触摸它。
除非使用.allowUserInteraction
动画选项,否则无法触摸动画视图。
此外,您必须继承并实施特殊的点击测试(因为动画视图实际上可能不在用户看到并尝试点击的位置):
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)
}