开发者

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.


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

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

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

}


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)
    })
 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜