【问题标题】:Scroll view scrolling up & down on button click [duplicate]滚动视图在按钮单击时向上和向下滚动[重复]
【发布时间】:2012-10-27 07:22:38
【问题描述】:
可能重复:
Programmatically scroll a UIScrollView
目前,我正在开发一个应用程序,其中我有UIScrollView,并且一些按钮以编程方式插入scroll view.Scrolling,这些添加的按钮对我来说很好。但我想加两个
更多按钮和一个按钮单击scroll view scrolling到底部并最后显示底部
按钮,当我单击第二个按钮时,它会滚动到顶部并显示最顶部的按钮。我已经尝试了很多但无法成功。我该如何解决这个问题?提前致谢。
【问题讨论】:
标签:
iphone
ios
xcode
ios6
uiscrollview
【解决方案1】:
这可能会解决你的 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];
}
【解决方案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];
}
【解决方案3】:
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];
}
}