Ios UIView.animate()在视图唤醒时冻结
我无限循环一个UIView动画,如下所示Ios UIView.animate()在视图唤醒时冻结,ios,swift,uiviewanimation,Ios,Swift,Uiviewanimation,我无限循环一个UIView动画,如下所示 UIView.animate(withDuration: 1.0, delay: 0, options: [.autoreverse, .repeat], animations: { self.someLabel.alpha = 0.3 }, completion: nil) 这可以正常工作,但当viewController唤醒时,动画将冻结在其所在的位置 在viewDidWakeUp()中运行与上述相同的代码并不能解决此问题
UIView.animate(withDuration: 1.0, delay: 0, options: [.autoreverse, .repeat], animations: {
self.someLabel.alpha = 0.3
}, completion: nil)
这可以正常工作,但当viewController唤醒时,动画将冻结在其所在的位置
在viewDidWakeUp()
中运行与上述相同的代码并不能解决此问题
如何使动画不冻结,或在viewController唤醒时继续其停止的位置
为了澄清,我所说的“唤醒”是指以下任一情况:
- 关闭应用程序并再次打开,同时此viewController处于活动状态
- 休眠手机,然后在此viewController仍处于活动状态时将其唤醒
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)
}
你把你的动画放在哪里了?你有没有试着在“viewDidAppear”节目中插播?非常感谢!对于其他有同样问题的人,我决定让我的“动画”方法为我重置。例如,func animation(){self.view.layer.removeAllAnimations()self.view.alpha=1.0 UIView.animate(持续时间:1.0,延迟:0,选项:[.autoreverse,.repeat],动画:{self.someLabel.alpha=0.3},完成:nil)}