iPhone dev: Increase scroll speed in UIWebView?

后端 未结 5 1170
滥情空心
滥情空心 2021-02-09 04:41

I have an app in which I render local HTML files in a UIWebView. The files, however, are sometimes large, and getting to where you want takes a long time with the default scroll

相关标签:
5条回答
  • 2021-02-09 05:02

    Have considered adding # tags into your html on significant boundaries?

    You could actually use native UI to implement bookmarks or a ToC for easier navigation, or simply embed links to the appropriate targets right in your html.

    If you 'speed up scrolling' your app is at risk of rejection for being non-standard, since it may confuse users who are used to webviews scrolling with a standard 'feel'.

    0 讨论(0)
  • 2021-02-09 05:09

    Search for a subview of UIWebView that responds to -setScrollDecelerationFactor: (it's UIScroller - a private class that's the only subview of UIScrollView). You'll find that it takes the same deceleration factors defined for the public UIScrollView class:

    - (void)webViewDidFinishLoad:(UIWebView *)aView {
        id decelerator = [aView viewWithSelector:@selector(setScrollDecelerationFactor:)];
        [decelerator setScrollDecelerationFactor:UIScrollViewDecelerationRateNormal];
    }
    

    Note that the method I'm using viewWithSelector: is a method I defined in a category of UIView. Presumably, if UIWebView changes in future, my search will return nil and this method will become a no-op.

    0 讨论(0)
  • 2021-02-09 05:18

    Old question but these setting or trick really helped me even in 2018.

    Follow these simple coding tricks to improve Android WebView Performance:

    WebView mWebView = new WebView(this);
    WebSettings settings = mWebView.getSettings();
    settings.setSupportZoom(true);
    settings.setBuiltInZoomControls(false);
    settings.setJavaScriptEnabled(true);
    settings.setLoadWithOverviewMode(true);
    settings.setUseWideViewPort(true);
    mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    mWebView.setScrollbarFadingEnabled(true);
    settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
    settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
    settings.setDomStorageEnabled(true);
    
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    
        mWebView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
    
    } else {
    mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }
    
    0 讨论(0)
  • 2021-02-09 05:20

    Find a subview of UIWebView which is a UIScrollView, then set decelerationRate to UIScrollViewDecelerationRateNormal. This makes the UIWebView as fast as an ordinary UIScrollView.

    In iOS 4/5, we can simply use the last subview of UIWebView.

    UIScrollView *scroll = [webView.subviews lastObject];
    if ([scroll isKindOfClass:[UIScrollView class]]) {
        scroll.decelerationRate = UIScrollViewDecelerationRateNormal;
    }
    

    The default decelerationRate of UIWebView's UIScrollView is 0.989324, while UIScrollViewDecelerationRateFast is 0.99, and UIScrollViewDecelerationRateNormal is 0.998.

    This method doesn't use any private API.

    0 讨论(0)
  • 2021-02-09 05:21

    In iOS 5 we can access the scrollView property of the UIWebView.

    If you are targeting iOS 5+, you can simply call:

    webView.scrollView.decelerationRate = UIScrollViewDecelerationRateNormal;
    
    0 讨论(0)
提交回复
热议问题