iphone - 为什么当我的应用程序转到后台时,我的无限期 UIView 动画会停止?

标签 iphone cocoa-touch core-animation multitasking

我使用以下代码无限期地对 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];

如果我的应用程序转到后台,然后我返回到它,所有像这样的动画现在都会停止。为什么会发生这种情况?

最佳答案

通常,当您的应用程序进入后台时,其执行会完全暂停。即使您的应用程序保持事件状态(出于位置跟踪、VoIP 或其他原因),任何绘制到屏幕上的内容 will be halted当您的应用程序进入后台状态时:

Avoid updating your windows and views. While in the background, your application’s windows and views are not visible, so you should not try to update them. Although creating and manipulating window and view objects in the background does not cause your application to be terminated, this work should be postponed until your application moves to the foreground.

事实上,如果您尝试在后台绘制 OpenGL ES 上下文,您的应用程序will immediately crash :

Do not make any OpenGL ES calls from your code. You must not create an EAGLContext object or issue any OpenGL ES drawing commands of any kind while running in the background. Using these calls causes your application to be terminated immediately.

通常,建议暂停应用程序移动到后台时的所有动画,然后在应用程序位于前台时恢复这些动画。这可以在 -applicationDidEnterBackground: 中处理和-applicationWillEnterForeground:委托(delegate)方法,或者通过监听UIApplicationDidEnterBackgroundNotificationUIApplicationWillEnterForegroundNotification通知。

关于iphone - 为什么当我的应用程序转到后台时,我的无限期 UIView 动画会停止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4668714/

相关文章:

objective-c - 确定用户按下了哪个按钮

ios - 限制 subview 在父 View 范围内的移动

ios - 在 subview 中添加相同的图层会改变其视觉位置

iphone - 如何使带有按钮的 UITableViewCell 背景透明

ios - 你能在不破坏后退按钮的情况下拦截 UIWebView 中的 NSURLRequests 吗?

iphone - UIView 始终以纵向模式加载

objective-c - Reeder Mac 应用程序在切换文件夹时如何以动画方式显示列表?

ios - 当我在 animationDidStart : 中设置结束值时,CABasicAnimation 不插值

iphone - 在上一个终点和下一个起点之间画一条线

iphone - 如何使用 GET 方法 JSON Objective C 在 NSRequestURL 中传递特殊字符(如 #、@、%、$)