Ios UIView.animate()在视图唤醒时冻结

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动画,如下所示

UIView.animate(withDuration: 1.0, delay: 0, options: [.autoreverse, .repeat], animations: {
        self.someLabel.alpha = 0.3
    }, completion: nil)
这可以正常工作,但当viewController唤醒时,动画将冻结在其所在的位置

viewDidWakeUp()
中运行与上述相同的代码并不能解决此问题

如何使动画不冻结,或在viewController唤醒时继续其停止的位置

为了澄清,我所说的“唤醒”是指以下任一情况:

  • 关闭应用程序并再次打开,同时此viewController处于活动状态
  • 休眠手机,然后在此viewController仍处于活动状态时将其唤醒

添加两个通知,分别为EnterForeGroundNotification和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)
}

你把你的动画放在哪里了?你有没有试着在“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)}