获取动画滚动视图的当前位置

phi*_*ipp 14 iphone cocoa-touch uiscrollview uiviewanimation

我遇到了以下问题.

我将一个uiscrollview子类化,其中contentOffset由此代码设置动画:

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

                 }
                 completion:^(BOOL completed) {
                     //some other stuff
                 }];
Run Code Online (Sandbox Code Playgroud)

现在,当用户"willBeginDragging"滚动视图时,我想停止当前动画并将scrollview.contentoffset设置为它在uianimation中的当前位置.

例如,用户进入屏幕,1秒后滚动视图开始滚动,如果用户在2秒后触摸滚动并拖动它,则scrollview.contentoffset应该具有当前动画位置的确切位置.

如果我在willbegindrag方法中获得内容偏移值,它已经作为动画的最后一个值,因为uianimation将值设置为结束状态然后设置动画.

如何从正在运行的动画中获取内容偏移的当前位置?

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

我已经阅读了关于表示层,但视图本身没有动画,我需要scrollview.contentoffset.

嗯,有什么想法吗?谢谢

已经解决:

self.layer presentationLayer绝对正确.

但我不得不使用边界而不是框架....

对不起,现在你和我知道了:P

所以正确的代码是:

CGPoint pos = [[self.layer presentationLayer] bounds].origin;
Run Code Online (Sandbox Code Playgroud)

在动画制作时获取动画滚动视图的当前位置.

Boo*_*Boy 10

您可以使用以下代码在ScrollView滚动时获取内容偏移量...

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

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

}
Run Code Online (Sandbox Code Playgroud)

  • 这个答案是题外话。问题是关于获取 contentOffset 中间动画。此解决方案在每次滚动调用时获取 contentOffset。 (2认同)