我正在尝试设置无限(水平)滚动的滚动视图.
向前滚动很容易 - 我已经实现了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) 我想像那样滚动1 2 3 1 2 3
我有一些按钮假设10我想在无尽的卷轴上显示.
numbercolors=[[NSMutableArray alloc] init];
//total count of array is 49
numbercolors = [NSMutableArray arrayWithObjects:@"25",@"26",@"27",@"28",@"29",@"31",@"32",@"33",@"34",@"35", @"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20",@"21",@"22",@"23",@"24",@"25",@"26",@"27",@"28",@"29",@"30",@"31",@"32",@"33",@"34",@"35", @"0",@"1",@"2",@"3",nil];
int x=2500;
for (NSInteger index = 0; index < [numbercolors count]; index++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(x ,0,29.0,77.0);
button.tag = index;
[button setTitle:[numbercolors objectAtIndex:index] forState:UIControlStateNormal];
[button addTarget:self action:@selector(didTapButton:)
forControlEvents:UIControlEventTouchUpInside];
[coloringScroll addSubview:button];
x=x+70+29;
}
[coloringScroll setContentSize:CGSizeMake(5000+ (29+70)*[numbercolors count], 1)];
[coloringScroll setContentOffset:CGPointMake(2500+(29+70)*11, 0)];
Run Code Online (Sandbox Code Playgroud)
这是我在scrollview上制作butttons的代码.
如何设置 - (void)scrollViewDidEndDecelerating :( UIScrollView*)发送方此方法进行无限滚动.