按钮点击时滚动视图向上和向下滚动

3

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

目前,我正在开发一个应用程序,其中我有一个UIScrollView,并且一些按钮是通过编程方式插入到scroll view中的。添加的这些按钮的滚动对我来说很好用。但是我想再添加两个按钮,当我点击一个按钮时,scroll view scrolling到底部并显示底部最后的按钮;当我点击第二个按钮时,它会向上滚动并显示最顶部的按钮。我尝试了很多次,但没有成功。我该怎么解决?提前致谢。


1
在按钮点击时,您可以设置内容偏移量。 - Virja Rahul
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];

如果已知滚动大小,则可以设置按钮点击事件。
-(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];
    }
}

3
这可能解决您的问题。
-(IBAction) clickfirstbutton 
{
   [scrollview setContentOffset:CGPointMake(0, (intvalue to the positon of two buttons)) animated:TRUE];
}

-(IBAction) clicksecondbutton
{
   [scrollview setContentOffset:CGPointMake(0, 0) animated:TRUE];
}

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];
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接