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

时间:2011-09-08 10:57:51

标签: 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
                 }];

现在,当用户“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);

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

嗯,有什么想法吗? THX

已解决:

self.layer presentationLayer绝对正确。

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

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

所以正确的代码是:

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

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

1 个答案:

答案 0 :(得分:9)

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

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

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

}
相关问题