views:

1102

answers:

3

I have a UIScrollView with images in it. When the user reaches the last image and swipes the finger for next image, I want to show the first image. Similarly, when the user is at the first image and swipes for previous image, I want to show the last image. In short, it will be a circular sort of a thing.

How can I implement this?

+3  A: 

You'll need to work with contentOffset and contentSize.

Everytime there is a scroll event, you can consider checking if the user is at say currentLength - 1, and if so, dynamically adding an image to the scrollview.

This should work for going being 5, although might get you in trouble in terms of memory if you dont release the earlier images.

I do not think this solution will work with images less than index 0.

Alternately... Lets say you have images I1 to I5. Layout a scrollview with the following images: I5 I1 I2 I3 I4 I5 I1

Autoscroll to I1 (height of I5), without animation.

If the user ever scrolls above this mark, autoscroll them (without animation) down to the second I5 I1 pair. If the user ever attempts to scroll below I5 near the end, you could autoscroll them back to the first I5 I1 pair. If you scroll without animation, and to the correct height, it will appear that the images are endless.

Im not sure if this will be very responsive, but it might give the effect for which you're looking.

Willi Ballenthin
That's pretty much what I did in one of my apps. It works fine.
Airsource Ltd
This does not work if you need the movement of the view to be continuous. Unless you could scroll somewhere with a specific speed.
Dimitris
+2  A: 

The key thing to realise is that a UIScrollView is very intelligent about what it redraws.

You can move its subviews around and as long as the views that are visible on the screen do not move or change, there is no redraw. You can reset the frame of any offscreen subviews (and if necessary adjust the contentOffset) without causing a redraw.

Of course, having paging enabled makes this a lot easier but I'm sure you could do it from didScroll too.

You should generate your 5 views add them as subviews and set the offset of the scroll view to the center of the five. As the scrollView scrolls, you can then switch the subviews around - like a round robin. There is no need to remove and re-add the subviews, you can just reset the frames.

This is (on a simple level) how a UITableView works too - it reuses a small set of cell views and keeps resetting the content offset.

Roger Nolan
A: 

This answer should really be a comment to Willi Ballenthin's answer, but I'm too new to Stack Overflow to be able to comment yet. The method Willi suggests as "alternatively" works great, but to ensure that the unanimated autoscrolling back and forth from the beginning to the end of the list of images is smooth and unnoticeable to the user, you should turn paging on in the UIScrollView and do the autoscrolling in the UIScrollViewDelegate's - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView method.

sam2themax