我在我的UIImageView rDot中添加了一个tapGesture。问题是当我点击rDot没有任何事情发生时它没有打印"它工作"。 rDot与其他元素在堆栈视图中,我不知道它是否与它无关。 rDot和redDot是两个独立的UIImageViews。
override func viewDidLoad() {
super.viewDidLoad()
rDot.isUserInteractionEnabled = true
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(rdotimageTapped(tapGestureRecognizer:)))
rDot.addGestureRecognizer(tapGestureRecognizer)
}
func rdotimageTapped(tapGestureRecognizer: UITapGestureRecognizer) {
whiteD?.isHidden = true
redDot?.isHidden = false
print("It worked")
}
答案 0 :(得分:1)
您需要将subView
置于前面,因为您已将imageView
置于stackView
之内。有时,视图层次结构无法识别被点击的subviews
。
override func viewDidLoad() {
super.viewDidLoad()
rDot.isUserInteractionEnabled = true
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(rdotimageTapped(tapGestureRecognizer:)))
rDot.addGestureRecognizer(tapGestureRecognizer)
view.bringSubview(toFront: rDot)
}