简体   繁体   English

WKWebView缩放无法正常工作

[英]WKWebView zooming not working properly

I am using XWalkView which is a subclass of WKWebView, added as a subview to a container view programmatically. 我正在使用XWalkView,它是WKWebView的子类,以编程方式作为子视图添加到容器视图中。 I am trying to zoom the content page of the WKWebView but it does not zoom first time. 我正在尝试缩放WKWebView的内容页面,但第一次没有缩放。 The WKWebView zooms the content page subsequently, ie when I pinch zoom or do double tap zoom it zooms WKWebView itself or container view (my guess) & after that, doing zoom gestures again the content page is zoomed (which is expected behaviour). WKWebView随后将内容页面缩放,即,当我捏缩放或双击缩放时,它将缩放WKWebView本身或容器视图(我的猜测);然后,再次执行缩放手势,将内容页面缩放(这是预期的行为)。

The question is that how to make the content page of WKWebView zoom whenever the zoom gesture is performed. 问题是,每当执行缩放手势时,如何使WKWebView的内容页面缩放。

I am conforming to UIScrollViewDelegate protocol in my ViewController.h as below: 我符合ViewController.h UIScrollViewDelegate协议,如下所示:

@interface ViewController : UIViewController<UIScrollViewDelegate, WKNavigationDelegate, WKUIDelegate>

I am setting the delegate to self in viewDidLoad as below: 我在viewDidLoad中将委托设置为self,如下所示:

xWalkView.scrollView.delegate = self;

Setting zooming scales in viewDidLoad as below: viewDidLoad设置缩放比例,如下所示:

xWalkView.scrollView.minimumZoomScale = 1;
xWalkView.scrollView.maximumZoomScale = 6;

& implementing the protocol methods in ViewController.m as below: 并在ViewController.m实现协议方法,如下所示:

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    if (shouldPageNotZoom) {
        NSLog(@"viewForZoomingInScrollView YES");
        return nil;
    } else {
        NSLog(@"viewForZoomingInScrollView NO");
        return xWalkView.scrollView.subviews[0];
    }
}

-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale {

}

I am using a BOOL shouldPageNotZoom & setting it YES or NO based on URL depending on whether zooming is required or not. 我正在使用BOOL shouldPageNotZoom并根据是否需要缩放根据URL将其设置为YES或NO。 On pages where zooming is required I can see the log of else block ( NSLog(@"viewForZoomingInScrollView NO"); ) being printed in log area & on pages where zooming is not required I can see the log of if block ( NSLog(@"viewForZoomingInScrollView YES"); ). 在需要缩放的页面上,我可以看到在日志区域中打印了else块的日志( NSLog(@"viewForZoomingInScrollView NO");NSLog(@"viewForZoomingInScrollView NO");在不需要缩放的页面上,我可以看到if块的日志( NSLog(@"viewForZoomingInScrollView YES"); )。 Which is working. 哪个在工作。 The only problem is content page does not zoom on first zooming gesture instead WKWebView or container view is zoomed (I guess), but zooms on subsequent zoom gestures. 唯一的问题是内容页面不会在第一个缩放手势上进行缩放,而是将WKWebView或容器视图缩放(我想),但在后续的缩放手势上进行缩放。

After some observation I found that subview at index 0 of scrollview is being zoomed the first & subsequent times (but the content page zooms subsequent times not the first time), which is being returned in viewForZoomingInScrollView . 经过一番观察后,我发现滚动视图的索引0处的子视图在第一次和随后的时间被缩放(但是内容页面在随后的时间而不是第一次缩放),这是在viewForZoomingInScrollView中返回的。 Which should be the case, & is. &是的。 I did this by assigning the tag values to each view & seeing the tag value by logging the tag value in scrollViewDidEndZooming method in log area as below: 我这样做是通过将标签值分配给每个视图并通过在日志区域的scrollViewDidEndZooming方法中记录标签值来查看标签值,如下所示:

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    if (shouldPageNotZoom) {
        NSLog(@"viewForZoomingInScrollView YES");
        return nil;
    } else {
        NSLog(@"viewForZoomingInScrollView NO");
        xWalkView.scrollView.tag = 20;
        xWalkView.tag = 15;
        xWalkView.scrollView.subviews[0].tag = 10;
        _containerView.tag = 5;
        return xWalkView.scrollView.subviews[0];
    }
}

-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale {
    NSLog(@"tag %li", (long) view.tag);
}

Giving the below log: 提供以下日志:

2017-11-28 17:21:36.610753+0530 MyApp[4614:241659] tag 10 //Getting this 1st time of zooming
2017-11-28 17:21:59.369461+0530 MyApp[4614:241659] tag 10 //Getting this at subsequent zooms 

I don't know what am I missing. 我不知道我在想什么。

Please comment if any clarification is needed. 如果需要任何说明,请发表评论。

Got the solution! 得到了解决方案! Returning the last subview first time & first subview of scrollview on subsequent zooms in viewForZoomingInScrollView method solved the problem, as shown below: viewForZoomingInScrollView方法的后续缩放中,首次返回scrollview的最后一个子视图和滚动视图的第一个子视图解决了此问题,如下所示:

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    if (shouldPageNotZoom) {
        NSLog(@"viewForZoomingInScrollView YES");
        return nil;
    } else {
        NSLog(@"viewForZoomingInScrollView NO");
        xWalkView.scrollView.tag = 20;
        xWalkView.tag = 15;
        xWalkView.scrollView.subviews[0].tag = 10;
        _containerView.tag = 5;

        if (isFirstTimeZooming) {
            return xWalkView.scrollView.subviews[xWalkView.scrollView.subviews.count - 1];
        } else {
            return xWalkView.scrollView.subviews[0];
        }
    }
}

-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale {
    NSLog(@"tag %li", (long) view.tag);
    isFirstTimeZooming = NO;
}

isFirstTimeZooming is set to YES on load of the page in didStartProvisionalNavigation method & set to NO in scrollViewDidEndZooming method so that first subview of scrollview can be passed on subsequent zooming. isFirstTimeZooming被设置为YES的页的负荷在didStartProvisionalNavigation方法&设置为NO在scrollViewDidEndZooming方法,以便滚动视图的第一子视图都可以在接下来变焦传递。

I had to use isFirstTimeZooming because if I pass last subview only in viewForZoomingInScrollView the zooming problem was reversed, ie it was zooming properly first time but not on subsequent zooms. 我必须使用isFirstTimeZooming因为如果仅在viewForZoomingInScrollView传递上一个子视图,则缩放问题将被逆转,即,它是第一次正确缩放,但在以后的缩放中没有。

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

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