Get current position of UIScrollView
Asked Answered
J

3

9

I'm coming from Android and i'm getting a lot of headache in IOS. I need to make a scroll menu like a movie credits. I used the code below:

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++;
}

This code works fine, but when the user interact scrolling up or down, the view return to position before (value of rol). The question is, how can i get the current content position after scrolling

i already tried these codes, but no one works:

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

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

Anyone can help me?

Jalisajalisco answered 22/8, 2014 at 16:50 Comment(0)
J
6

The solution was:

//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];
}
Jalisajalisco answered 1/9, 2014 at 16:56 Comment(0)
P
9

In timer_rol:

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

Or, if you don't want the X-scroll to change,

[scroll_view setContentOffset:
    CGPointMake(scroll_view.contentOffset.x, scroll_view.contentOffset.y + 1) 
    animated:YES];
Pskov answered 22/8, 2014 at 16:58 Comment(1)
Ty @Gutblender, your solution gave me the way to solve this. I updated the question with the answer.Bruce
J
6

The solution was:

//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];
}
Jalisajalisco answered 1/9, 2014 at 16:56 Comment(0)
H
1

Your timer will continue to run after they scroll, so you should probably call [timer invalidate]; when they start scrolling and then initialize the timer again after they stop scrolling. (also set rol to the new contentOffset)

Harlie answered 22/8, 2014 at 22:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.