为什么当我的应用程序转到后台时,我的无限期 UIView 动画会停止?
我使用以下代码无限期地对 UIView 进行动画处理:
#define DEFAULT_ANIM_SPPED 0.6
#define INFINATE_VALUE 1e100f
[UIView beginAnimations:nil context:nil];
[UIView setAnimationRepeatAutoreverses:YES];
[UIView setAnimationRepeatCount:INFINATE_VALUE];
[UIView setAnimationDuration:DEFAULT_ANIM_SPPED];
CGRect tempFrame=myView.frame;
tempFrame.origin.y -= 30;
myView.frame=tempFrame;
[UIView commitAnimations];
如果我的应用程序转到后台,然后返回到它,则所有类似这样的动画现在都会停止。为什么会发生这种情况呢?
I am using the following code for animating a UIView indefinitely:
#define DEFAULT_ANIM_SPPED 0.6
#define INFINATE_VALUE 1e100f
[UIView beginAnimations:nil context:nil];
[UIView setAnimationRepeatAutoreverses:YES];
[UIView setAnimationRepeatCount:INFINATE_VALUE];
[UIView setAnimationDuration:DEFAULT_ANIM_SPPED];
CGRect tempFrame=myView.frame;
tempFrame.origin.y -= 30;
myView.frame=tempFrame;
[UIView commitAnimations];
If my application goes to the background, and then I return to it, all animations like this are now halted. Why would this be happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通常,当您的应用程序进入后台时,其执行会完全暂停。即使您的应用程序保持活动状态(出于位置跟踪、VoIP 或其他原因),绘制到屏幕上的任何内容都 将停止:
事实上,如果您尝试在后台绘制 OpenGL ES 上下文,您的应用程序 将立即崩溃:
一般来说,建议暂停应用程序移动到后台时的所有动画,然后在应用程序位于前台时恢复这些动画。这可以在
-applicationDidEnterBackground:
和-applicationWillEnterForeground:
委托方法中处理,或者通过监听UIApplicationDidEnterBackgroundNotification
和UIApplicationWillEnterForegroundNotification
来处理代码>通知。Usually your application's execution is entirely suspended when it goes to the background. Even if your application remains active (for location tracking, VoIP, or some other reason), anything that draws to the screen will be halted when your application moves to the background state:
In fact, if you try to draw to an OpenGL ES context in the background, your application will immediately crash:
Generally, the recommendation is to pause any animations on the movement of your application to the background, and then to resume those once your application is in the foreground. This can be handled in the
-applicationDidEnterBackground:
and-applicationWillEnterForeground:
delegate methods, or by listening to theUIApplicationDidEnterBackgroundNotification
andUIApplicationWillEnterForegroundNotification
notifications.正如 @BradLarson 在他的答案中建议的那样,我添加了
NSNotificationObserver
来处理UIApplicationDidEnterBackgroundNotification< /code> 和
UIApplicationWillEnterForegroundNotification
在我的视图控制器中。As @BradLarson suggested in his answer, I have added
NSNotificationObserver
to handleUIApplicationDidEnterBackgroundNotification
andUIApplicationWillEnterForegroundNotification
in my view controller.当应用程序进入后台时,iOs 会停止代码的执行。 看看这个。
iOs stops the execution of code when the application goes in background. Check this out.