简体   繁体   English

重新启动动画iOS Swift

[英]Restarting animations ios swift

The animation stops working after i 动画在我之后停止工作

  1. Transits to another view controller and comes back 转换到另一个视图控制器,然后返回
  2. app goes to background and then comes back to foreground 应用程序进入后台,然后又回到前台

i am using this code (in viewwillappear)to achieve blinking animation 我正在使用此代码(将出现在视图中)来实现闪烁的动画

    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)

Can anyone help me? 谁能帮我? thanks. 谢谢。

Yeah, it is the expected behaviour. 是的,这是预期的行为。 Animations will stop when the view disappears, either by minimizing the app or by showing another viewcontroller. 当视图消失时,动画将停止,方法是最小化应用程序或显示另一个ViewController。 Move your animation code to viewDidAppear, and the animation will not stop when you move to any other viewcontroller and come back. 将动画代码移动到viewDidAppear,并且当您移动到任何其他viewcontroller并返回时,动画不会停止。 For handling the case where animation stops when app goes to background, use following code: 要处理当应用程序进入后台时动画停止的情况,请使用以下代码:

Inside your viewDidAppear, 在您的viewDidAppear内部,

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

and in your viewWillDisappear, 在您看来,WillDisappear,

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

and write this function inside your viewcontroller, 并在您的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)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM