UIWebView 和触摸事件

UIWebView and touch event

我想在视图中使用自定义触摸事件。有一个 web 视图是该视图的子视图。 我重写了 touchBegan 和其他函数,但它没有 运行.

如果您想在点击视图时调用函数,您可以使用 UITapGestureRecognizer

override func viewDidLoad() {
    super.viewDidLoad()
    let tapRecognizer = UITapGestureRecognizer(target: view, action: "handleSingleTap:")
    tapRecognizer.numberOfTapsRequired = 1
    self.view.addGestureRecognizer(tapRecognizer)
}

func handleSingleTap(recognizer: UITapGestureRecognizer) {
    //Do something here with the gesture
}    

对于Swift 3:

override func viewDidLoad() {
    super.viewDidLoad()
    let tapRecognizer = UITapGestureRecognizer(target: view, action: #selector(handleSingleTap))
    tapRecognizer.numberOfTapsRequired = 1
    self.view.addGestureRecognizer(tapRecognizer)
}

@objc func handleSingleTap(recognizer: UITapGestureRecognizer) {
    //Do something here with the gesture
}