我已经通过按钮实现了以下水平滚动,但似乎只在5秒后开始滚动。它卡住了。我猜,UI正在被阻塞。水平滚动的按钮在iOS中
我有以下代码来创建,在我ViewDidLoad()
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,self.view.frame.size.height-100, self.view.frame.size.width, 100)];
int x = 10;
for (int i = 0; i < 10; i++) {
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x,10,80,80)];
[button setTitle:[NSString stringWithFormat:@"Button %d", i] forState:UIControlStateNormal];
[button setTag:i];
[button setBackgroundColor:[UIColor greenColor]];
[button addTarget:self action:@selector(onClickofFilter:) forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:button];
x += button.frame.size.width+10;
}
scrollView.contentSize = CGSizeMake(x, scrollView.frame.size.height);
scrollView.backgroundColor = [UIColor redColor];
[self.view addSubview:scrollView];
有没有什么办法让它顺利?
您的循环后添加滚动视图,这就是为什么它的延迟,因为for循环,然后完成它的负载,并设置按钮=零,如果弧否则“[按钮释放]”添加按钮 – 2014-10-17 02:38:51
更好的后使用集合视图苹果介绍UICollectionView试试吧 – 2014-10-17 02:39:51
scrollView.contentSize = CGSizeMake(x,scrollView.frame.size.height);这行代码可能是错误的。它看起来应该是scrollView.contentSize = CGSizeMake(buttonWidth * x,buttonHeight + padding); – DBoyer 2014-10-17 02:49:28