滚动视图向上滚动&点击按钮

时间:2012-11-07 09:02:11

标签: iphone ios xcode ios6 uiscrollview

  

可能重复:
  Programmatically scroll a UIScrollView

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

按钮,当我点击第二个按钮时,它会滚动到顶部,并显示最顶部的按钮。我已经尝试了这么多,但不能成功。我怎么解决这个问题?提前谢谢。

3 个答案:

答案 0 :(得分: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];
    }
}

答案 1 :(得分: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];
}

答案 2 :(得分: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];
}