UIWebView将垂直滚动事件委托给父级,如果滚动到顶部或底部 [英] UIWebView delegate vertical scroll event to parent, if scrolled to top or bottom

查看:124
本文介绍了UIWebView将垂直滚动事件委托给父级,如果滚动到顶部或底部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在UIScrollView中有一个UIWebView,它是分页的。基本上我想用滚动/拖动手势在不同的webview之间切换。水平它工作正常。如果webview滚动到页面边框,则滚动/平移事件将传递到滚动视图,以便显示下一页。在垂直轴上,这不起作用。似乎没有传递给scrollview的事件。这可能是弹跳的问题,我禁用了,因为我想,它会消耗这个事件。但即使禁用弹跳,这也不起作用。

I have an UIWebView in an UIScrollView, which is paginated. Basically I want to switch between different webviews with scroll / fling gestures. Horizontally it works fine. If the webview is scrolled to the "border of the page", the scroll/pan event is passed to the scrollview, so that the next page is shown. On the vertical axis, this does not work. There does not seem to be an event passed to the scrollview. It might be a problem with the bouncing, which I disabled, because I thought, it would consume the event. But even with bouncing disabled, this does not work.

我之前在一个scrollview中使用TextViews对此进行了测试,这也很有效。可能是webviews的一种特殊行为。

I tested this before with TextViews in a scrollview, which worked, too. Might be a special behaviour of webviews.

任何想法,我是如何让这个工作的?我是否必须实现事件监听器并手动将事件传递给scrollview?

Any idea, how I get this to work? Do I have to implement event listeners and pass the events manually to the scrollview?

此外,如果您更好地了解如何实现这种布局,请告诉我。正如我所说,我希望有一个不同视图的网格,可以通过fling / scroll手势来改变。与AppStore应用程序类似,其中应用程序的详细信息可以垂直滚动,屏幕截图可以水平滚动。

Furthermore, if you have a better idea how to implement this kind of layout, let me know. As I said, I want to have a grid of different views, which can be changed by fling/scroll gestures. Similar to the AppStore app, where the details of an app can be scrolled vertically and the screenshots can be scrolled horizontally.

干杯
Henrik

Cheers Henrik

推荐答案

所以,我想通了。

首先我设置了webview的委托,以便我获得滚动事件并可以检查,如果webview滚动到顶部或底部:

First I set the delegate of the webview, so that I get scroll events and can check, if the webview is scrolled to top or bottom:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if([scrollView isEqual:webView.scrollView]) {
        float contentHeight = scrollView.contentSize.height;
        float height = scrollView.frame.size.height;
        float offset = scrollView.contentOffset.y;

        if(offset == 0) {
            webViewScrolledToTop = true;
            webViewScrolledToBottom = false;
        } else if(height + offset == contentHeight) {
            webViewScrolledToTop = false;
            webViewScrolledToBottom = true;
        } else {
            webViewScrolledToTop = false;
            webViewScrolledToBottom = false;
        }

        //NSLog(@"Webview is at top: %i or at bottom: %i", webViewScrolledToTop, webViewScrolledToBottom);
    }
}

然后我在webview的滚动视图中注册了额外的滑动手势识别器:

Then I registered additional swipe gesture recognizers at the webview's scrollview:

swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeUp)];
swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
swipeUp.delegate = self;
[self.webView.scrollView addGestureRecognizer:swipeUp];
[self.webView.scrollView.panGestureRecognizer requireGestureRecognizerToFail:swipeUp];
swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeDown)];
swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
swipeDown.delegate = self;
[self.webView.scrollView addGestureRecognizer:swipeDown];
[self.webView.scrollView.panGestureRecognizer requireGestureRecognizerToFail:swipeDown];



注意到 [self.webView.scrollView.panGestureRecognizer requireGestureRecognizerToFail呼叫:swipeUp ]; 。这些是绝对必要的,因为没有它们,webview的平移手势识别器总是在它们到达轻扫手势识别器之前消耗事件。这些调用改变了优先级。

Notice the calls to [self.webView.scrollView.panGestureRecognizer requireGestureRecognizerToFail:swipeUp];. Those are absolutely necessary, because without them, the pan gesture recognizer of the webview would always consume the events, before they reach the swipe gesture recognizer. Those calls change the priorities.

在swipeUp和swipeDown方法中,我计算下一个页面的位置并将父滚动视图滚动到此位置,如果有实际上是下一页。

In the swipeUp and swipeDown methods, I calculate the position of the next "page" and scroll the parent scroll view to this position, if there actually is a next page.

最后一件事是检查,如果webview滚动到顶部或底部,并且只接受这种情况下的手势。为此,你必须实现手势识别的代表:

Last thing is, to check, if the webview is scrolled to top or bottom and only accept the gestures in that case. Therefor you have to implement the delegate of the gesture recognizer:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if(gestureRecognizer == swipeUp) {
        return webViewScrolledToBottom;
    } else if(gestureRecognizer == swipeDown) {
        return webViewScrolledToTop;
    }

    return false;
}

你可能还必须禁用滚动弹跳才能使这个工作与网页一起使用,网页很小,它们根本不滚动: webView.scrollView.bounces = false;

You might also have to disable the scroll bouncing to make this work with webpages, which are so small, that they are not scrolled at all:webView.scrollView.bounces = false;

这篇关于UIWebView将垂直滚动事件委托给父级,如果滚动到顶部或底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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