UIGesturerecognizer无法在UIView上运行

时间:2019-05-31 09:37:12

标签: ios swift uiview uigesturerecognizer

当我点击UIView但Gesture无法正常工作时。

override func viewDidLoad() {
        super.viewDidLoad()

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

        // Do any additional setup after loading the view.
    }


func handleTap(_sender: UITapGestureRecognizer) {
        print("---------View Tapped--------")
        viewForSHadow.isHidden = true
        viewForAlert.isHidden = true
    }

我只想在UIView点击上执行此操作。

6 个答案:

答案 0 :(得分:1)

您必须执行以下操作:

    class ViewController: UIViewController, UIGestureRecognizerDelegate {

...

    override func viewDidLoad() {
        super.viewDidLoad()
        let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap(_sender:)))
        viewForSHadow.isUserInteractionEnabled = true
        viewForSHadow.addGestureRecognizer(tap)
    }

    @objc func handleTap(_sender: UITapGestureRecognizer) {
        print("---------View Tapped--------")
        viewForSHadow.isHidden = true
        viewForAlert.isHidden = true
    }
...

}

答案 1 :(得分:1)

您的代码足以拥有可以正常工作的UIGestureRecognizer,您应该检查其他一些东西,例如,是否有其他东西可以消耗用户交互。还要检查您是否使用

isUserInteractionEnabled = false

到viewForSHadow的某些父视图。

答案 2 :(得分:1)

如果 alpha = 0 的任何内容与您的 viewForSHadow 重叠,则可以检查调试视图层次结构

答案 3 :(得分:0)

您需要提及numberOfTapsRequired的{​​{1}}属性。

UITapGestureRecognizer

还要确保您没有在代码中的任何地方执行override func viewDidLoad() { super.viewDidLoad() let tap = UITapGestureRecognizer(target: self, action: #selector(handleTapGesture(_sender:))) tap.numberOfTapsRequired = 1 viewForSHadow.isUserInteractionEnabled = true viewForSHadow.addGestureRecognizer(tap) } @objc func handleTapGesture(_sender: UITapGestureRecognizer) { print("---------View Tapped--------") // Why are you hiding this view **viewForSHadow** viewForSHadow.isHidden = true viewForAlert.isHidden = true }

答案 4 :(得分:0)

First of all you code doesn't compile. The handleTap(_:) signature must be like:

@objc func handleTap(_ sender: UITapGestureRecognizer) {
    print("---------View Tapped--------")
}

Secondly, you need to first try with the minimal code in a separate project. And by minimal code I mean what you've added in the question.

class ViewController: UIViewController {
    @IBOutlet weak var viewForSHadow: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
        let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
        viewForSHadow.addGestureRecognizer(tap)
    }

    @objc func handleTap(_ sender: UITapGestureRecognizer) {
        print("---------View Tapped--------")
    }
}

Try with just the above code and see if you can get it working. The above code is working well at my end without any delegate or numberOfTaps.

答案 5 :(得分:0)

您必须使用UIGestureRecognizerDelegate

let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleDismiss))
        tapRecognizer.delegate = self
        blackView.addGestureRecognizer(tapRecognizer)

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
    return true
}

@objc func handleDismiss() {
        print("handleDismiss")
}

Reference

不要忘记在您的课程中设置UIGestureRecognizerDelegate