用于控制滚动视图的按钮

时间:2013-06-14 07:27:24

标签: iphone objective-c uiscrollview

我有一个控制按钮向上和向下滚动的按钮有问题。当我第一次点击按钮时,它工作正常(向上滚动),但第二次滚动视图没有向上滚动。我做错了什么?这是代码:

创建滚动:

scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(38, 5, 90, 280)];
[scrollView setContentSize:CGSizeMake(90, 950)];
[scrollView setScrollEnabled: NO];
[imageView addSubview:scrollView];
[scrollView setShowsHorizontalScrollIndicator:NO];
[scrollView setShowsVerticalScrollIndicator:NO];

按钮操作:

-(IBAction)upButtonPress:(id)sender{

NSLog(@"UP");
[scrollView setContentOffset:CGPointMake(0, self.scrollView.frame.origin.y + 95)];

 }
-(IBAction)downButtonPress:(id)sender{

NSLog(@"DOWN");
[scrollView setContentOffset:CGPointMake(0, self.scrollView.frame.origin.y - 95)];

 }

2 个答案:

答案 0 :(得分:0)

相应地在按钮操作上使用此功能:

[scrollView setContentOffset:CGPointMake(0, Y) animated:YES];

其中Y是scrollView.contentOffset.y + scrollView.contentSize.height的值。

答案 1 :(得分:0)

每次使用滚动视图框架原点的y值。您应该使用scrollView.contentOffset.y代替。

-(IBAction)upButtonPress:(id)sender {
    NSLog(@"UP");
    [scrollView setContentOffset:CGPointMake(0, self.scrollView.contentOffset.y + 95)];
 }

-(IBAction)downButtonPress:(id)sender {
    NSLog(@"DOWN");
    [scrollView setContentOffset:CGPointMake(0, self.scrollView.contentOffset.y - 95)];
}