問題描述
我有一個 scrollview
,其中有 5 個寬度為 88 的圖像視圖.
I have a scrollview
with 5 image views of width 88.
我希望 scrollview
滾動到每個圖像視圖 (非分頁)
I want the scrollview
to scroll to each image view (Not Paging)
和
我想在 iPhone 中制作一個無限滾動視圖,這意味著當它滾動到最后一個項目時,它會在它旁邊顯示第一個項目..
I want to make an infinite scrollview in iPhone which means the when it scroll to last item it will display a first item next to it..
我一直在嘗試按偏移量,但是當按偏移量移動它時它會停止,而且 我使用了蘋果街道滾動條,它不允許我將每個元素停止在中心(就像選擇器視圖)..>
I have been trying by offset but it stops when move it by offset and also I have used apple street scroller which does not allow me to stop each element in center(just like Picker view)..
推薦答案
首先,我推薦使用 UITableView,這樣可以保持較低的內存使用率.我在一個項目中成功使用的方法如下:
First of all, I recommend to use a UITableView so you can maintain the memory usage low. An approach that I had used successfully in a project is the following:
- 1.列表項
復制單元格的內容,我的意思是如果你有 5 個這樣的項目:
Duplicate the content of your cells, I mean if you have 5 items in this way:
|1 |2 |3 |4 |5 |
| 1 | 2 | 3 | 4 | 5 |
您應該在表格末尾添加相同的內容(在具有可重用引擎的表格視圖中,這不是什么大問題),以便看起來像這樣:
You should add the same (In a table view with reusable engine that's not a big deal) at the end of the table in order to look like this:
|1 |2 |3 |4 |5 |1 |2 |3 |4 |5 |
| 1 | 2 | 3 | 4 | 5 | 1 | 2 | 3 | 4 | 5 |
- 2.修改 scrollViewDidScroll 如下:
- 2. modify the scrollViewDidScroll with the following:
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView == _yourScrollView) {
CGFloat currentOffsetX = scrollView.contentOffset.x;
CGFloat currentOffSetY = scrollView.contentOffset.y;
CGFloat contentHeight = scrollView.contentSize.height;
if (currentOffSetY < (contentHeight / 6.0f)) {
scrollView.contentOffset = CGPointMake(currentOffsetX,(currentOffSetY + (contentHeight/2)));
}
if (currentOffSetY > ((contentHeight * 4)/ 6.0f)) {
scrollView.contentOffset = CGPointMake(currentOffsetX,(currentOffSetY - (contentHeight/2)));
}
}
}
如果您幾乎到達滾動的最后,上面的代碼將滾動位置移動到頂部;或者,如果您幾乎在頂部,則將您移至底部...
The code above move the scroll position at top if you almost reach the final of the scrolling; Or if you are almost on the top, moves you to the bottom...
- 3.就是這樣.
這篇關于如何在 iPhone 中制作無限滾動視圖?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!