joh*_*ike 2 objective-c uibutton uiscrollview ios
我已经使用按钮实现了以下水平滚动,但它似乎只在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];
Run Code Online (Sandbox Code Playgroud)

有没有办法使它顺利?
请检查此代码:
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
int x = 0;
CGRect frame;
for (int i = 0; i < 10; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
if (i == 0) {
frame = CGRectMake(10, 10, 80, 80);
} else {
frame = CGRectMake((i * 80) + (i*20) + 10, 10, 80, 80);
}
button.frame = frame;
[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];
if (i == 9) {
x = CGRectGetMaxX(button.frame);
}
}
scrollView.contentSize = CGSizeMake(x, scrollView.frame.size.height);
scrollView.backgroundColor = [UIColor redColor];
[self.view addSubview:scrollView];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6767 次 |
| 最近记录: |