views:

56

answers:

1

In my iPhone app I'm looking for a solution where I have some images scrolling horizontally, automatically and infinitely around and around. So if I have four images: first image 1 is shown, then it automatically scrolls out, image 2 is shown, then it scrolls out, ... image 4 is shown, then it scrolls out, and then image 1 is shown again and around it goes... (image1->image2->image3->image4->image1...)

I have been looking for a solution where I use a UIScrollView, but I'm not sure how to implement it. Is there a tutorial or anything on how to do this? How would you solve it? Thanks!

+1  A: 

You can implement the ScrollViewDelegate's - (void)scrollViewDidScroll:(UIScrollView *)scrollView method and use the setContentOffset of the scroll view once the content offset is larger than some maximum or less than some minimum.

Before doing that you will have to duplicate the contents of the scroll view at least 3 times, like this: .

Once the scroll offset reaches the duplicated content you will have to set the offset to the original content offset...

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.infiniteScrollView) {
        CGFloat xOffset = scrollView.contentOffset.x;
        CGFloat yOffset = scrollView.contentOffset.y;

        if (xOffset > maxOffset) {
            xOffset = origOffset + (xOffset - maxOffset);
        }
        else if (xOffset < minOffset) {
            xOffset = origOffset + (xOffset - minOffset);
        }

        if (xOffset != scrollView.contentOffset.x) {
            [scrollView setContentOffset:CGPointMake(xOffset ,yOffset)];
        }
    }
}
Michael Kessler