拦截 UIWebView 的 UITapGestureRecognizer [英] Intercept UITapGestureRecognizer for UIWebView

查看:26
本文介绍了拦截 UIWebView 的 UITapGestureRecognizer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚如何拦截 UIWebView 上的点击事件.看起来正确的手势是 UITapGestureRecognizer,但我似乎无法正确看待.我一直无法快速找到解决方案.我的层次结构是 View ->UIWebView.我的用例是部署一个 Web 应用程序并根据 Web 视图导航进行一些自定义操作.例如,能够识别我想要投放插页式广告或显示 UINavigationBar 的导航事件.感谢您的帮助!

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")
   }   

}

推荐答案

先设置 Gesture Delegate 并继承 geture UIGestureRecognizerDelegate

first set Gesture Delegate and inherited geture UIGestureRecognizerDelegate

tapRecognizer.delegate = self

并插入这个委托

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

试试这个可能有帮助...

Try this maybe help-full...

这篇关于拦截 UIWebView 的 UITapGestureRecognizer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆