I want use a custom touch event in a view. There is a web view which is the subview of this view. I override touchBegan and other functions but it does not run.
Asked
Active
Viewed 4,286 times
1 Answers
5
If you want to call a function while tapping a view you can use 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
}
For 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
}

Joseph Beuys' Mum
- 2,395
- 2
- 24
- 50

mrBallista
- 548
- 5
- 8
-
I have known that. I mean there is another webview in this and cover it. The view cannot receive the event or gesture.@mrBallista – Vonfry Feb 11 '15 at 12:00
-
have you tried to disable webview interection webView.userInteractionEnabled = false – mrBallista Feb 11 '15 at 12:35
-
but I can not operate the webview. I want to using both of the. – Vonfry Feb 11 '15 at 12:44
-
ok , maybe we need to see some code to look what you actually want to do – mrBallista Feb 11 '15 at 13:05
-
I have done with it. The gesture can be used. Sorry. – Vonfry Feb 11 '15 at 13:07