Objective c 滚动到UIScrollview的中心单击UIButton以编程方式添加并编辑

Objective c 滚动到UIScrollview的中心单击UIButton以编程方式添加并编辑,objective-c,ios5,Objective C,Ios5,我有一个UIbuttons的scrollview构建如下: -(void) loadCompeticionSlide{ float x=0; for (int i = 0; i < [categoriasArray count]; i++) { NSDictionary *categoria = [categoriasArray objectAtIndex:i]; UIButton *btn = [UIButton buttonWit

我有一个UIbuttons的scrollview构建如下:

-(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;

}
以下是图像中的详细信息,在第一个图像中,蓝色箭头指示部分隐藏的左按钮的先前状态,以及单击后移动到滚动视图中间的操作:


非常感谢

您的boton参数(来自CambiarCompetition:selector)具有您所需的一切。只需这样调用(假设“CompetitionSlide”是一个UIScrollView):


祝你好运

谢谢黑暗势力,我试过了,但卷轴不动了。有什么建议吗?我重读了你的问题,我不太确定你需要什么。几个问题:您的所有按钮都可见(无需滚动查看所有按钮)?当您说希望UIButton“滚动到ScrollView的中心”时,您的确切意思是什么?是否要将按钮设置为另一个位置的动画。。。或者滚动整个滚动视图?不,它们不是全部可见。我的意思是,一旦用户点击了,一个按钮就在可见滚动视图的右边,ScLoVIEW会滚动并在滚动视图中间滚动按钮。同样的if按钮在左侧。我将在稍后添加一个解释图像。谢谢我刚刚注意到您的代码中有一些东西:competitionslide.contentSize=CGSizeMake(350,35);您的滚动视图宽度不应该更大吗?按钮的组合宽度、按钮之间的空间以及左右两侧的一些空间?试试这个,然后看看是否
scrollRectToVisible:
移动了ScrollView。我已经更改了它,但是你的建议响了,我尝试滚动到中间的按钮没有隐藏,所以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;    

           }


}
[self.competicionSlide scrollRectToVisible:boton.frame];