swift - 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("")
})
这仅部分按预期工作。动画块内的所有内容都已执行,但完成块并没有让我感到困惑。这是一个错误吗?当然,这不可能是设计使然。
解决方案
在检查完成块内容的期望之前,尝试强制执行额外的运行循环。
RunLoop.current.run(until: Date())
Apple doc:如果动画的持续时间为 0,则在下一个 run loop 循环开始时执行此块。
推荐阅读
- php - 子查询或 sum 内的 sum。这是可能的?
- excel - 使用 VBA 宏将文本 (csv) 文件导入 Excel 表
- java - 无法初始化 Mockito
- node.js - React 未定义,我有一个错误连接
- python - Python 不同长度、平均值和标准差的多个列表
- android - 如何检查用户是否购买了 IAP 提供的产品
- vue.js - 将事件从一个组件传递到其他组件
- android - JSON 对象成为另一个 JSON 对象的键
- git - git diff - 将索引与 HEAD 进行比较?暂存区和 HEAD 呢?
- python - 尝试拆分字符串,但我不断收到此错误:“类型错误:必须是 str 或 None,而不是列表”