【问题标题】:UIScrollView scrollRectToVisible:animated: is there a way that a method can be called when animation endsUIScrollView scrollRectToVisible:animated: 有没有办法在动画结束时调用方法
【发布时间】:2012-02-26 18:14:00
【问题描述】:

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

【问题讨论】:

  • 关于这个非常古老的问题,现在有三种情况必须涵盖。这是 iOS 中最奇怪和最困难的问题之一。 ://

标签: ios uiscrollview uiscrollviewdelegate


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    【讨论】:

    • 不会每次都调用它。当在 traitCollat​​ionDidChange 中调用 scrollRectToVisible()
    • 这是错误的,需要涵盖三种不同的情况。
    【解决方案3】:

    通过以下方式为您的 UIScrollView 实现 UIScrollViewDelegate 委托方法:

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

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

    【讨论】:

    • 对于任何在这里搜索的人,这个非常古老的答案现在不正确 - 谢谢,Apple! ://
    【解决方案4】:

    我这样做是因为有时使用委托对我来说并不实用,比如我在 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
    }];
    

    【讨论】:

    • 没错!我也使用了相同的代码,然后它按预期工作。
    • 我将单元格修改代码放入completion,但我的animationcompletion 正在逐行执行。没有按预期工作:(
    • 不幸的是,这在很多情况下都会崩溃。
    【解决方案5】:

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

    // 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() 中,做你需要的。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多