iPhone dev: Increase scroll speed in UIWebView?

2020-06-04 04:56发布

问题:

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 speed. Is there any way to boost up the vertical scroll speed of a UIWebView?

回答1:

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;


回答2:

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.



回答3:

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.



回答4:

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'.



回答5:

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);
}