菜鸟教程小白 发表于 2022-12-13 00:03:09

ios - UIWebView 上的双击识别 - Swift 3


                                            <p><p>我正在尝试通过 UIWebView 实现手势识别器来检测双击。但是没有认可。 </p>

<p>我在网上查看了一些教程,我可以编译成这个。代码如下。但是没有成功。</p>

<pre><code>class PageContentViewController: UIViewController, UIGestureRecognizerDelegate, UIWebViewDelegate{

@IBOutlet weak var webView: UIWebView!

var pageIndex: Int = 0
var strTitle: String!
var flag : Int = 0

override func viewDidLoad() {
    super.viewDidLoad()
    webView.delegate = self

    DispatchQueue.main.async {
    let req = URLRequest(url: URL(fileURLWithPath: Bundle.main.path(forResource: self.strTitle+&#34;/index&#34; , ofType: &#34;html&#34;)!))
    self.webView.loadRequest(req)
    self.view.addSubview(self.webView)

    }

    let tapGestureRecognizer = UITapGestureRecognizer(target:self, action:#selector(self.goToback))
    tapGestureRecognizer.delegate? = self
    tapGestureRecognizer.numberOfTapsRequired = 2
    webView.isUserInteractionEnabled = true
    webView.addGestureRecognizer(tapGestureRecognizer)
    webView.scrollView.addGestureRecognizer(tapGestureRecognizer)
    webView.gestureRecognizerShouldBegin(tapGestureRecognizer)
}


func gestureRecognizer(_: UIGestureRecognizer,shouldRecognizeSimultaneouslyWith:UIGestureRecognizer) -&gt; Bool {
    return true
}
func gestureRecognizer(_: UIGestureRecognizer, shouldReceive:UITouch) -&gt; Bool {
    return true
}


func goToback(){
    print(&#34;On the Back&#34;)
}

}
</code></pre>

<p> <a href="/image/pTouz.png" rel="noreferrer noopener nofollow"><img src="/image/pTouz.png" alt="This picture contains webview in orange"/></a>
<a href="/image/P0MMI.png" rel="noreferrer noopener nofollow"><img src="/image/P0MMI.png" alt="Attribute Inspector for UIWebView"/></a> </p>

<p>代码有什么问题?</p>

<p>我不擅长敏捷。 </p>

<p>提前致谢</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我想你需要添加</p>

<pre><code>webView.delegate = self
</code></pre>

<p>还将 UIWebViewDelegate 添加到您的类(class)中</p>

<p>编辑:</p>

<p>您的 shouldRecognizeSimultaneouslyWith 和 shouldReceive 看起来有点奇怪,可能是复制粘贴错误?它应该看起来像:</p>

<pre><code>func gestureRecognizer(_: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith:UIGestureRecognizer) -&gt; Bool {
    return true
}
func gestureRecognizer(_: UIGestureRecognizer, shouldReceive:UITouch) -&gt; Bool {
    return true
}
</code></pre>

<p>编辑 2:</p>

<p>我复制了你的确切代码并删除了gestureRecognizerShould begin并且它工作,我也能够删除一些其他代码,不确定你的项目中是否需要它,但这是对我有用的:</p>

<pre><code>class ViewController: UIViewController, UIGestureRecognizerDelegate, UIWebViewDelegate{

@IBOutlet weak var webView: UIWebView!

override func viewDidLoad() {
    super.viewDidLoad()
    webView.delegate = self

    DispatchQueue.main.async {
      let req = URLRequest(url: URL(fileURLWithPath: Bundle.main.path(forResource: self.strTitle+&#34;/index&#34; , ofType: &#34;html&#34;)!))
      self.webView.loadRequest(req)
    }

    let tapGestureRecognizer = UITapGestureRecognizer(target:self, action:#selector(self.goToback))
    tapGestureRecognizer.delegate = self
    tapGestureRecognizer.numberOfTapsRequired = 2
    webView.isUserInteractionEnabled = true
    webView.addGestureRecognizer(tapGestureRecognizer)
}


func gestureRecognizer(_: UIGestureRecognizer,shouldRecognizeSimultaneouslyWith:UIGestureRecognizer) -&gt; Bool {
    return true
}


func goToback() {
    print(&#34;On the Back&#34;)
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - UIWebView 上的双击识别 - Swift 3,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/42550365/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/42550365/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - UIWebView 上的双击识别 - Swift 3