Never-ending paging UIScrollView


Paging through landscapes on the iPhone believes excellent. Flicking your way through the images app believes simple and normal. And like most iPhone UI handles it's available to programmers through UIKit. It's a UIScrollView with paging permitted.

This paging management is obviously created for articles separated into websites, and most products with websites has both a starting and an end. But in an app I created (which is awaiting approval), I desired the websites to carry on permanently — so as to have a starting but no end. Here's how I did it. Duplication a lot from Apple's own paging search perspective PDF example.

I've created a example venture for you to have fun with with. Here are some of the functions revealed — the primary areas.


- (void)viewDidLoad {
  [super viewDidLoad];

  pagingView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
  pagingView.pagingEnabled = YES;
  pagingView.showsHorizontalScrollIndicator = NO;
  pagingView.delegate = self;
  [self.view addSubview:pagingView];

  visiblePages = [[NSMutableSet alloc] init];
  recycledPages = [[NSMutableSet alloc] init];

  [self tilePages];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  [self tilePages];
}
First we set up a full-screen search perspective with paging permitted. Also we'll have two places of landscapes - the your we're currently making and some for recycling where possible. Our search perspective has no end but the cell phone's RAM have. Lastly we phone tilePages to set up the articles of the search perspective. This technique is also known as when the search perspective is shifting to develop the next websites series up and the earlier ones gradually go away.
- (void)tilePages {
  CGRect visibleBounds = pagingView.bounds;
  int firstNeededPageIndex = floorf(CGRectGetMinX(visibleBounds) / CGRectGetWidth(visibleBounds));
  int lastNeededPageIndex  = floorf((CGRectGetMaxX(visibleBounds)-1) / CGRectGetWidth(visibleBounds));
  int historyCount = 5;
  firstNeededPageIndex = MAX(firstNeededPageIndex-historyCount, 0);
  lastNeededPageIndex  = MAX(lastNeededPageIndex+1, 1);

  // Recycle unneeded controllers
  for (PageViewController *vc in visiblePages) {
    if (vc.index < firstNeededPageIndex || vc.index > lastNeededPageIndex) {
      [recycledPages addObject:vc];
      [vc.view removeFromSuperview];
    }
  }
  [visiblePages minusSet:recycledPages];

  // Add missing pages
  for (int i = firstNeededPageIndex; i <= lastNeededPageIndex; i++) {
    if (![self isDisplayingPageForIndex:i])
      [self addPageWithIndex: i];
  }

  [self resizePagingViewContentSize];
}
The service techniques are have very informative brands, addPageWithIndex and resizePagingViewContentSize, and they do just that. Take a look in the example venture for value.

0 comments:

Post a Comment

Powered by Blogger.
Twitter Delicious Facebook Digg Stumbleupon Favorites More