在WKWebView中禁用放大手势 [英] Disable magnification gesture in WKWebView

查看:2317
本文介绍了在WKWebView中禁用放大手势的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来禁用WKWebView的iOS实现上的捏缩放放大手势。 OS X有一个放大BOOL属性,但它似乎在iOS上不可用。

I'm looking for a way to disable the "pinch to zoom" magnification gesture on the iOS implementation of WKWebView. There is a magnification BOOL property available for OS X but it doesn't seem to be available on iOS.

WKWebView.h

WKWebView.h

#if !TARGET_OS_IPHONE
/* @abstract A Boolean value indicating whether magnify gestures will
 change the web view's magnification.
 @discussion It is possible to set the magnification property even if
 allowsMagnification is set to NO.
 The default value is NO.
 */
@property (nonatomic) BOOL allowsMagnification;

我也试过看看WKWebView的手势识别器,但这似乎已经出现了空数组。我假设实际的识别器在组件的结构中更加深入(相当复杂,看起来很复杂),如果可能的话,宁愿不去挖掘它们。

I've, also, tried look at the WKWebView's gesture recognizers but that seems to be turning up an empty array. I'm assuming the actual recognizers are bured deeper in the component's structure (fairly complex, by the looks of it) and would rather not go digging for them if at all possible.

我知道可能存在可能会禁用手势的黑客攻击(选择性地将手势传递给WebView,添加子视图以捕获捏手势等)但我总是发现那些引入滞后的事件并希望保持实现尽可能干净/无黑客。

I know of possible hacks that could potentially disable the gesture from firing (selectively passing gestures to the WebView, add child view to capture pinch gesture, etc) but I've always found those introduce lag into the event and want to keep the implementation as clean/hack free as possible.

推荐答案

您可以通过设置WKWebKit的UIScrollView的委托来阻止用户进行缩放。实现 viewForZooming(in :) ,如下所示:

You can prevent your users from zooming by setting the delegate of your WKWebKit's UIScrollView and implementing viewForZooming(in:) as in the following:

class MyClass {
    let webView = WKWebView()

    init() {
        super.init()
        webView.scrollView.delegate = self
    }

    deinit() {
        // Without this, it'll crash when your MyClass instance is deinit'd
        webView.scrollView.delegate = nil
    }
}

extension MyClass: UIScrollViewDelegate {
    func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        return nil
    }
}

这篇关于在WKWebView中禁用放大手势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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