2

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

就是我正在做的事情。

- (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. }
4

2 回答 2

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 坐标添加相同的检查。

于 2012-10-16T09:24:01.093 回答
0

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

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

于 2012-10-16T09:19:33.987 回答