【问题标题】:iOS - Add multiple UIButton inside UIScrollView not scrolling smoothlyiOS - 在 UIScrollView 内添加多个 UIButton 滚动不顺畅
【发布时间】:2012-10-06 07:54:04
【问题描述】:

我在 UIScrollview 中添加 (400+) UIButtons,当我在 simulator 中滚动时,它运行良好,但在 iPad 中滚动缓慢。

Here's我在做什么。

- (void)viewDidLoad {
[scrollView setContentSize:CGSizeMake(180 * 24 , 22 * 40 + 200)];
for (int e=0; e<12; e++) {
    for(int i=0;i<24;i++)
    {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.titleLabel.font = [UIFont systemFontOfSize:12.0f];
        [btn setBackgroundColor:[UIColor colorWithRed:248.0/255.0 green:248.0/255.0 blue:248.0/255.0 alpha:1]];


        CALayer *layer = btn.layer;
        layer.cornerRadius = 5.f;
        layer.masksToBounds = YES;
        layer.borderWidth = 1.f;
        layer.borderColor = [[UIColor colorWithRed:190.0/255.0 green:190.0/255.0 blue:190.0/255.0 alpha:1] CGColor];

        CGRect rect = btn.frame;
        rect.size.height = 40;
        rect.size.width = 50;
        rect.origin.x =  100 * i;
        rect.origin.y = 40 * e;
        btn.frame = rect;

        CAGradientLayer *shineLayer = [CAGradientLayer layer];
        shineLayer.frame = layer.bounds;
        shineLayer.colors = [NSArray arrayWithObjects:
                             (id)[UIColor colorWithWhite:0.9f alpha:0.5f].CGColor,
                             (id)[UIColor colorWithWhite:0.9f alpha:0.7f].CGColor,
                             (id)[UIColor colorWithWhite:0.9f alpha:1.f].CGColor,
                             (id)[UIColor colorWithWhite:0.9f alpha:1.f].CGColor,
                             (id)[UIColor colorWithWhite:0.9f alpha:1.f].CGColor,nil];
        [layer addSublayer:shineLayer];

        UILabel *lblDes = [[UILabel alloc] initWithFrame:CGRectMake(0,0,50,40)];
        lblDes.backgroundColor = [UIColor clearColor];
        lblDes.numberOfLines = 3;
        lblDes.textColor = [UIColor colorWithRed:4.0/255.0 green:106.0/255.0 blue:200.0/255.0 alpha:1];
        lblDes.textAlignment = UITextAlignmentCenter;
        lblDes.text = @"adf";
        lblDes.tag = -1;
        lblDes.font = [UIFont systemFontOfSize:11.f];

        [btn addSubview:lblDes];
        [scrollView addSubview:btn];
    }
}
[super viewDidLoad];  // Do any additional setup after loading the view, typically from a nib. }

【问题讨论】:

  • iOS模拟器比实际设备快,因为它使用计算机资源。
  • 就像 Jossef 解释的那样,您无法将模拟器的速度与设备的速度进行比较。模拟器没有放慢速度来模拟ipad!因此,在设备上测试所有内容非常重要。如果真的有必要在屏幕上添加 400 多个 UIButton,我真的很感兴趣,这背后的情况是什么?
  • 谢谢我看到了,但我不知道该怎么做?

标签: ios uiscrollview uibutton smooth-scrolling


【解决方案1】:

iOS 模拟器在 Mac 上运行,比实际的 Device 快很多。

这完全取决于您的要求(为什么需要这么多按钮),

我建议添加一个 UIView 作为子视图,然后在 drawRect - 绘制必要的东西。然后你可以使用手势识别器来找出用户点击的位置。 (根据 tdubik 的建议)

其他解决方案可能是 tableView。 (可以重复使用单元格。您可以为每个单元格添加一个按钮)。

如果 tableView 不是您要查找的内容,并且 drawRect 过于简单(因为它将没有选择效果) - 您可以有一个隐藏的 UIButton,然后可以将其(处于突出显示状态)相应地放置在用户点击的位置,并在用户从屏幕上移除触摸时移除。

编辑

使用 drawrect 时 - 然后您可以为使用 drawrect 绘制元素的子视图添加手势识别器:

CustomViewClass *mView = [CustomViewClass alloc] init];

...

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]
    initWithTarget:self action:@selector(handleTap:)];

singleTap.delegate = self;

singleTap.numberOfTapsRequired = 1;

singleTap.numberOfTouchesRequired = 1;

[mView addGestureRecognizer:singleTap];

然后:

- (void)handleTap:(UITapGestureRecognizer *)tap
{
    CGPoint mPoint = [tap locationInView:tap.view];

    int mOffset = 0; //beginning offset where buttons will start.

    for(int i = 0; i < 20; i++) //how many rows of buttons? 20? 
    { 
        mOffset += 20; //your custom drawn button height + offset from previous button

        if(mPoint.y < mOffset) //now check - if y tapped position is lower than current button.y + its height, then we know what button we tapped.
        {
            //do something - we found button row!!
            break;
        }
    }

    mOffset = 0; //beginning x offset

    for(int i = 0; i < 20; i++) //how many columns buttons? 20? 
    { 
        mOffset += 20; //your custom drawn button width + offset from previous button

        if(mPoint.x < mOffset) //now check - if x tapped position is lower than current button.x + its width, then we know what button we tapped.
        {
            //do something - we found button column!! at this point we know which button was tapped.
            break;
        }
    }
}

如果您有二维按钮数组,请添加相同的 x 坐标检查。

【讨论】:

  • 我使用 drawRect 绘制一个矩形,并希望在其上创建事件 [button addTarget: self action: @selector (btnPlay) forControlEvents: UIControlEventTouchUpInside];怎么做
  • CGContextRef 上下文 = UIGraphicsGetCurrentContext(); CGContextSetLineWidth(上下文, 2.0); CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:190.0/255.0 green:190.0/255.0 blue:190.0/255.0 alpha:1].CGColor); CGRect 矩形 = CGRectMake(50,0,60 ,40); CGContextAddRect(上下文,矩形); CGContextStrokePath(上下文);
  • 您是否只对在相应的“按钮”点击上调用函数感兴趣?
  • 点击并绘制矩形按钮。
  • 亲爱的 Treulands,你能支持这个问题吗.. 这是给我的代码 -> mediafire.com/?t5xj3nz9yyr2d8d
【解决方案2】:

在仪器中分析您的应用程序,以确保按钮是迟缓的原因。

而不是 UIButtons 创建 UIView 派生类安装了手势识别器或只是 UIControl。最好的方法是使用 drawRect 方法绘制视图。

【讨论】:

  • 我不明白你的意思,你能给我举个例子来提高ipad的滚动速度
  • 按照 tdubik 的建议。您应该分析您的应用程序。但是,它在您的代码中清楚地看到它没有正确管理内存。在将btn 添加到scrollview 之后,您应该写[lblDes release]; btn = nil;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-25
  • 2016-01-13
  • 2013-03-26
  • 2013-08-05
  • 2012-04-08
相关资源
最近更新 更多