swift – 在WKWebView上禁用双击滚动

前端之家收集整理的这篇文章主要介绍了swift – 在WKWebView上禁用双击滚动前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个自定义键盘,带有宽度和高度的WKWebView.我已经通过wkWebView禁用了滚动!.scrollView.scrollEnabled = false但是我仍然在WKWebView底部双击时有一个奇怪的滚动行为.这里是我尝试加载的简单网页的源代码http://is.gd/gt8h2q(非常简单,只是带有背景绿色和一行文本的div全屏).下面是GIF的解释.以下是我创建WKWebView的方法
class KeyboardViewController: UIInputViewController,WKScriptMessageHandler {
var wkWebView: WKWebView?

override func loadView() {
    super.loadView()

    let contentController = WKUserContentController()
    contentController.addScriptMessageHandler(self,name:"callbackTestOne")

    let config = WKWebViewConfiguration()
    config.userContentController = contentController

    self.wkWebView = WKWebView(frame:self.view.frame,configuration:config)
    self.view = self.wkWebView!
}

override func viewDidLoad() {
    super.viewDidLoad()

    (...)

    wkWebView!.scrollView.bounces = false
    wkWebView!.scrollView.scrollEnabled = false
    wkWebView!.scrollView.backgroundColor = UIColor(red:248,green:248,blue:248,alpha:1)
    wkWebView!.scrollView.opaque = true
    wkWebView!.scrollView.showsHorizontalScrollIndicator = false
    wkWebView!.scrollView.showsVerticalScrollIndicator = false
    wkWebView!.scrollView.decelerationRate = UIScrollViewDecelerationRateNormal
}

(...)

我有类似的问题,并找到了解决方案.也就是说,删除负责不当行为的UITapGestureRecognizer.

WKWebView,或更确切地说是WKWebView中包含的UIScrollView及其子视图,添加了许多手势识别器.因此,您可以轻松地迭代视图中的所有识别器并删除所需的识别器.

如果要从webView中删除所有单指双击识别器,则需要在滚动视图的子视图内搜索.您可以执行以下操作:

// iterate over all subviews of the WKWebView's scrollView
for subview in _webView.scrollView.subviews {

    // iterate over recognizers of subview
    for recognizer in subview.gestureRecognizers ?? [] {

        // check the recognizer is  a UITapGestureRecognizer
        if recognizer.isKind(of: UITapGestureRecognizer.self) {

            // cast the UIGestureRecognizer as UITapGestureRecognizer
            let tapRecognizer = recognizer as! UITapGestureRecognizer

            // check if it is a 1-finger double-tap
            if tapRecognizer.numberOfTapsrequired == 2 && tapRecognizer.numberOfTouchesrequired == 1 {

                // remove the recognizer
                subview.removeGestureRecognizer(recognizer)
            }
        }
    }
}

这应该可以解决您的问题.

原文链接:https://www.f2er.com/swift/319160.html

猜你在找的Swift相关文章