需要示例代码来实现基于分页的UIScrollView [英] Need sample code to implement Paging Based UIScrollView
问题描述
我是iOS开发的新手.谁能给我一个基于分页的scrollview的工作示例项目/教程?当用户向下滚动时,我需要从Web服务在表视图中加载更多数据.我已经花了很多时间,但是找不到适合我需要的东西.任何帮助将不胜感激.
I am new to iOS development. Can anyone give me a working sample project/ tutorial of paging based scrollview? I need to load more data in my table view from a webservice as user scrolls down. I have spent a lot of time, but could not find anything that suits my need. Any help will be greatly appreciated.
我能够找出问题的解决方案.现在,我尝试根据y偏移量来检测上下滚动.问题是它始终将阻力读为向下,即使它是向上的.这就是我试图检测滚动方向的方式:
I was able to figure out the solution of my problem. Now I am trying to detect scroll up and down based on the y offset difference. The problem is it always reads the drag to be downwards even if it is upward. This is how I am trying to detect the scroll direction:
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
CGPoint scrollOffSet = scrollView.contentOffset;
NSLog(@"Current Content offset: , y = %f", scrollOffSet.y);
NSLog(@"Previous offset , y = %f", self.previousYOffset);
if(scrollOffSet.y > self.previousYOffset)
{
self.previousYOffset = scrollOffSet.y;
NSLog(@"Scrolled Down");
self.nextPageNumber++;
//Here I send the request to fetch data for next page number
}
else {
NSLog(@"Scrolled up");
self.previousYOffset = scrollOffSet.y;
}
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
CGPoint scrollOffSet = scrollView.contentOffset;
NSLog(@"Current Content offset: y = %f", scrollOffSet.y);
}
问题是:无论我向上还是向下滚动,它始终显示向下滚动",因此即使不打算请求,它也会继续获取下一页的数据.另外,它在上/下滚动中返回的y偏移位置也非常令人困惑.请指导我如何准确检测向上滚动?
The problem is: either i scroll up or down, it will always display "scrolled down", thus it goes to fetch data for next page number even if it was not intended to request. Plus, the y offset positions it returns on up/down scroll are also very confusing. Please guide me how to accurately detect an upside scroll?
推荐答案
您是指可交换"的 UIScrollView
吗?
分页 UIScrollView
非常简单.首先,请确保将 pagingEnabled
属性设置为 YES
. UIScrollView
将使用它自己的框架作为页面的大小(如视口).
Paging a UIScrollView
is quite easy. First make sure you have the pagingEnabled
property set to YES
. The UIScrollView
will use it's own frame as the size of a page (like a viewport).
使用 contentSize
属性确定可滚动区域
.
示例:
//scrollView is a UIScrollView instance
int pageCount = 3;
scrollView.pagingEnabled = YES;
scrollView.contentSize = CGSizeMake(scrollView.frame.width*pageCount, scrollView.frame.height);
这篇关于需要示例代码来实现基于分页的UIScrollView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!