重新启动动画iOS Swift

时间:2018-10-28 17:32:24

标签: ios swift animation

动画在我之后停止工作

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

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

<div class="header">
  <div class="header-info">
    <p>
      Hi
    </p>
  </div>
  <div class="header-title">
    <h1>Welcome</h1>
  </div>
  <div class="header-info">
    <p>
      Hi
    </p>
  </div>
</div>

有人可以帮助我吗? 谢谢。

1 个答案:

答案 0 :(得分:4)

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

在viewDidAppear内部,

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

并在您的视图中会消失

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

并将此函数写入您的ViewController中,

@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)
}