如何禁用多个手势识别器之一?

时间:2019-02-04 01:53:41

标签: ios swift uigesturerecognizer

我正在尝试禁用添加到视图中的3个手势识别器中的1个,有人知道正确的代码是什么吗?

这是我到目前为止的内容:

// the gesture recognizer i'm trying to disable

    let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
    myImageView1.addGestureRecognizer(tap)


// the code to disable it

    let allGRs = [currentView.gestureRecognizers]

    for g in allGRs {

        if let g as? UITapGestureRecognizer {
            g.isEnabled = false
        }

    }

谢谢!

2 个答案:

答案 0 :(得分:0)

希望这可以为您提供帮助!

  1. 为您的UITapGestureRecogniser添加唯一名称

    let tapName = "100" //Any unique name
    func addGestureRecognisers() {
        let view = UIView() // Your view in your context. I just added to test my code
        let tap = UITapGestureRecognizer(target: self, action: action: #selector(self.handleTap(_:)))
        tap.name = tapName 
        view.addGestureRecognizer(tap)
    }
    
  2. 过滤UITapGestureRecognisers,然后按name过滤结果。

    func disableTapGesture(from view: UIView) {
        view.gestureRecognizers?.filter({$0.name == tapName}).first?.isEnabled = false
    }
    

答案 1 :(得分:0)

您应该将name属性分配给手势识别器。

tap.name = "myTapGesture"

然后您就可以循环使用雷佐尼,并且只禁用您想要的雷佐尼。

for aRecognizer in view.gestureRecognizers {
   if let name = aRecognizer.name {
       if name == "myTapGesture" {
           aRecognizer.isEnabled = false
       }
   }
}