title: "Infinite UIScrollView" containing UIWebViews
body: <p><strong>The background:</strong></p>
  
  <p>First of all, the background. I've created an "infinite scrollview" by using an UIScrollView with the width of three pages (0, 1 and 2) and then made sure that the UIScrollView always is centered on page 1 when not being scrolled upon. When you scroll to either page 0 or page 2 and the UIScrollViewDelegate senses that the scrolling ended, the content of all the three pages first switches in the direction that you've scrolled and then the UIScrollView instantly moves back to page 1, simulating the effect that you can continue to scroll through an endless stream of pages.</p>
  
  <p>At first, I used a UITextView to display the text content of each page, but pretty soon wanted something more flexible when it comes to styling the text. I therefore ended up using a UIWebView in which I load an NSString containing the styled HTML content I wish to display. This worked perfectly, just the way I wanted it to look.</p>
  
  <p><strong>The problem:</strong></p>
  
  <p>Naturally, it takes longer time to load, parse and show a simple HTML page in a UIWebView than it takes to just load a static text in a UITextView. Because of this, when you've scrolled in whatever direction, and the scroll view automatically moves back and switches the content of the pages, you can hint the old content for a tenth of a second before the new one shows.</p>
  
  <p><strong>My solution (that did not work):</strong></p>
  
  <p>I searched for the UIWebViewDelegate in the documentation and found that there's an event when the page is done loading. I hooked this up to do this:</p>
  
  <ol>
  <li>User scrolls from page 1 to page 2.</li>
  <li>UIScrollViewDelegate calls scrollViewDidEndDecelerating, telling page 1 to switch it's content to the same content currently being displayed at page 2.</li>
  <li>UIWebViewDelegate of page 1 calls webViewDidFinishLoad, telling the UIScrollView to move back to page 1 and then switch the content of page 0 and 2.</li>
  </ol>
  
  <p>In my head this would fix this problem, but apparently it's still not enough. Therefore, I ask you for help. Any ideas? Might be worth mentioning that I've had NSLog in all the methods and made sure that they're being called. If there's anything else, I'd be glad to answer any questions you have.</p>
  
  <p>Thanks in advance!<br /><br /></p>
  
  <p><strong>View Controller containing UIScrollView:</strong></p>
  
  <pre><code>    // Sense when the UIScrollView stops accelerating
      - (void)scrollViewDidEndDecelerating:(UIScrollView *)sender {
          if(pScrollView.contentOffset.x &gt; pScrollView.frame.size.width) {
              [self switchToNextPost];
          } else if (pScrollView.contentOffset.x &lt; pScrollView.frame.size.width) {
              [self switchToPreviousPost];
          }
      }
  
      // Update the currentPost variable and switch page 1
      - (void)switchToNextPost {
          [prefs setInteger:[[self getNextKey:nil] intValue] forKey:@"currentPost"];
  
          [self loadPostWithKey:[self getCurrentKey] atPage:[NSNumber numberWithInt:1] withViewController:currentPage];
      }
  
      // Update the currentPost variable and switch page 1
      - (void)switchToPreviousPost {
          [prefs setInteger:[[self getPreviousKey:nil] intValue] forKey:@"currentPost"];
  
          [self loadPostWithKey:[self getCurrentKey] atPage:[NSNumber numberWithInt:1] withViewController:currentPage];
      }
  
      // Custom delegate function. Scrolls UIScrollView to page 1 and then loads new content on page 1 and 3
      - (void)webViewFinishedLoading {
          [pScrollView scrollRectToVisible:CGRectMake(pScrollView.frame.size.width, 0, pScrollView.frame.size.width, pScrollView.frame.size.height) animated:NO];
  
          [self loadPostWithKey:[self getPreviousKey:nil] atPage:[NSNumber numberWithInt:0] withViewController:previousPage];
          [self loadPostWithKey:[self getNextKey:nil] atPage:[NSNumber numberWithInt:2] withViewController:nextPage];
      }
  </code></pre>
  
  <p><br />
  <strong>View Controller for the subviews being displayed as separate pages in the UIScrollView</strong></p>
  
  <pre><code>    // The delegate shown in previous code
      - (void)webViewDidFinishLoad:(UIWebView *)webView {
          [[self delegate] webViewFinishedLoading];
      }
  </code></pre>