截取UIWebView的

时间:2015-06-29 04:11:18

标签: ios swift uiwebview

我正在试图找出如何拦截UIWebView上的点击事件。似乎正确的手势是UITapGestureRecognizer,但我似乎无法正确查看。我无法在swift中找到解决方案。我的层次结构是View -> UIWebView。我的用例是部署Web应用程序并根据Web视图导航进行一些自定义操作。例如,能够识别我想要投放插页式广告或显示UINavigationBar的导航事件。谢谢你的帮助!

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 个答案:

答案 0 :(得分:11)

首先设置Gesture Delegate并继承geture UIGestureRecognizerDelegate

tapRecognizer.delegate = self

并插入此委托

func gestureRecognizer(UIGestureRecognizer,  shouldRecognizeSimultaneouslyWithGestureRecognizer:UIGestureRecognizer) -> Bool 
{
    return true
}

尝试这可能是帮助 - 完整......