【发布时间】:2011-05-05 07:09:42
【问题描述】:
当用户通过切换到前台激活应用程序时,我需要在屏幕上隐藏一些内容。
我尝试在 applicationDidBecomeActive 或 applicationWillEnterForeground 中插入我的代码,虽然它运行正常,但带有我想要隐藏的文本的旧屏幕会暂时显示。
如何在重绘屏幕之前隐藏该字段?
谢谢
iphaaw
【问题讨论】:
标签: iphone ios uiapplicationdelegate
当用户通过切换到前台激活应用程序时,我需要在屏幕上隐藏一些内容。
我尝试在 applicationDidBecomeActive 或 applicationWillEnterForeground 中插入我的代码,虽然它运行正常,但带有我想要隐藏的文本的旧屏幕会暂时显示。
如何在重绘屏幕之前隐藏该字段?
谢谢
iphaaw
【问题讨论】:
标签: iphone ios uiapplicationdelegate
我认为问题在于,iOS 会在您的应用程序进入后台的那一刻捕获屏幕截图,因此动画将立即生效。
在我看来,这样做的唯一方法是在应用程序进入后台时隐藏/覆盖您的视图。
【讨论】:
在applicationWillResignActive: 中编写一些代码以“隐藏”您需要隐藏的任何内容。
【讨论】:
我遇到了类似的情况,但我不想隐藏,而是想显示一个块代码屏幕来授予访问权限。无论如何,我认为该解决方案也适用于您的需求。
我经常在我的 iOS 应用程序中实现自定义基本视图控制器。因此,我没有处理applicationDidBecomeActive: 或applicationWillResignActive:,而是设置了这个视图控制器来监听等效的通知:
@interface BaseViewController : UIViewController
- (void)prepareForGrantingAccessWithNotification:(NSNotification *)notification;
- (void)grantAccessWithNotification:(NSNotification *)notification;
@end
@implementation BaseViewController
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self addNotificationHandler:@selector(grantAccessWithNotification:)
forNotification:UIApplicationDidBecomeActiveNotification];
[self addNotificationHandler:@selector(prepareForGrantingAccessWithNotification:)
forNotification:UIApplicationWillResignActiveNotification];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)prepareForGrantingAccessWithNotification:(NSNotification *)notification {
// Hide your views here
myCustomView.alpha = 0;
// Or in my case, hide everything on the screen
self.view.alpha = 0;
self.navigationController.navigationBar.alpha = 0;
}
- (void)grantAccessWithNotification:(NSNotification *)notification {
// This is only necessary in my case
[self presentBlockCodeScreen];
self.view.alpha = 1;
self.navigationController.navigationBar.alpha = 1;
...
}
@end
【讨论】: