UIGesturerecognizer 不适用于 UIView

UIGesturerecognizer is not working on UIView

当我点击 UIView 但手势不起作用时。

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 时执行此操作。

您必须执行如下操作:

    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
    }
...

}

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

isUserInteractionEnabled = false

到 viewForSHadow 的一些父视图。

您需要提及 numberOfTapsRequired 属性 的 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
}

此外,请确保您没有在代码中的任何地方执行 viewForSHadow.isUserInteractionEnabled = false

如果 alpha = 0 与您的 viewForSHadow[= 重叠,您可以检查 调试视图层次结构 15=].

首先你的代码无法编译。 handleTap(_:) 签名必须类似于:

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

其次,您需要先在单独的项目中尝试使用最少的代码。我所说的最少代码是指您在问题中添加的内容。

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--------")
    }
}

尝试使用上面的代码,看看是否可以正常工作。上面的代码在我这边运行良好,没有任何 delegatenumberOfTaps.

你必须使用 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

不要忘记在 class

中设置 UIGestureRecognizerDelegate