Get current position of animated scrollview
Asked Answered
P

2

15

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.

Paten answered 8/9, 2011 at 10:57 Comment(0)
T
8

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

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

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

}
Thickskinned answered 8/9, 2011 at 11:11 Comment(1)
This answer is off-topic. The question is about getting the contentOffset mid-animation. This solution gets contentOffset each scroll call.Scoop
D
0

Swift 5

var timer: Timer?  
UIView.animate(withDuration: 5.0) {
   self.timer = Timer.scheduledTimer(withTimeInterval: 0.05, repeats: true, block: { (timer) in
                let lastOffset = self.telepromterTextView.layer.presentation()?.bounds.origin
                print(lastOffset)
    })
 }
Dudgeon answered 6/10, 2021 at 14:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.