Vonfry
Vonfry

Reputation: 365

UIWebView and touch event

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.

Upvotes: 0

Views: 4327

Answers (1)

mrBallista
mrBallista

Reputation: 548

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
}

Upvotes: 5

Related Questions