UIScrollView scrollRectToVisible:animated: 有没有一种方法可以在动画结束时调用方法

发布于 2025-01-03 00:16:39 字数 41 浏览 4 评论 0原文

有没有办法知道动画何时结束并且 uiscrollview 何时停止。

Is there a way to know when the animation has end and uiscrollview has come to rest.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

这样的小城市 2025-01-10 00:16:39

我这样做是因为有时使用委托对我来说并不实际,就像我在 UIViewController 转换中这样做一样:

[UIView animateWithDuration:0.3 animations:^{
    [scrollView setContentOffset:CGPointMake(0, -scrollView.contentInset.top) animated:NO];
} completion:^(BOOL finished) {
    // This is called when it's complete
}];

I do it like this because sometimes using the delegate isn't practical for me, like if i'm doing it in UIViewController transition:

[UIView animateWithDuration:0.3 animations:^{
    [scrollView setContentOffset:CGPointMake(0, -scrollView.contentInset.top) animated:NO];
} completion:^(BOOL finished) {
    // This is called when it's complete
}];
落墨 2025-01-10 00:16:39

实现 UIScrollViewDelegate 的委托方法UIScrollView 的方式如下:

当您通过调用 setContentOffset:animated:scrollRectToVisible:animated: 启动滚动时,使用 scrollViewDidEndScrollingAnimation: 来检测滚动动画何时结束: 方法(带有动画:YES)。

如果要监视由触摸手势启动的滚动视图运动,请使用 scrollViewDidEndDecelerating: 方法,该方法在滚动运动停止时调用。

Implement UIScrollViewDelegate delegate methods for your UIScrollView the following way:

Use scrollViewDidEndScrollingAnimation: to detect when the scrolling animation concludes when you've initiated the scrolling by calling setContentOffset:animated: or scrollRectToVisible:animated: methods (with animated:YES).

If you want to monitor scroll view motion that's been initiated by touch gestures, use scrollViewDidEndDecelerating: method, which is called when the scrolling movement comes to a halt.

分開簡單 2025-01-10 00:16:39

您需要涵盖三种(!)情况。谢谢,苹果。

// do note that you need all three of the following

public func scrollViewDidEndScrollingAnimation(_ s: UIScrollView) {
    // covers case setContentOffset/scrollRectToVisible
    fingerOrProgrammaticMoveDone()
}

public func scrollViewDidEndDragging(_ s: UIScrollView, willDecelerate d: Bool) {
    if decelerate == false {
        // covers certain cases of user finger
        fingerOrProgrammaticMoveDone()
    }
}

public func scrollViewDidEndDecelerating(_ s: UIScrollView) {
    // covers certain cases of user finger
    fingerOrProgrammaticMoveDone()
}

(小心不要忘记中间的额外的“if”子句。)

然后在 fingerOrProgrammaticMoveDone() 中,执行您需要的操作。

一个很好的例子就是处理分页滚动的噩梦。很难知道你在哪一页。

You need to cover THREE (!) cases. Thanks, Apple.

// do note that you need all three of the following

public func scrollViewDidEndScrollingAnimation(_ s: UIScrollView) {
    // covers case setContentOffset/scrollRectToVisible
    fingerOrProgrammaticMoveDone()
}

public func scrollViewDidEndDragging(_ s: UIScrollView, willDecelerate d: Bool) {
    if decelerate == false {
        // covers certain cases of user finger
        fingerOrProgrammaticMoveDone()
    }
}

public func scrollViewDidEndDecelerating(_ s: UIScrollView) {
    // covers certain cases of user finger
    fingerOrProgrammaticMoveDone()
}

(Be careful to not forget the extra "if" clause in the middle one.)

Then in fingerOrProgrammaticMoveDone() , do what you need.

A good example of this is the nightmare of handling paged scrolling. It is very, very hared to know what page you are on.

喜爱皱眉﹌ 2025-01-10 00:16:39

scrollViewDidEndDecelerating:当scrollView完全停止时调用UIScrollView委托方法。

scrollViewDidEndDecelerating: UIScrollView delegate method is called when scrollView stops completely.

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