問題描述
如何實現一個循環(huán)的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];
這樣您就可以始終查看同一個實際頁面,每側各有一個頁面.當滾動發(fā)生時,看起來用戶可以繼續(xù)循環(huán)滾動 - 但是在每個頁面結束后,視圖會被打亂,滾動視圖中的位置會被設置回中間,并且用戶可以再次滾動.
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]
.
這篇關于如何實現一個循環(huán)的 UIScrollView?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!