在后台运行应用程序的最佳方法是什么

时间:2019-09-12 03:22:08

标签: ios swift lottie

我正在快速制作一个简单的应用程序来测试Lottie动画库。当我第一次吃午餐时,该代码可以完美运行。但是,如果我从后台重新启动,动画将停止。

我如何保持动画在后台运行,所以当我切换到另一个应用程序并再次返回时,我看到动画在工作。

我刚刚开始学习敏捷,所以我提前道歉。

import UIKit
import Lottie

class ViewController: UIViewController {
    @IBOutlet weak var animationView: AnimationView!

    override func viewDidLoad() {
        super.viewDidLoad()
        SetupAnimation()
    }

func SetupAnimation() {
        let animationView = AnimationView(name: "kiss")
        animationView.frame = self.animationView.frame
        self.animationView.addSubview(animationView)
        animationView.loopMode = .autoReverse
        animationView.contentMode = .scaleAspectFit
        animationView.play()
   }
}

2 个答案:

答案 0 :(得分:1)

有一件简单的事情可以帮助您播放动画。

在添加了动画的视图控制器中添加willEnterForegroundNotification的以下通知。

另一件事,在全局声明Lottie的AnimationView的实例。参见以下代码

class MyClassVC: UIViewController {

    @IBOutlet weak var animationView : UIView!
    var animation : AnimationView?

    func setupAnimation() {
        animation = AnimationView(name: "kiss")
        animation?.frame = self.animationView.bounds
        self.animationView.addSubview(animation!)
        animation?.loopMode = .autoReverse
        animation?.contentMode = .scaleAspectFit
        animation?.play()
    }

    @objc func applicationEnterInForground() {
        if animation != nil {
            if !(self.animation?.isAnimationPlaying)! {
                self.animation?.play()
            }
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupAnimation()
        NotificationCenter.default.addObserver(self, selector: #selector(applicationEnterInForground), name: UIApplication.willEnterForegroundNotification, object: nil)
    }
}

当应用程序从后台进入前台时,它将播放动画。

答案 1 :(得分:0)

尝试

Get-ChildItem "$OpJob_Path" -File -Filter "ID_DON" -Recurse | %{Remove-Item "$($_.DirectoryName)\ID" -ErrorAction SilentlyContinue}

另一件事是在这里扭动

override func viewDidAppear(animated: Bool)
{
    super.viewDidAppear(animated: animated)
    animation.play()
}

我认为var animation: AnimationView! func SetupAnimation() { animation = AnimationView(name: "kiss") animation.frame = self.animationView.frame self.animationView.addSubview(animation) animation.loopMode = .autoReverse animation.contentMode = .scaleAspectFit } 的任何用途都不是animationView。您可以将其更改为AnimationView

UIView

@IBOutlet weak var animationView: AnimationView!