滚动到UIScrollview的中心点击UIButton以编程方式添加EDITED

时间:2012-05-21 15:35:37

标签: objective-c ios5

我有一个如下所示的UI按钮滚动视图的滚动视图:

-(void) loadCompeticionSlide{

    float x=0;

    for (int i = 0; i < [categoriasArray count]; i++) {

        NSDictionary *categoria = [categoriasArray objectAtIndex:i];

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

        NSString *titleString  = [categoria valueForKey:@"competicion"];  // get button title

        btn.titleLabel.font = [UIFont boldSystemFontOfSize:11.0];

        CGSize fontSize = [titleString sizeWithFont:[UIFont systemFontOfSize:11.0]];

        CGRect currentFrame = btn.frame;

        CGRect buttonFrame = CGRectMake(x, currentFrame.origin.y, fontSize.width + 22.0, fontSize.height + 12.0);

        [btn setFrame:buttonFrame];

        x = x + fontSize.width + 35.0;

        [btn setTitle:titleString forState: UIControlStateNormal];

        int idc = [[categoria valueForKey:@"idc"]intValue];

         [btn addTarget:self action:@selector(cambiarCompeticion:) forControlEvents:UIControlEventTouchUpInside];

         [btn setTag:idc];

        [self.competicionSlide addSubview:btn];

    }

    //[competicionSlide setBackgroundColor:[UIColor whiteColor]];
    competicionSlide.contentSize = CGSizeMake(350,35);
    competicionSlide.layer.cornerRadius = 11;
    competicionSlide.layer.masksToBounds = YES;

}

然后,在添加的选择器cambiarCompeticion:中,我点击了按钮,这里我需要使用scrollRectToVisible:将点击的UIButton滚动到包含它的UIScrollview的中心,但我不知道如何这样做。

这是由按钮选择触发的选择器方法,我理解scrollRectToVisible:必须被调用:

-(void)cambiarCompeticion:(UIButton*)boton{

    int idCompeticion;

    idCompeticion = boton.tag;

    switch (idCompeticion) {
        case 1:
            [self tablaLigaRegular];
            break;

        case 5:
            [self tablaCoparey];
            break;

        case 10:
            [self tablaPlayOff];
            break;    

           }


}

这里是图像中的细节,在第一个图像中,蓝色箭头表示部分隐藏的左按钮的上一个状态,以及单击后滚动视图中间的移动:

enter image description here

非常感谢

1 个答案:

答案 0 :(得分:1)

您的 boton 参数(来自 cambiarCompeticion:选择器)拥有您需要的一切。只需这样调用(假设“competicionSlide”是UIScrollView):

[self.competicionSlide scrollRectToVisible:boton.frame];
祝你好运!