简体   繁体   English

防止Angular NativeScript WebView用捏缩放进行缩放

[英]Prevent Angular NativeScript WebView from zooming with pinch zoom

So I have a Nativescript App using Angular (NG 5.1.1 / Angular 7.x) 所以我有一个使用Angular(NG 5.1.1 / Angular 7.x)的Nativescript应用

I have a view with a webview. 我有一个webview视图。

    @ViewChild("myWebView") webViewRef: ElementRef;

    <WebView class="webview" #myWebView [src]="myURL"></WebView>

Inside my webview.component.ts I have this. 在我的webview.component.ts内部,我有这个。

ngAfterViewInit(): void {
    const webview: WebView = this.webViewRef.nativeElement;

    webview.on(WebView.loadFinishedEvent, (args: LoadEventData) => {
        this.setIndicatorFalse();
        if (webview.ios) {
            webview.ios.scrollView.delegate = UIScrollViewDelegateZ.new();
            webview.ios.scrollView.minimumZoomScale = 1;
            webview.ios.scrollView.maximumZoomScale = 1;
        }
    });
    webview.on(WebView.loadStartedEvent, (args: LoadEventData) => {
        if (webview.android) {
            webview.android.getSettings().setBuiltInZoomControls(false);
            webview.android.getSettings().setDisplayZoomControls(false);
        } else {
            // @ts-ignore
            webview.ios.multipleTouchEnabled = false;
            webview.ios.scalesPageToFit = false;
            webview.ios.scrollView.bounces = false;
            webview.ios.scrollView.showsHorizontalScrollIndicator = true;
            webview.ios.scrollView.showsVerticalScrollIndicator = true;
            webview.ios.opaque = false;
            webview.ios.scrollView.allowsInlineMediaPlayback = true;
            webview.ios.scrollView.mediaPlaybackRequiresUserAction = false;
        }
    });
}

As you can see i've tried all sorts of stuff to get this webview to not pinch zoom. 如您所见,我已经尝试了各种方法来使该Webview不会收缩缩放。

I am overriding my ViewDelegate with 我用覆盖我的ViewDelegate

webview.ios.scrollView.delegate = UIScrollViewDelegateZ.new();

and that file is here 这个文件在这里

export class UIScrollViewDelegateZ extends NSObject implements UIScrollViewDelegate {

public static ObjCProtocols = [UIScrollViewDelegate];

static new(): UIScrollViewDelegateZ {
    console.log("here we are");

    return <UIScrollViewDelegateZ>super.new();
}

viewForZoomingInScrollView(scrollView: UIScrollView): UIView {
    console.log("viewForZoomingInScrollView");

    return null;
}

scrollViewDidScroll(scrollView: UIScrollView): void {
    console.log("scrollViewDidZoom");

    return null;
}

scrollViewWillBeginZoomingWithView(scrollView: UIScrollView, view: UIView): void {
    console.log("scrollViewWillBeginZoomingWithView   " + scrollView);

    return null;
    }
}

When I load my webview and pinch zoom my console log says this: 当我加载Webview并捏缩放时,我的控制台日志显示以下内容:

 CONSOLE LOG file:///app/app/webview/scrollDelegate.js:9:20: here we are
 CONSOLE LOG file:///app/app/webview/scrollDelegate.js:21:20: scrollViewWillBeginZoomingWithView   <WKScrollView: 0x7fb140afe000; baseClass = UIScrollView; frame = (0 0; 375 603); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x600000ab2100>; layer = <CALayer: 0x600000537480>; contentOffset: {0, 0}; contentSize: {375, 1916}; adjustedContentInset: {0, 0, 0, 0}>
 CONSOLE LOG file:///app/app/webview/scrollDelegate.js:17:20: scrollViewDidZoom

So I feel like I am close?? 所以我觉得我接近了吗? but I just can't get it to go? 但我无法继续前进? I thought I was suppose to return null / undefined when trying to pinch zoom? 我以为是在尝试缩放时会返回null / undefined? which is what a few other SO answers / the web say to do but nothing is working. 这是其他一些SO答案/网络说要执行的操作,但没有任何效果。

the HTML page also has the correct metatag headers... HTML页面还具有正确的metatag标头...

  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0, minimal-ui"/>

Any help would be appreciated! 任何帮助,将不胜感激! Thanks!! 谢谢!!

You have to override the default meta data being set by tns-core-modules. 您必须覆盖tns-core-modules设置的默认元数据。

import { WebView } from 'tns-core-modules/ui/web-view';

declare var WKUserScript, WKUserScriptInjectionTime, WKUserContentController, WKWebViewConfiguration, WKWebView, CGRectZero;

(<any>WebView.prototype).createNativeView = function () {
    const jScript = `var meta = document.createElement('meta'); 
    meta.setAttribute('name', 'viewport');
    meta.setAttribute('content', 'initial-scale=1.0 maximum-scale=1.0');
    document.getElementsByTagName('head')[0].appendChild(meta);`;
    const wkUScript = WKUserScript.alloc().initWithSourceInjectionTimeForMainFrameOnly(jScript, WKUserScriptInjectionTime.AtDocumentEnd, true);
    const wkUController = WKUserContentController.new();
    wkUController.addUserScript(wkUScript);
    const configuration = WKWebViewConfiguration.new();
    configuration.userContentController = wkUController;
    configuration.preferences.setValueForKey(
        true,
        "allowFileAccessFromFileURLs"
    );
    return new WKWebView({
        frame: CGRectZero,
        configuration: configuration
    });
};

Here is a Playground Sample , since you are using Angular you may add the above lines to your app.component.ts 这是一个Playground示例 ,由于您正在使用Angular,因此可以将上述app.component.ts行添加到您的app.component.ts

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM