可能是重复问题:
以编程方式滚动UIScrollView
目前,我正在开发一个应用程序,其中我有一个UIScrollView
,并且一些按钮是通过编程方式插入到scroll view
中的。添加的这些按钮的滚动对我来说很好用。但是我想再添加两个按钮,当我点击一个按钮时,scroll view scrolling
到底部并显示底部最后的按钮;当我点击第二个按钮时,它会向上滚动并显示最顶部的按钮。我尝试了很多次,但没有成功。我该怎么解决?提前致谢。
可能是重复问题:
以编程方式滚动UIScrollView
目前,我正在开发一个应用程序,其中我有一个UIScrollView
,并且一些按钮是通过编程方式插入到scroll view
中的。添加的这些按钮的滚动对我来说很好用。但是我想再添加两个按钮,当我点击一个按钮时,scroll view scrolling
到底部并显示底部最后的按钮;当我点击第二个按钮时,它会向上滚动并显示最顶部的按钮。我尝试了很多次,但没有成功。我该怎么解决?提前致谢。
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];
}
}
-(IBAction) clickfirstbutton
{
[scrollview setContentOffset:CGPointMake(0, (intvalue to the positon of two buttons)) animated:TRUE];
}
-(IBAction) clicksecondbutton
{
[scrollview setContentOffset:CGPointMake(0, 0) animated:TRUE];
}
-(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];
}