重启动画ios swift



i 后动画停止工作

  1. 传输到另一个视图控制器并返回
  2. 应用程序进入后台,然后返回前台

我正在使用此代码(在视图中将出现(来实现闪烁动画

UIView.animate(withDuration: 1.0, delay: 0.4, options:[ UIViewAnimationOptions.curveEaseOut , .repeat], animations: {
self.logoLabel1.alpha = 0.0
self.logoLabel2.alpha = 0.0
}, completion: nil)

有人能帮我吗?谢谢

是的,这是预期的行为。当视图消失时,通过最小化应用程序或显示另一个视图控制器,动画将停止。将动画代码移动到viewDidAppear,当您移动到任何其他视图控制器并返回时,动画不会停止。为了处理当应用程序进入后台时动画停止的情况,请使用以下代码:

在您的视图中显示,

NotificationCenter.default.addObserver(self, selector: #selector(willEnterForeground), name: .UIApplicationWillEnterForeground, object: nil)

在你看来WillDisappear,

NotificationCenter.default.removeObserver(self, name: .UIApplicationWillEnterForeground, object: nil)

并在您的视图控制器中编写此函数

@objc func willEnterForeground() {
// your animations
UIView.animate(withDuration: 1.0, delay: 0.4, options:[ UIViewAnimationOptions.curveEaseOut , .repeat], animations: {
self.logoLabel1.alpha = 0.0
self.logoLabel2.alpha = 0.0
}, completion: nil)
}

最新更新