在 applicationWillEnterForeground 之前更改视图

发布于 2024-11-09 11:03:15 字数 515 浏览 0 评论 0原文

我想在用户处于后台一段时间后将其锁定在我的应用程序之外。我在 AppDelegate 的 applicationWillEnterForeground 中捕获了这一点(并与 applicationWillResignActive 中存储的时间进行比较)。如果小于超时时间,则不会发生任何操作。如果超过超时时间,我调用:

[_navigationController popToRootViewControllerAnimated:NO];

这会将用户带回根视图。

只要有一次视觉中断,它就能完美地工作。先前的视图(应用程序处于非活动状态时用户正在查看的视图)在弹出到根目录之前会出现非常短暂。在测试中,视图似乎在调用 applicationWillEnterForeground 之前重新出现。

我唯一的想法是在不活动之前隐藏所有内容,例如用填充的矩形遮挡视图。这对我来说就像是一个肮脏的黑客,所以我想有一个更好的方法。

我也愿意采用不同的方式来达到相同的最终结果。谢谢你!

I want to lock users out of my app after a period of being in the background. I'm catching this in the the AppDelegate's applicationWillEnterForeground (and comparing to time stored in applicationWillResignActive). If less than the timeout period no action takes place. If more than the timeout period I call:

[_navigationController popToRootViewControllerAnimated:NO];

which brings the user back to the root view.

It works perfectly fine with one visual interruption. The prior view (that which the user was viewing when the application went inactive) appears very briefly before popping to root. In testing it seems the view reappears prior to applicationWillEnterForeground being called.

The only thought I've had is to hide everything before going inactive, such as by obscuring the view with a filled rectangle. This smells like a dirty hack to me, so I'm thinking there's a better way.

I'm also open to different ways to reach the same end result. Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

羅雙樹 2024-11-16 11:03:15

我通过使视图不可见解决了这个问题。在 applicationWillResignActive 我有 _navigationController.view.alpha=0;,并且我放置了 _navigationController.view.alpha=100;在(如有必要)弹出到登录视图之后的 applicationWillEnterForeground 中。比弹出和恢复视图更容易(在这种情况下相当重)。

@TheBlack 指出了隐藏属性,这在设备上可能稍微容易一些。我保留 alpha 是因为 A) 使用 50% alpha 视图进行调试很有趣,B) 我只是喜欢 alpha。但除非你像我一样是阿尔法粉丝,否则隐藏可能会更好一些。

I've solved this by making the view invisible. In applicationWillResignActive I have _navigationController.view.alpha=0;, and I've placed _navigationController.view.alpha=100; in applicationWillEnterForeground after (if necessary) popping to the login view. Easier than popping and restoring views (which in this case are pretty heavy).

@TheBlack points out the hidden property, which is probably slightly easier on the device. I'm leaving alpha in because A) it's fun debugging with a 50% alpha view, and B) I just like alpha's. But unless you're an alpha fan like me, hidden might be a little better.

断桥再见 2024-11-16 11:03:15

来自文档: 处理应用程序状态转换的策略

准备应用快照

应用程序委托的 applicationDidEnterBackground: 方法返回后不久,系统会拍摄应用程序窗口的快照。同样,当应用程序被唤醒以执行后台任务时,系统可能会拍摄新快照以反映任何相关更改。例如,当唤醒应用程序以处理下载的项目时,系统会拍摄新的快照,以便反映因合并项目而引起的任何更改。系统在多任务 UI 中使用这些快照图像来显示应用程序的状态。

如果您在进入后台时对视图进行了更改,则可以调用主视图的 snapshotViewAfterScreenUpdates: 方法来强制渲染这些更改。在视图上调用 setNeedsDisplay 方法对于快照无效,因为快照是在下一个绘制周期之前拍摄的,因此会阻止渲染任何更改。调用值为 YES 的 snapshotViewAfterScreenUpdates: 方法会强制立即更新快照机制使用的底层缓冲区。

From the docs: Strategies for Handling App State Transitions

Prepare for the App Snapshot

Shortly after an app delegate’s applicationDidEnterBackground: method returns, the system takes a snapshot of the app’s windows. Similarly, when an app is woken up to perform background tasks, the system may take a new snapshot to reflect any relevant changes. For example, when an app is woken to process downloaded items, the system takes a new snapshot so that can reflect any changes caused by the incorporation of the items. The system uses these snapshot images in the multitasking UI to show the state of your app.

If you make changes to your views upon entering the background, you can call the snapshotViewAfterScreenUpdates: method of your main view to force those changes to be rendered. Calling the setNeedsDisplay method on a view is ineffective for snapshots because the snapshot is taken before the next drawing cycle, thus preventing any changes from being rendered. Calling the snapshotViewAfterScreenUpdates: method with a value of YES forces an immediate update to the underlying buffers that the snapshot machinery uses.

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