ios - 视图唤醒时 UIView.animate() 冻结
问题描述
我用以下内容无限循环 UIView 动画
UIView.animate(withDuration: 1.0, delay: 0, options: [.autoreverse, .repeat], animations: {
self.someLabel.alpha = 0.3
}, completion: nil)
这很好用,但是当 viewController 唤醒时,动画会冻结在原来的位置。
运行与上面相同的代码viewDidWakeUp()
并不能修复它。
如何使动画不冻结,或者在 viewController 唤醒时从中断处继续。
澄清一下,“醒来”是指以下任何一种:
- 关闭应用程序并再次打开它,此 viewController 处于活动状态,
- 让手机休眠,然后用这个 viewController 唤醒它仍然处于活动状态
解决方案
添加两个通知 willEnterForegroundNotification 和 didEnterBackgroundNotification。
这也值得注意。在某些情况下,您需要重置动画属性以使新动画保持不变。我可以通过动画转换来确认这一点。
只是打电话...
view.layer.removeAllAnimations()
self.someLabel.alpha = 1.0
//完整代码
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
NotificationCenter.default.addObserver(self, selector:#selector(didEnterForeground) , name: UIApplication.willEnterForegroundNotification, object: nil)
NotificationCenter.default.addObserver(self, selector:#selector(didEnterBackground) , name: UIApplication.didEnterBackgroundNotification, object: nil)
}
@objc func didEnterBackground() {
view.layer.removeAllAnimations()
self.someLabel.alpha = 1.0
}
@objc func didEnterForeground() {
DispatchQueue.main.async {
self.animation()
}
}
func animation() {
UIView.animate(withDuration: 1.0, delay: 0, options: [.autoreverse, .repeat], animations: {
self.someLabel.alpha = 0.3
}, completion: nil)
}
推荐阅读
- apache-kafka - 当新消费者连接时,Kafka 消费者会获得很多重播
- c++ - 在 android 上为 sched_setaffinity 唤醒睡眠 CPU
- opengl - GLSL 缓冲区限定符是否仅适用于 OpenGL 4.3 及更高版本
- java - Graphql 在 Postman 中以文本形式发送带有变量的 POST
- android - 为什么库在 Android Studio 项目视图中显示为红色?
- ssl - OpenAS2 Server SSL 配置,在 Windows 上配置和应用证书
- javascript - 按下回车键时调用事件
- python - 如何在不缩进所有代码的情况下以日期为条件执行代码?
- c# - 解密时出现错误“填充无效且无法删除”
- java - Java ArrayList 第一项冲突不起作用,但没有它起作用