Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone dev: Increase scroll speed in UIWebView?

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?

like image 547
thomax Avatar asked May 06 '10 15:05

thomax


3 Answers

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;
like image 178
Bruno Delgado Avatar answered Oct 19 '22 09:10

Bruno Delgado


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.

like image 27
Quotation Avatar answered Oct 19 '22 10:10

Quotation


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.

like image 39
MrO Avatar answered Oct 19 '22 08:10

MrO