Ios 获取UIScrollView的当前位置
我来自安卓系统,在IOS上我感到很头疼。我需要制作一个滚动菜单,就像电影字幕一样。我使用了下面的代码:Ios 获取UIScrollView的当前位置,ios,objective-c,uiscrollview,position,contentoffset,Ios,Objective C,Uiscrollview,Position,Contentoffset,我来自安卓系统,在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 setCon
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 {}
有人能帮我吗?在
定时器控制中:
[scroll_view setContentOffset:
CGPointMake(0, scroll_view.contentOffset.y + 1)
animated:YES];
或者,如果您不想更改X-scroll
[scroll_view setContentOffset:
CGPointMake(scroll_view.contentOffset.x, scroll_view.contentOffset.y + 1)
animated:YES];
您的计时器在滚动后将继续运行,因此您可能需要调用[timer invalidate]代码>当它们开始滚动时,然后在停止滚动后再次初始化计时器。(同时将rol设置为新的contentOffset
)解决方案是:
//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];
}
Ty@Gutblender,你的解决方案给了我解决这个问题的方法。我用答案更新了问题。