Get current position of animated scrollview

僤鯓⒐⒋嵵緔 提交于 2019-12-23 06:59:20

问题


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.


回答1:


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

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

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

}


来源:https://stackoverflow.com/questions/7346952/get-current-position-of-animated-scrollview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!