我在我的应用中使用Lottie动画,并尝试在退出应用后保持动画在后台运行(不是强制关闭)...
我已经成功地做到了,但问题在于当我选择不同的选项卡并返回具有动画视图的选项卡时,动画会停止。
我已经成功地做到了,但问题在于当我选择不同的选项卡并返回具有动画视图的选项卡时,动画会停止。
这是我的代码。
import UIKit
import Lottie
import UserNotifications
import NotificationCenter
class HomeViewController: UIViewController {
@IBOutlet weak var animationView: UIView!
var animation : AnimationView?
override func viewDidLoad() {
super.viewDidLoad()
setupAnimation()
NotificationCenter.default.addObserver(self, selector: #selector(applicationEnterInForground), name: UIApplication.willEnterForegroundNotification, object: nil)
}
func setupAnimation() {
animation = AnimationView(name: "cong")
animation?.frame = self.animationView.bounds
self.animationView.addSubview(animation!)
animation?.loopMode = .loop
animation?.contentMode = .scaleAspectFit
animation?.play()
}
@objc func applicationEnterInForground() {
if animation != nil {
if !(self.animation?.isAnimationPlaying)! {self.animation?.play()}}
}
}