点击手势不起作用。已启用用户互动。

时间:2017-04-16 01:31:51

标签: swift uigesturerecognizer

具有以下视图结构:

enter image description here

以编程方式将点击手势rec添加到Temp lbl:

    let tempLblTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(MainFeedVC.convertDegrees))
    tempLblTap.delegate = self
    tempLblTap.numberOfTapsRequired = 1
    tempLblTap.numberOfTouchesRequired = 1
    tempLblTap.cancelsTouchesInView = false
    self.tempLbl.isUserInteractionEnabled = true
    self.tempLbl.addGestureRecognizer(tempLblTap)

但未触发方法convertDegrees

还有2个滑动手势识别器添加到同一视图中:

let leftSwipeGestureRecognizer: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(MainFeedVC.showPostPicVC))
    leftSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirection.left
    self.view.addGestureRecognizer(leftSwipeGestureRecognizer)

    let rightSwipeGestureRecognizer: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(MainFeedVC.showUserVC))
    rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirection.right
    self.view.addGestureRecognizer(rightSwipeGestureRecognizer)

也许他们是这个原因?

1 个答案:

答案 0 :(得分:4)

找到解决方案:

包含我的lbl的视图在故事板中未选中userInteractionEnabled。 因此,在向标签,图片或简单添加按钮添加手势识别器时,请始终检查所有父视图是否都有userInteractionEnabledtrue