我想在视图中使用自定义触摸事件。有一个web视图是此视图的子视图。
我重写touchBegind和其他函数,但它不运行。
最佳答案
如果要在轻触视图时调用函数,可以使用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
}