iOS - 在UIScrollView中添加多个UIButton不能平滑滚动

时间:2012-10-16 08:53:17

标签: ios uiscrollview uibutton smooth-scrolling

我在UIButton中添加(400 +)UIScrollview s,当我在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. }

2 个答案:

答案 0 :(得分:1)

iOS模拟器在Mac上运行,比实际设备快得多。

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

我建议添加一个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;
        }
    }
}

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

答案 1 :(得分:0)

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

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