gpt4 book ai didi

ios - iOS 中带有按钮的水平滚动

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:07:07 28 4
gpt4 key购买 nike

我已经实现了带有按钮的水平滚动,但它似乎只在 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];

Image showing a row of green buttons on a red background.

有什么办法可以让它顺畅吗?

最佳答案

请检查此代码:

  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];

关于ios - iOS 中带有按钮的水平滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26416995/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com