3

可能重复:
以编程方式滚动 UIScrollView

目前,我正在开发一个应用程序,其中UIScrollView一些按钮以编程方式插入scroll view.Scrolling这些添加的按钮对我来说很好。但我想再添加两个按钮并在一个按钮上单击scroll view scrolling底部并最后显示底部

按钮,当我单击第二个按钮时,它会滚动到顶部并显示最顶部的按钮。我已经尝试了很多但无法成功。我该如何解决这个问题?提前致谢。

4

3 回答 3

7
scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 20, 320, 400)];
scrollView.backgroundColor=[UIColor lightGrayColor];
scrollView.contentSize=CGSizeMake(320, 500);
scrollView.showsVerticalScrollIndicator=YES;
[self.view addSubview:scrollView];

如果 Scroll Size 已知,那么我们可以设置按钮单击事件

-(void)buttonCkicked:(UIButton*)sender
{
    if (sender.tag==1001) {
        [scrollView setContentOffset:CGPointMake(0, 100) animated:YES];
    }
    if (sender.tag==1002) {
        [scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    }
}
于 2012-11-07T10:10:27.030 回答
3

这可以解决你的 pbm

-(IBAction) clickfirstbutton 
{
   [scrollview setContentOffset:CGPointMake(0, (intvalue to the positon of two buttons)) animated:TRUE];
}

-(IBAction) clicksecondbutton
{
   [scrollview setContentOffset:CGPointMake(0, 0) animated:TRUE];
}
于 2012-11-07T09:08:24.087 回答
2
-(IBAction)btn1_Clicked:(id)sender{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    yourScrollView.frame=CGRectMake(0, -scrollView.contentSize.height + 360, 320, scrollView.contentSize.height);/// set y (starting point) with your scrollview height
    [UIView commitAnimations];
}

-(IBAction)btn2_Clicked:(id)sender{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    yourScrollView.frame=CGRectMake(0,0 , 320, scrollView.contentSize.height);
    [UIView commitAnimations];
}
于 2012-11-07T09:19:48.193 回答