Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current position of animated scrollview

i got the following problem.

i subclassed a uiscrollview which contentOffset is animated by this code:

[UIView animateWithDuration:1.0 
                      delay:1.0
                    options:options
                 animations:^{
                     self.contentOffset = CGPointMake(label.frame.size.width, 0);

                 }
                 completion:^(BOOL completed) {
                     //some other stuff
                 }];

Now, when the user "willBeginDragging" the scrollview, i want to stop the current animation and set the scrollview.contentoffset to the current position it has in the uianimation.

for example, the user gets into the screen, after 1 second the scroll view starts scrolling, if the user touches the scroll after 2 seconds and drag it, the scrollview.contentoffset should have the exact position of the current animation position.

if i get the content offset value in the willbegindrag method, it already as the last value of the animation, as uianimation sets the values to the end state and then animate.

how do i get the current position of the content offset from the running animation?

NSLog(@"pos of content x %f, y %f", [[self.layer presentationLayer] frame].origin.x, [[self.layer presentationLayer] frame].origin.y);

i`ve read about the presentation layer, but the view itself is not animated, i need the scrollview.contentoffset.

mhh, any ideas? thx

ALREADY SOLVED:

The self.layer presentationLayer is absolutely correct.

But i had to use the bounds and not the frame....

Sorry, now you and i know it :P

So the correct code is:

CGPoint pos = [[self.layer presentationLayer] bounds].origin;

to get the current position of an animated scrollview while animating.

like image 793
philipp Avatar asked Sep 08 '11 10:09

philipp


1 Answers

You can get the content offset while the ScrollView is scrolling using the following code...

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{

 NSLog(@"%f", scrollView.contentOffset.x);

}
like image 167
stack2012 Avatar answered Oct 10 '22 16:10

stack2012