开发者

Make UIScrollView wrap around [duplicate]

This question already has answers here: Closed 10 years ago.

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 scr开发者_如何学Pythonoll 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.


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;
    }

}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜