user1256378
user1256378

Reputation: 742

Scroll fast with UIWebView

I have a UIWebView which I autoscroll via

  // First the timer is called.  
 [NSTimer scheduledTimerWithTimeInterval:scrollerTimerInterval
                                         target:self
                                       selector:@selector(_autoScrollTimerMethod)
                                       userInfo:nil
                                        repeats:YES];
      - (void) _autoScrollTimerMethod{
        scrollerTimerCount++;
        CGFloat offset = scrollerTimerCount * 1;
        uiWebView.scrollView.contentOffset = CGPointMake(0, offset);
 }  

uiWebView is of type UIWebView

This code does autoscroll the uiWebView, but its too slow. There is a flicker in another part of the program which does not occur when this code is commented out.

Any suggestions are welcome.

Cheers,

Upvotes: 1

Views: 532

Answers (1)

wottle
wottle

Reputation: 13619

So I think I came up with a solution that might do what you need. Basically, I created a UIWebView inside of a UIScrollView. I had to do this, because otherwise the UIWebView by itself wouldn't be fully rendered why the animation is going, and you would get large sections of white (unrendered portions of the webview). So in Interface Builder, add a scrolview with a webview inside it, then hook them both up to IBOutlets.

You'll also need your ViewController to implement the UIWebViewDelegate protocol.

@interface ViewController : UIViewController <UIWebViewDelegate>

@end

In your viewDidLoad, (or interface builder) hook up the delegate for the webview:

    self.webView.delegate = self;

Then load a URL in the webview:

[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://stackoverflow.com/questions/"]]];

Once the webview is finished loading, we will expand it's frame to the full height of the contentSize, causing it to be fully rendered. No this will be a memory hob if you do this for very large web pages, so use it with caution. Then we will set the contentSize of the scrollview to match, then simply animate setting the content offset to the end, with whatever duration you want.

- (void)webViewDidFinishLoad:(UIWebView *)webView {

   webView.frame = CGRectMake(webView.frame.origin.x, webView.frame.origin.y, webView.scrollView.contentSize.width, webView.scrollView.contentSize.height);

    self.scrollView.contentSize = webView.scrollView.contentSize;

    [UIView animateWithDuration:10 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
        [self.scrollView setContentOffset:CGPointMake(0, self.scrollView.contentSize.height - self.scrollView.frame.size.height)];
    } completion:^(BOOL finished) {

    }];
}

Upvotes: 3

Related Questions