[英]UIView animation is not working properly
I have a UIViewController
with an animated UIBarButtonItem
on the toolbar. 我在工具栏上有一个带有动画
UIBarButtonItem
的UIViewController
。
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. 按下另一个
UIButton
以推动第二个UIViewController
(其中不存在动画按钮)并弹出它,动画将按预期工作。
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. 此外,我在第一个
UIViewController
的视图中添加了相同的动画按钮,并且动画通过按下push / pop停止工作而没有进入背景。
In the code of the UIButton
, I added this in order to manage the animation during transition to background and foreground: 在
UIButton
的代码中,我添加了此代码,以便在过渡到背景和前景期间管理动画:
[[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.
因此,一旦再次显示视图,就需要重新创建它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.