tapGesture没有被执行

时间:2017-08-29 17:35:05

标签: ios swift uitapgesturerecognizer

我在我的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")
    }

1 个答案:

答案 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)                
}