How to implement a cyclic UIScrollView?

2019-01-15 16:14发布

How to implement a cyclic UIScrollView? That is to say, when you scroll to the very left item then the UIScrollView will show the very right one. Any help would be appreciate.

1条回答
Luminary・发光体
2楼-- · 2019-01-15 16:25

Sure, you need three views. At any given time you have a left view, a right view and a current view.

This requires notification of each movement through the UIScrollViewDelegate.

If you detect that you moved right, you free left, make left = current, current = right, and make a new right.

If you detect that you moved left, you free right, make right = current, current = left, and make a new left.

Generally speaking, any view that is more than one page away from current is not required. So you need only three pages in total.

Of course you also need to manipulate the position of the UIScrollView so you can make the movements - the net result is you don't move although it looks like you have. When you have done the scroll, and altered the views according to the left/current/right shuffle - you do

  [self scrollRectToVisible:(middle frame) animated:NO];

so that you are always looking at the same actual page, with one page each side of it. When the scroll happens it looks like the user can keep scrolling around in a loop - but after each page ticks over, the views are shuffled, the position within the scroll view gets set back to the middle and the user can scroll again.

Getting back to the start is simply a matter of using the view related to whatever object is at the other end of whatever data structure you are using - so if current = [(NSArray)data lastObject] then right = [(NSArray)data objectAtIndex:0].

查看更多
登录 后发表回答