简体   繁体   中英

UIView animation is not working properly

I have a UIViewController with an animated UIBarButtonItem on the toolbar.

Pressing another UIButton in order to push the second UIViewController (where the animated button doesn't exist) and popping it, the animations works as expected.

But if I try to move the app in background and restore it, I still see the animation there, but not anymore if I try to push and pop.

Furthermore I added the same animated button in the view of the first UIViewController , and the animation stopped working by pressing push/pop without going in background.

In the code of the UIButton , I added this in order to manage the animation during transition to background and foreground:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillEnterForeground) name:UIApplicationWillEnterForegroundNotification object:nil];

...

- (void)applicationDidEnterBackground
{
    [self.spinner.layer removeAllAnimations];
}

- (void)applicationWillEnterForeground
{
    [self createAnimation];
}

- (void)createAnimation
{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];

    [animation setFromValue:@(0)];
    [animation setToValue:@(DegreesToRadians(359))];
    [animation setDuration:0.4];
    [animation setRepeatCount:MAXFLOAT];
    [self.spinner.layer addAnimation:animation forKey:animation.keyPath];
}

problem solved (I don't know if it's the best way)

- (void)didMoveToWindow
{
    [super didMoveToWindow];
    if (self.window != nil) {
         [self createAnimation];
    }
}

Basically, it looks that when the view is disappearing (because of a push in this case) all the animations are removed automatically. Because of this, it's needed to recreate them once the view is showed again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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