views:

881

answers:

4

Hi folks!

In my iPhone app there is a scrollview (pagingEnabled=NO) which can contain up to 200 subviews (150 x 150) and the challenge is to do lazy loading and simulate endless scrolling (without bouncing) in horizontal directions.

Is there a solution or an alternative for this request?

Thanks a lot, Daniel

+3  A: 

Lazy loading of a scroll view is demonstrated in one of Apple's sample code projects: PageControl.

To fake endless scrolling what I'd suggest is to make your scroll view very wide to begin with, wider than an average person would scroll in one set of scrolling behaviors. Then in your delegate methods -scrollViewDidEndScrollingAnimation:, -scrollViewDidEndDragging:willDecelerate: and -scrollViewDidEndDecelerating:, one or more of which will be called after the user is done scrolling, reposition your content to exist in the center of the scroll view and update your contentOffset point without animating.

For that to work visually you would need to also disable the horizontal scroller. You'll also need to consider how to determine what view to draw at a particular contentOffset with this method since you won't be able to just divide the contentOffset.x by the scroll view's bounds anymore to find out where you are.

Ashley Clark
A: 

Hello I found the way to do it. I have a master array with all the subviews (in my case they are images, so I store the names). The scrollview only has 3 subviews: left, current, right. Paging is enabled, so the user cant really spin more that one view left/right at any time. What I do is: 1) track his current position on the master array. If he moves left, subtract one; right add one. Like this:

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  [some code to determine current page, based on contentOffset]
    if (page == 0){
        NSLog(@"Going left");
        if (currentPage > 0){
            currentPage--;
        } else {
            //cycle to last
            currentPage = [images count] -1;
        }
    } else if (page == 2){
        NSLog(@"Going right");
        if (currentPage < ([images count] -1)){
            currentPage++;
        } else {
            //cycle to first
            currentPage = 0;
        }
    } else{
        NSLog(@"Not moving");
    }

2) after the user moves, I reload 3 new images, like this:

//updates the 3 views of the scrollview with a new center page.
-(void) updateScrollViewForIndex:(NSInteger)newCenterPage{
    //fist clean scroll view
    for (UIView *sView in [scroll subviews]){
        [sView removeFromSuperview];
    }

    NSInteger imgCount = [images count];

    //set center view
    [self loadImageIndex:newCenterPage atPosition:1];

    //set left view
    if (newCenterPage > 0){
        [self loadImageIndex:newCenterPage-1 atPosition:0];

    } else {
        //its the first image, so the left one is the last one
        [self loadImageIndex:imgCount-1 atPosition:0];
    }

    //set right view
    if (newCenterPage < imgCount-1){
        [self loadImageIndex:newCenterPage+1 atPosition:2];
    } else {
        //its the last image, so ther right one is the first one
        [self loadImageIndex:0 atPosition:2];       
    }
}

3) Finally re-center the scroll view to the center view again:

[scroll setContentOffset:CGPointMake(1 * viewWidth, 0)];

Hope this helps, although "the man with the plan" is Mr. Clark, who pointed the way.

Gonso

gonso
A: 

What would "some code to determine current page" look like?

Shades
A: 

Matt Gallagher has a blog post which describes a solution to this exact problem. I've used it and it works great.

The UIScrollView and UIPageControl in Cocoa Touch allow for user interfaces with multiple panning pages. The sample project that Apple provides (PageControl) keeps all child views for every page in a lazily loaded array. I'll show you how you can implement this using just two child views, no matter how many virtual pages you wish to represent.

It works by shuffling around the child views and reseting their content when necessary. I used this for displaying flash cards, where there could be anywhere from 3 to 3,000 items. Although it's set up right now for paging, I'm sure you could get it to work with regular scrolling.

Michael Grinich