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

发布于 2024-10-12 02:14:59 字数 474 浏览 3 评论 0原文

我使用以下代码无限期地对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雨轻弹 2024-10-19 02:14:59

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

避免更新窗口和视图。
在后台时,您的
应用程序的窗口和视图是
不可见,所以你不应该尝试
更新它们。虽然创造和
操作窗口和视图对象
在后台不会导致您
申请被终止,这
工作应该推迟到你
应用程序移至前台。

事实上,如果您尝试在后台绘制 OpenGL ES 上下文,您的应用程序 将立即崩溃

不要进行任何 OpenGL ES 调用
您的代码。
您不得创建
EAGLContext 对象或发出任何 OpenGL
任何类型的 ES 绘图命令
在后台运行。使用这些
调用会导致您的应用程序
立即终止。

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

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:

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.

In fact, if you try to draw to an OpenGL ES context in the background, your application 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.

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 the UIApplicationDidEnterBackgroundNotification and UIApplicationWillEnterForegroundNotification notifications.

半衾梦 2024-10-19 02:14:59

正如 @BradLarson 在他的答案中建议的那样,我添加了 NSNotificationObserver 来处理 UIApplicationDidEnterBackgroundNotification< /code> 和 UIApplicationWillEnterForegroundNotification 在我的视图控制器中。

- (void) addNotificationObserver {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
}

- (void) removeNotificationObserver {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillEnterForegroundNotification object:nil];
}

- (void) applicationDidEnterBackground:(NSNotification *)notification {
    // went to background
}

- (void) applicationWillEnterForeground:(NSNotification *)notification {
    // comes to foreground
}

- (void) viewDidLoad {
    [super viewDidLoad];
    [self addNotificationObserver];
}

- (void) dealloc {
    [self removeNotificationObserver];
}

As @BradLarson suggested in his answer, I have added NSNotificationObserver to handle UIApplicationDidEnterBackgroundNotification and UIApplicationWillEnterForegroundNotification in my view controller.

- (void) addNotificationObserver {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
}

- (void) removeNotificationObserver {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillEnterForegroundNotification object:nil];
}

- (void) applicationDidEnterBackground:(NSNotification *)notification {
    // went to background
}

- (void) applicationWillEnterForeground:(NSNotification *)notification {
    // comes to foreground
}

- (void) viewDidLoad {
    [super viewDidLoad];
    [self addNotificationObserver];
}

- (void) dealloc {
    [self removeNotificationObserver];
}
时光礼记 2024-10-19 02:14:59

当应用程序进入后台时,iOs 会停止代码的执行。 看看这个。

iOs stops the execution of code when the application goes in background. Check this out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文