Objective-C: infinite UIScrollView without pagingEnabled

My iPhone app has a scrollview pagingEnabled=NO

that can hold up to 200 subviews (150 x 150) and the challenge is to do lazy loading and simulate infinite scrolling (no bouncing) in horizontal directions.

Is there a solution or alternative for this query?

+2


a source to share


3 answers


Scroll lazy loading is demonstrated in one of Apple's code projects: PageControl .

To fake infinite scrolling, I would suggest making your scroll very wide to start with, wider than a normal human would scroll in a single scroll set. Then in your delegate methods -scrollViewDidEndScrollingAnimation:

, -scrollViewDidEndDragging:willDecelerate:

and -scrollViewDidEndDecelerating:

one or more of which will be called after the user scrolls, move your content to the scroll center and update yours contentOffset

without animation.



To do this, visually and visually, you will also need to disable the horizontal scroller. You will also need to consider how to determine which view to draw on a particular one contentOffset

using this method, as you cannot simply split contentOffset.x

into scroll boundaries to find out where you are.

+6


a source


Hello I have found a way to do this. I have a main array with all the subviews (in my case these are images, so I store the names). There are only 3 subheadings in scrollview: left, current, right. Paging is enabled, so the user cannot unscrew more than one left / right view at any time. What I am doing: 1) Track its current position in the main array. If it moves to the left, subtract it; add one correctly. 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 navigates, I reload 3 new images, for example:

//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, expand the scroll view to the center again:

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

      

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

Gonso

+2


a source


Matt Gallagher has a blog post describing a solution to this exact problem. I have used it and it works great.

UIScrollView and UIPageControl in Cocoa Touch enable user interfaces with multiple panning pages. The sample project that Apple provides (PageControl) stores all child views for each page in a lazy loaded array. I'll show you how you can implement this using only two child views, no matter how many virtual pages you want to present.

It works by traversing the child views and rewriting their content when needed. I used this to display flash cards where there can be 3 to 3000 points. Although it is configured for swap right now, I'm sure you can get it to work with normal scrolling.

+1


a source







All Articles