Intercept UITapGestureRecognizer for UIWebView

狂风中的少年 提交于 2019-12-11 13:36:36

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!