获取UIScrollView的当前位置

时间:2014-08-22 16:50:08

标签: ios objective-c uiscrollview position contentoffset

我来自Android,我在IOS中遇到了很多麻烦。我需要像电影一样制作滚动菜单。我使用下面的代码:

rol = scroll_view.contentOffset.y;
timer = [NSTimer scheduledTimerWithTimeInterval:.02 target:self selector:@selector(timer_rol) userInfo:nil repeats:YES];

-(void)timer_rol{
    [scroll_view setContentOffset:CGPointMake(0,rol) animated:YES];
rol++;
}

此代码工作正常,但当用户交互向上或向下滚动时,视图返回到之前的位置(rol的值)。问题是,如何在滚动后获得当前内容位置

我已经尝试过这些代码,但没有人工作:

CGPoint point = [scroll_view contentOffset];
rol = r.y  +1;

-(void) handleGesture:(UIGestureRecognizer *) sender{}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {}

任何人都可以帮助我?

3 个答案:

答案 0 :(得分:8)

timer_rol

[scroll_view setContentOffset:
    CGPointMake(0, scroll_view.contentOffset.y + 1) 
    animated:YES];

或者,如果您不想更改X滚动,

[scroll_view setContentOffset:
    CGPointMake(scroll_view.contentOffset.x, scroll_view.contentOffset.y + 1) 
    animated:YES];

答案 1 :(得分:5)

解决方案是:

//Update the value of rol
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    rol_y = scroll_webView.contentOffset.y;
    rol_x = scroll_webView.contentOffset.x;
}

//Call the timer
timer = [NSTimer scheduledTimerWithTimeInterval:.25 target:self selector:@selector(timer_rol) userInfo:nil repeats:YES];

//Scroll the view (Works when i set animated:NO)
- (void)timer_rol{
    [scroll_webView setContentOffset:CGPointMake(rol_x, rol_y + 1) animated:NO];
}

答案 2 :(得分:1)

您的计时器将在滚动后继续运行,因此您应该在开始滚动时调用[timer invalidate];,然后在停止滚动后再次初始化计时器。 (还设置rol到新的contentOffset