【问题标题】:When I scroll my UIScrollView, animations that are connected to a timer and animate views inside of the scrollview, suddenly stop ticking当我滚动 UIScrollView 时,连接到计时器的动画和滚动视图内的动画视图突然停止滴答作响
【发布时间】:2021-12-30 12:22:50
【问题描述】:

我有一个包含视图的 UIScrollView,这些视图连接到每 x 秒调用一次函数的计时器。一切正常,直到我开始滚动滚动视图,计时器停止计时,这意味着动画停止发生。我不知道这是否足够清楚,但我会在下面显示一些代码来尝试澄清。

@objc func lowBeatingAnimation(){
        for i in lowWindow{
            let List = i as? [Any] ?? []
            let View = List[0] as! UIView
            let width = List[1] as! NSLayoutConstraint
            let height = List[2] as! NSLayoutConstraint
            let label = List[3] as! UILabel
            
            self.view.layoutIfNeeded()
            
            UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseIn, animations: {
                View.layer.shadowRadius = 50
                width.constant += -20
                height.constant += -20
                label.alpha = 0.65
                View.layer.cornerRadius += -10
                self.view.layoutIfNeeded()
            }, completion: { finished in
                UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseOut, animations: {
                    View.layer.shadowRadius = 10
                    width.constant += 20
                    View.layer.cornerRadius += 10
                    label.alpha = 0.85
                    height.constant += 20
                    self.view.layoutIfNeeded()
                }, completion: { finished in
                
                })
            })
        }
    }

这是我每秒调用的函数。 lowWindow 是一个数组,由以下格式的数组组成:[UIView, NSLayoutConstraint(属于列表的第一个元素),NSLayoutConstraint(也属于列表的第一个元素),UILabel]

lowWindow 中的第一个元素是 UIView,它是滚动视图的子视图,它会导致动画在滚动时停止。

我认为这个问题可以归结为以下问题,虽然我不完全确定:为什么每当编辑滚动视图的位置时外部计时器停止工作?

我还尝试了不同的方法来确定动画视图是滚动视图的直接子视图,还是滚动视图的子视图的子视图。到目前为止没有任何效果。如果您对如何解决此问题有任何想法并愿意分享,我们将不胜感激。谢谢。

【问题讨论】:

    标签: swift timer uiview uiscrollview


    【解决方案1】:

    我通过将计时器添加到 Runloop 解决了这个问题。

    RunLoop.main.add(lowTimer, forMode: .common)
    RunLoop.main.add(mediumTimer, forMode: .common)
    RunLoop.main.add(highTimer, forMode: .common)
    

    【讨论】:

    • RunLoop 将在多种模式下运行。在滚动期间,它会限制处理的内容,以确保滚动尽可能平滑。在这些时间里,默认模式下的事情不会被处理。你上面使用的常用模式包括default和eventTracking,所以会被处理。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-26
    • 1970-01-01
    • 1970-01-01
    • 2013-04-25
    相关资源
    最近更新 更多