Hello! Sorry you are having an Issue! Please help us make Lottie better by filling everything below out with as much information as you can so we can try to reproduce and fix the issue!
(Screenshots encouraged)
(Screenshots encouraged)
Adding the animation JSON helps us debug the issue faster!
I set loopAnimation to YES ,it works fine frist,but when I pressed the home button,and then back the app, the animation stopped
+1
I think it would be nice to have this as a controllable flag, so the view could handle the pause/resume if it was playing when entering background and becoming active.
BTW thanks for a great project!
+1
I think it is base on Core animation, so .... how to fix it?
seeing this same problem... Added an animation as subview to an UIView object, loop is on yes but the animation disappears after a few seconds...
@EZIOoooo In Core Animation ,set CAAnimation removeOnCompletion = NO; can solve this problem, but this don't work for lottie...
+1 the same problem , emmmmmmmm, so .... somebody tell how to fix plz......
In viewdidappear you can check isAnimationPlaying property. If Its false call [animationView play]
+1 the same problem
@Brsoyan It wont't work, come to foreground, UIViewController will not call viewdidappear,
but you can observe system Notification --UIApplicationWillEnterForegroundNotification ,but it is kind of stupid.
I have the same problem, and I have downgraded to V 2.0.3:
pod 'lottie-ios', '2.0.3'
Worked for me...
Just implemented viewDidLayoutSubview() method, put the animation within the method and it works fine. Thank you!
I tried to hook play method in the category's load method and added a listener to UIApplicationDidEnterBackgroundNotification and UIApplicationDidBecomeActiveNotification notifications to solve this problem.
But I don鈥檛 know if this will have any risk, or do you have any suggestions?
Maybe it's stopping for saving energy.
Just observe UIApplicationWillEnterForegroundNotification in your view controller
[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationWillEnterForegroundNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
if (![animationView isAnimationPlaying]) {
[animationView play];
}
}];
Hey!
I had the same problem which is the animation disappear when i leave the page where the animation is located and come back to it later
i solved it although i work on Lottie.Forms but the solution is generic it gonna work for each one of you.
there is some event fired when the animation page loads right regardless your platform is right?
you subscribe to this event and do this:
remove the animation view implemented earlier
insert a new animation object within the same place
it's not very friendly to the performance but it woks since there is no official solution from Lottie themselves
Have you tried to program a simple delay? My animation was stopping after the first play, then I did:
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
// Your code with delay
self.animatedView.loopAnimation = true
self.animatedView.play()
}
and it started working. Hope it helps!
IMHO This should definitely be a feature of Lottie. It was always a pain in the a拢拢 to fix the UIKit/calayer animations on foreground. It was always one of the weaknesses. Game rendering engines etc do not have this problem.
At least it should be a feature that is by default on (auto pause in bg, resume on fg) that could be toggled off as needed.
for me, this worked:
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(restartAnimation), name: NSNotification.Name.UIApplicationWillEnterForeground, object: nil)
}
@objc func restartAnimation() {
animation.play()
}
Yes sure it works - of course. thanks.
+ (void)load{
Method oldPlayMethod = class_getInstanceMethod(self, @selector(play));
Method newPlayMethod = class_getInstanceMethod(self, @selector(sk_play));
method_exchangeImplementations(oldPlayMethod, newPlayMethod);
Method oldPauseMethod = class_getInstanceMethod(self, @selector(pause));
Method newPauseMethod = class_getInstanceMethod(self, @selector(sk_pause));
method_exchangeImplementations(oldPauseMethod, newPauseMethod);
}
- (void) sk_play {
if (self.isAnimationPlaying) {
return;
}
[self sk_play];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillEnterForegroud) name:UIApplicationWillEnterForegroundNotification object:nil];
}
- (void) sk_pause {
if (!self.isAnimationPlaying) {
return;
}
[self sk_pause];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillEnterForegroundNotification object:nil];
}
- (void) applicationWillEnterForegroud {
[self sk_play];
}
We can all do that @luyinuo. The point of this issue is to get it changed in the library, not having to workaround this. And if it doesn't work like this by design, to know why...
Recommending swizzling shouldn't be so casual.
Fortunately, it looks like a fix was put in place for this by @buba447 in 2.5.1 and he can probably mark this as fixed. Thanks buba!
Updated to the most recent version on 10/25/18 and this has been corrected. Thanks @buba447 !
Hello all. What is exactly the root cause of that? I'm using xamarin forms and I have this issue of both platforms ios and Android but the animation literally disappears sometimes and come back or not but there is nothing in the logs.
cool
Lottie has been completely rewritten in Swift as of 3.0 (https://github.com/airbnb/lottie-ios/pull/777)
I am closing all issues prior to this release to reduce the noise. If you continue to run into this issues or any issue with Lottie 3.0 please open a new ticket
For continued support of Lottie Objective-c please point to this branch: https://github.com/airbnb/lottie-ios/tree/lottie/objectiveC
Seem we have this to pause in background and restart when it reach foreground animationView.backgroundBehavior = .pauseAndRestore
thanks @levantAJ , worked a charm
One of the best solution:
Create a notifier when the app goes to the background for pause and when back to foreground
override func viewDidLoad() {
super.viewDidLoad()
let notificationCenter = NotificationCenter.default
notificationCenter.addObserver(self, selector: #selector(appMovedToBackground), name: UIApplication.willResignActiveNotification, object: nil)
notificationCenter.addObserver(self, selector: #selector(appBecomeActive), name: UIApplication.didBecomeActiveNotification, object: nil)
animationView = .init(name: "lang")
animationView!.frame = CGRect(x: 20, y: 100, width: 50, height: 50)
animationView!.contentMode = .scaleAspectFit
animationView!.loopMode = .loop
animationView!.animationSpeed = 0.5
view.addSubview(animationView!)
animationView!.play()
}
This will invoke notification methods when the app moves to background and foreground
@objc func appMovedToBackground() {
print("App moved to background!")
animationView!.removeFromSuperview()
}
@objc func appBecomeActive() {
print("App become active")
//animationView!.play()
animationView = .init(name: "lang")
animationView!.frame = CGRect(x: 20, y: 100, width: 50, height: 50)
animationView!.contentMode = .scaleAspectFit
animationView!.loopMode = .loop
animationView!.animationSpeed = 0.5
view.addSubview(animationView!)
animationView!.play()
}
This will solve your problem....... For sure
Most helpful comment
Seem we have this to pause in background and restart when it reach foreground
animationView.backgroundBehavior = .pauseAndRestore