【发布时间】:2011-03-26 16:59:30
【问题描述】:
我正在尝试设置一个无限(水平)滚动的滚动视图。
向前滚动很容易——我已经实现了 scrollViewDidScroll,当 contentOffset 接近尾声时,我将滚动视图的 contentsize 变大并在空间中添加更多数据(我将不得不处理稍后会产生的严重影响! )
我的问题是向后滚动 - 计划是看看我何时接近滚动视图的开头,然后当我确实使 contentsize 更大时,移动现有内容,将新数据添加到开头,然后 -重要的是调整 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 = YES;
mainScrollView.contentSize = CGSizeMake(mainScrollView.contentSize.width + 3200, mainScrollView.contentSize.height);
mainScrollView.contentOffset = CGPointMake(mainScrollView.contentOffset.x + 3200, 0);
for (UIView *view in [mainContainerView subviews]) {
view.frame = CGRectMake(view.frame.origin.x+3200, view.frame.origin.y, view.frame.size.width, view.frame.size.height);
}
//add new data here at (0, 0);
}
//** MY CHECK!
NSLog(@"%f", mainScrollView.contentOffset.x);
}
随着滚动的发生,日志显示: 1286.500000 1285.500000 1284.500000 1283.500000 1282.500000 1281.500000 1280.500000
然后,当 pageNumber
太棒了! - 但数字应该会在 4,000 秒内继续下降,但下一个日志条目显示: 1278.000000 1277.000000 1276.500000 1275.500000 等等……
从中断的地方继续!
仅作记录,如果滚动缓慢,日志会显示: 1294.500000 1290.000000 1284.500000 1280.500000 4476.000000 4476.000000 4473.000000 4470.000000 4467.500000 4464.000000 4460.500000 4457.500000 等等……
有什么想法吗????
谢谢
本。
【问题讨论】:
标签: iphone uiscrollview