单击手势不处理以编程方式添加的子视图

时间:2016-06-27 08:03:29

标签: ios swift uiview uigesturerecognizer addsubview

背景: 我以前在视图上有一个触摸手势检测器,它工作正常。 然后我的应用程序增长了,现在我需要将相同的手势检测器添加到3个视图中。

问题: 当我将手势检测器添加到视图中时,手势检测器从未被调用,该视图已使用" .addSubview"

附加到父视图

守则:

    parentView.addSubview(waveview)
    parentView.addSubview(waveviewCap)
    parentView.addSubview(waveviewCapBG)

    let singleFingerDTap:UITapGestureRecognizer=UITapGestureRecognizer.init(target: self, action: "handleWaveviewTap")
    singleFingerDTap.numberOfTapsRequired = 1;

    self.waveview!.addGestureRecognizer(singleFingerDTap)
    self.waveviewCap!.addGestureRecognizer(singleFingerDTap)
    self.waveviewCapBG!.addGestureRecognizer(singleFingerDTap)

1 个答案:

答案 0 :(得分:0)

请在父视图中添加UIGestureRecognizerDelegate。并更新" shouldReceiveTouch"委托方法。

  func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {

    if touch.view == waveview || touch.view == waveviewCap || touch.view == waveviewCapBG {

        return true

    }
    return false
}

希望它的工作......