How to implement a cyclic UIScrollView?

故事扮演 提交于 2019-11-26 21:55:04

问题


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:


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].



来源:https://stackoverflow.com/questions/3421269/how-to-implement-a-cyclic-uiscrollview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!