【发布时间】:2014-12-12 13:31:22
【问题描述】:
我已经实现了带有按钮的水平滚动,但它似乎只在 5 秒后才开始滚动。它卡住了。我猜是 UI 被屏蔽了。
我有以下代码在我的 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 循环之后添加滚动视图,这就是为什么它是延迟的原因,因为 for 循环完成然后加载并设置 button = nil if arc 否则添加按钮后为“[button release]”
-
比较好用的collection view apple是引入UICollectionView试试吧
-
scrollView.contentSize = CGSizeMake(x, scrollView.frame.size.height);这行代码可能是错误的。好像应该是 scrollView.contentSize = CGSizeMake(buttonWidth * x, buttonHeight + padding);
-
我完全忘记了collectionview ....cheee
-
先添加你的scrollView,然后在scollView上添加所有按钮。
标签: ios objective-c uiscrollview uibutton