Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make UIScrollView wrap around [duplicate]

Possible Duplicate:
UIScrollView. Any thoughts on implementing “infinite” scroll/zoom?

I have a UIScroll which has eight or so pages. My aim is for them to 'loop' around so that when the user reaches the last page the first page will be the next page (and vice versa) so that the user could scroll through the pages endlessly. What is the best way to achieve this?

I am working from this example from Cocoa with Love.

Any help is greatly appreciated. Thank you.

like image 539
Andrew Davis Avatar asked Jun 18 '10 16:06

Andrew Davis


2 Answers

Problem is sorted now. Basically, I created extra pages before and after my pages so it looks like this:

8 1 2 3 4 5 6 7 8 1

Then in the scrollViewDidEndDecelerating I do a simple if statement that checks if I am at either the first or last page and locate the scrollView appropriately.

- (void)scrollViewDidEndDecelerating:(UIScrollView *)newScrollView
{
    [self scrollViewDidEndScrollingAnimation:newScrollView];
    pageControl.currentPage = currentPage.pageIndex;

    if(currentPage.pageIndex==0){
        scrollView.contentOffset = CGPointMake(320*(pageControl.numberOfPages-2), 0);
        pageControl.currentPage = pageControl.numberOfPages-2;
    }else if(currentPage.pageIndex==pageControl.numberOfPages-1){
        scrollView.contentOffset = CGPointMake(320, 0);
        pageControl.currentPage = 1;
    }

}
like image 173
Andrew Davis Avatar answered Sep 28 '22 22:09

Andrew Davis


For anyone else trying to solve this problem, I would recommend taking a look at apple's video, which includes a tutorial for infinite scrolling & wrapping: https://developer.apple.com/videos/wwdc/2011/

Along with its sample code: https://developer.apple.com/library/ios/#samplecode/StreetScroller/Introduction/Intro.html

I'm currently using the "8 1 2 3 4 5 6 7 8 1" method and haven't attempted to implement the method described in the video yet, but it appears that it could prevent the small "bounce" at the last or first page you can encounter when scrolling quickly on the 8 1 2...1 method.

like image 40
jankins Avatar answered Sep 30 '22 22:09

jankins