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

发布于 2024-12-03 19:48:42 字数 1260 浏览 4 评论 0原文

我遇到了以下问题。

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

宛菡 2024-12-10 19:48:42

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

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

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

}

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

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

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

}
揽清风入怀 2024-12-10 19:48:42

斯威夫特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)
    })
 }

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)
    })
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文