问题
I'm trying to figure out how to intercept a click event on a UIWebView. It seems like the correct gesture is UITapGestureRecognizer, but I can't seem to get the view right. I haven't been able to find a solution in swift. My hierarchy is View -> UIWebView. My use case is to deploy a web app and have some custom actions based on the web view navigation. For example, being able to recognize navigation events in which I'd want to serve an interstitial ad or display a UINavigationBar. Thanks for any help!
import UIKit
class ViewController: UIViewController {
@IBOutlet var Webview: UIWebView!
let URLPath = "http://www.example.com"
func loadAddressURL(){
let requestURL = NSURL(string: URLPath)
let request = NSURLRequest(URL: requestURL!)
Webview.loadRequest(request)
}
override func viewDidLoad() {
super.viewDidLoad()
loadAddressURL()
NSLog("I'm getting going")
//NOT SURE IF YOU SHOULD DISABLE THIS OR NOT
//Webview.userInteractionEnabled=false
let tapRecognizer = UITapGestureRecognizer(target: self , action: "handleSingleTap:")
NSLog("I'm past the tab recognizer!\(tapRecognizer)")
tapRecognizer.numberOfTapsRequired = 1
Webview.addGestureRecognizer(tapRecognizer)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func handleSingleTap(recognizer: UITapGestureRecognizer) {
//
NSLog("I made it here")
}
}
回答1:
first set Gesture Delegate and inherited geture UIGestureRecognizerDelegate
tapRecognizer.delegate = self
and Insert this delegate
func gestureRecognizer(UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer:UIGestureRecognizer) -> Bool
{
return true
}
Try this maybe help-full...
来源:https://stackoverflow.com/questions/31107656/intercept-uitapgesturerecognizer-for-uiwebview