重新启动ios swift动画

重新启动ios swift动画,ios,swift,animation,Ios,Swift,Animation,动画在我打开后停止工作 转换到另一个视图控制器并返回 应用程序转到后台,然后返回前台 我正在使用此代码(在ViewWillDisplay中)实现闪烁动画 UIView.animate(withDuration: 1.0, delay: 0.4, options:[ UIViewAnimationOptions.curveEaseOut , .repeat], animations: { self.logoLabel1.alpha = 0.0 self.l

动画在我打开后停止工作

  • 转换到另一个视图控制器并返回
  • 应用程序转到后台,然后返回前台
  • 我正在使用此代码(在ViewWillDisplay中)实现闪烁动画

        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)
    
    有人能帮我吗?
    谢谢。

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

    在你的视图中

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

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

    在你的视图中

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

    尝试viewDidApperTry ViewDidApprit不工作:(我想一定是有潜在问题吧?我为动画安装了Spring Pod,现在一切都正常工作了。我不知道为什么observer不使用UIView.animate方法。感谢您的回答。它不工作:(我想一定有一个潜在的问题吧?我为动画安装了Spring Pod,现在一切都正常工作了。我不知道为什么观测者不使用UIView.animate方法。谢谢你的回答。