具有以下视图结构:
以编程方式将点击手势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)
也许他们是这个原因?
答案 0 :(得分:4)
找到解决方案:
包含我的lbl的视图在故事板中未选中userInteractionEnabled
。
因此,在向标签,图片或简单添加按钮添加手势识别器时,请始终检查所有父视图是否都有userInteractionEnabled
到true
。