如何实现一个循环的 UIScrollView?

How to implement a cyclic UIScrollView?(如何实现一个循环的 UIScrollView?)

本文介绍了如何实现一个循环的 UIScrollView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何实现一个循环的UIScrollView?也就是说,当您滚动到最左边的项目时,UIScrollView 将显示最右边的项目.任何帮助将不胜感激.

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.

推荐答案

当然,你需要三个视图.在任何给定时间,您都有左视图、右视图和当前视图.

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

这需要通过 UIScrollViewDelegate 通知每次移动.

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.

当然,您还需要操纵 UIScrollView 的位置,以便进行移动 - 最终结果是您没有移动,尽管看起来您已经移动了.当您完成滚动并根据左/当前/右随机播放更改视图时 - 您可以

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.

回到开始只是使用与您正在使用的任何数据结构的另一端的任何对象相关的视图 - 所以如果 current = [(NSArray)data lastObject] 然后 right = [(NSArray)data objectAtIndex:0].

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

这篇关于如何实现一个循环的 UIScrollView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何实现一个循环的 UIScrollView?

基础教程推荐