首页 > 解决方案 > UIView setAnimationsEnabled 完成块

问题描述

我正在为 ViewController 编写单元测试,并在测试中使用 UIView.setAnimationsEnabled(false) 来禁用动画,以便立即执行以下代码。

UIView.animate(withDuration: 0.3, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 3.0, options: .curveEaseInOut, animations: {
        self.neueUnterhaltungButton.alpha = 0.0
        self.neueUnterhaltungButton.isEnabled = false
        self.plusButton.isEnabled = false
        self.plusButton.alpha = 0.0
        self.okButtonTopConstraint.constant = self.okButtonTopConstraintConstant!
        self.tableViewTopConstraint.constant = self.tableViewTopConstraintExpandedConstant!
        self.view.layoutIfNeeded()
    }, completion: { success in
        self.neueUnterhaltungSegmentedControl.selectIndex(0)
        self.teilnehmerSuchen("")
    })

这仅部分按预期工作。动画块内的所有内容都已执行,但完成块并没有让我感到困惑。这是一个错误吗?当然,这不可能是设计使然。

标签: swiftunit-testing

解决方案


在检查完成块内容的期望之前,尝试强制执行额外的运行循环。

RunLoop.current.run(until: Date())

Apple doc:如果动画的持续时间为 0,则在下一个 run loop 循环开始时执行此块。


推荐阅读