获取动画滚动视图的当前位置
我遇到了以下问题。
我对 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.layerpresentationLayer 是绝对正确的。
但我必须使用边界而不是框架......
抱歉,现在你和我都知道了:P
所以正确的代码是:
CGPoint pos = [[self.layer presentationLayer] bounds].origin;
在动画时获取动画滚动视图的当前位置。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用以下代码在 ScrollView 滚动时获取内容偏移量...
You can get the content offset while the ScrollView is scrolling using the following code...
斯威夫特5
Swift 5