禁用拖动UIWebView/UIScrollView的顶部("拉到刷新"手势)

Luk*_*uke 4 objective-c uiwebview uiscrollview uikit ios

我有一个UIWebView我希望用户能够向下滚动,然后再次备份,但不能从顶部拉回页面(通常与拉动刷新相关的手势).我不希望用户在它背后没有任何东西时拉动它,当他们这样做时什么都不会发生.叫我过度承担;)

向上移动并且已经在内容的顶部,可以禁用滚动,而不会影响向下滚动,或者如果不在顶部则备份?

Ali*_*bas 16

你应该使用:

webView.scrollView.bounces = NO;
Run Code Online (Sandbox Code Playgroud)

UIWebView符合UIScrollViewDelegate.您可以像这样实现委托方法:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    //The webview is is scrolling
    int yPosition = [[_webview stringByEvaluatingJavaScriptFromString: @"scrollY"] intValue];

    if([[_webview stringByEvaluatingJavaScriptFromString: @"document.body.offsetHeight"] intValue] - yPosition == _webview.frame.size.height)
    {
        //The user scrolled to the bottom of the webview
        _webview.scrollView.bounces = YES;
    }else if([[_webview stringByEvaluatingJavaScriptFromString: @"document.body.offsetHeight"] intValue] - yPosition > _webview.frame.size.height + 100){
        _webview.scrollView.bounces = NO;
    }

}
Run Code Online (Sandbox Code Playgroud)

我在Google云端硬盘上传了一个XCode项目示例: 示例代码