在iOS中按钮的水平滚动 [英] Horizontal scroll with buttons in iOS

查看:131
本文介绍了在iOS中按钮的水平滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用按钮实现了以下水平滚动,但似乎它仅在5秒后开始滚动。它卡住了。我想是用户界面被阻止了。

I have implemented following horizontal scroll with buttons, but it seems like it starts scrolling only after 5 seconds. It got stuck. UI is getting blocked, I guess.

我有以下代码在我的 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];

有没有办法让它变得流畅?

Is there any way to make it smooth?

推荐答案

请检查此代码:

  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中按钮的水平滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆