我正在尝试设置无限(水平)滚动的滚动视图.
向前滚动很容易 - 我已经实现了scrollViewDidScroll,当contentOffset接近结尾时,我将使scrollview内容更大,并将更多数据添加到空间中(我将不得不处理稍后会产生的严重影响!)
我的问题是向后滚动 - 计划是看到我何时接近滚动视图的开头,然后当我确实使内容更大时,移动现有内容,将新数据添加到开头然后 - 重要的是调整contentOffset使视图端口下的数据保持不变.
如果我慢慢滚动(或启用分页),这可以很好地工作但如果我快速(甚至不是非常快!)它会发疯!下面是代码:
- (void) scrollViewDidScroll:(UIScrollView *)scrollView {
float pageNumber = scrollView.contentOffset.x / 320;
float pageCount = scrollView.contentSize.width / 320;
if (pageNumber > pageCount-4) {
//Add 10 new pages to end
mainScrollView.contentSize = CGSizeMake(mainScrollView.contentSize.width + 3200, mainScrollView.contentSize.height);
//add new data here at (320*pageCount, 0);
}
//*** the problem is here - I use updatingScrollingContent to make sure its only called once (for accurate testing!)
if (pageNumber < 4 && !updatingScrollingContent) {
updatingScrollingContent = …Run Code Online (Sandbox Code Playgroud)