問題描述
我正在嘗試在我的 UIScrollView 中進行循環(huán)"滾動,但沒有成功.
I am trying to make "Circular" scrolling in my UIScrollView, but unsuccessful.
我想做什么:如果 uiscrollview 到達結(jié)束,它應該移動開始如果 uiscrollview 在開始并向后移動,它應該移動到結(jié)束
What I want to do: if uiscrollview reaches end, it should move to start if uiscrollview at start and moving back, it should move to end
在我的情況下附加滾動視圖不是好方法(其他方法應該得到頁面ID")
Appending scrollview isn't good way in my situation (other methods should get "page id")
你有什么想法嗎?
推薦答案
我已經(jīng)實現(xiàn)了這個方法,但是它需要啟用分頁.假設您有五個元素 A、B、C、D 和 E
.設置視圖時,將最后一個元素添加到開頭,將第一個元素添加到結(jié)尾,并調(diào)整內(nèi)容偏移量以查看第一個元素,例如 E,[A],B,C,D,E,A代碼>.在 UIScrollViewDelegate 中,檢查用戶是否到達任何一端,并將偏移 without 動畫移動到另一端.
I've implemented this method, but it requires paging enabled. Lets assume you have five elements A,B,C,D and E
. When you set up your view, you add the last element to the beginning and the first element to the end, and adjust the content offset to view the first element, like this E,[A],B,C,D,E,A
. In the UIScrollViewDelegate, check if the user reach any of the ends, and move the offset without animation to the other end.
想象一下 [ ] 表示正在顯示的視圖:
Imagine the [ ] indicates the view being shown:
E,A,B,C,[D],E,A
用戶向右滑動
E,A,B,C,D,[E],A
用戶向右滑動
E,A,B,C,D,E,[A]
然后,自動設置內(nèi)容偏移到第二個元素
Then, automatically set the content offset to the second element
E,[A],B,C,D,E,A
通過這種方式,用戶可以雙向滑動創(chuàng)建無限滾動的錯覺.
This way the user can swipe both ways creating the illusion of an infinite scroll.
E,A,[B],C,D,E,A
更新
我已經(jīng)上傳了該算法的完整實現(xiàn).這是一個非常復雜的類,因為它還具有點擊選擇、無限循環(huán)滾動和單元格重用.您可以按原樣使用代碼,修改它或提取您需要的代碼.最有趣的代碼在 TCHorizo??ntalSelectorView
類中.
Update
I've uploaded a complete implementation of this algorithm. It's a very complicated class, because it also has on-click selection, infinite circular scroll and cell reuse. You can use the code as is, modify it or extract the code that you need. The most interesting code is in the class TCHorizontalSelectorView
.
文件鏈接
盡情享受吧!
UICollectionView
現(xiàn)在是實現(xiàn)此目的的推薦方法,它可用于獲得完全相同的行為.本教程詳細描述了如何去實現(xiàn)它.
UICollectionView
is now the recommended way to achieve this and it can be used to obtain the very same behavior. This tutorial describes in details how to achieve it.
這篇關(guān)于帶有“圓形"的 UIScrollView滾動的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!