使Uiscrollview移动到选定区域

时间:2013-04-23 04:59:54

标签: ios

您好我遇到了一个简单的问题,我已经添加了一个uiscollview来查看并从一个数组中动态添加按钮,我将它们从superview中删除,并再次将它们添加到视图中,然后在代码中进行查看

- (void)loadGroups {

    if(scroll!=nil){
        if([scroll isDescendantOfView:self.view]){

            [scroll removeFromSuperview];

        }
    }
     NSUInteger n=[self.resultSet.dataObjectList count];
    scroll = [[UIScrollView alloc]init];
    scroll.delegate=self;
    scroll.frame = CGRectMake(0, 0, 320, (height+25));
    [scroll setContentSize:CGSizeMake((width-30)*n, height+25)];
    [scroll setContentOffset:CGPointMake(0, 0) animated:NO];
    scroll.backgroundColor=[UIColor clearColor];
    //scroll.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"GP-BG.png"]];
    [scroll setCanCancelContentTouches:NO];
    scroll.indicatorStyle=UIScrollViewIndicatorStyleWhite;
    scroll.bounces=NO;
    scroll.scrollEnabled=YES;
    scroll.pagingEnabled=NO;
    scroll.showsHorizontalScrollIndicator=NO;
    for(int i=0;i<n;i++)
    {
        MODELRoom *room = [self.resultSet.dataObjectList objectAtIndex:i];
        CGRect rectForTitleButton = CGRectMake(i*(width-30), 0, width-30, height+25);
        int buttonTag =10000+i;
        UIButton *titleButton = [self getButton:room tag:buttonTag ];
        titleButton.tag=buttonTag;
        [titleButton setFrame:rectForTitleButton];
        titleButton.backgroundColor=[UIColor clearColor];
        [scroll addSubview:titleButton];

        CGRect rectForLabel = CGRectMake(i*(width-30), padding-5, width-35, height-20);
        UILabel *label = [[UILabel alloc]init ];
        label.frame=rectForLabel;
        MODELRoom *room1 = [self.resultSet.dataObjectList objectAtIndex:i];
        label.text=(room1).name;
        label.textColor=[UIColor whiteColor];
        label.textAlignment=NSTextAlignmentCenter;
        label.backgroundColor=[UIColor clearColor];
        label.font = [UIFont fontWithName:@"Helvetica" size:14];
        [scroll addSubview:label];


        CGRect rectForImageView = CGRectMake(27.5+(width-30)*i, padding+25, width-90, height-20);
        UIImageView *imageView = [[UIImageView alloc]init ];
        imageView.frame=rectForImageView;
        imageView.backgroundColor=[UIColor clearColor];
        imageView.image=[UIImage imageNamed:@"Group-icon.png"];
        [scroll addSubview:imageView];

    }
    [self.view addSubview:scroll];

    }

我的问题是,当我在滚动后选择一个按钮时,它会重新调整到第一个按钮,但我需要在那里选择按钮。请帮助我,我做错了...

提前感谢....

2 个答案:

答案 0 :(得分:1)

尝试使用这个我希望这可以正常工作......

[scroll setContentOffset:CGPointMake(titleButton.frame.origin.x, titleButton.frame.origin.y) animated:YES];

答案 1 :(得分:0)

一旦看到这个,

每当scollview contentOffset值为(0,0)时,尝试更改其值,那么您将得到您的要求,

[scroll setContentOffset:CGPointMake(x-value, y-value) animated:NO];