iOS延遲啟動圖並且控制他的消失
阿新 • • 發佈:2019-01-06
最近在搞專案的時候,遇見一個問題。專案描述:
在程式啟動的時候要檢查程式的版本升級,就是在AppDelegate裡有一個func區檢查版本升級。去強制或者選擇升級、並且必須還要獲取一個時間戳
,這個時間戳並且還在登入的時候用到這個時間戳。那麼問題就來了:
app在第一次啟動的時候開啟就是登入頁面,當我們點選登入的時候,極有可能在觸發登入事件時,這個獲取時間戳還沒有獲取到。靠!!!
解決問題辦法:
我在登入頁面上的當前的Window
上添加了一個view,並且在這個view上添加了一個和當前view一樣大得UIImageView,這個上面的圖片就是我們設定的啟動圖了。
當在AppDelegate的網路請求成功
通知
.登入頁面接受廣播,在把當前Window上得View從父檢視removeFromSuperview
掉。
順便再解釋一下網路上得設定辦法[NSThread sleepForTimeInterval:2.0];
這個方法是可以控制啟動頁的停留時間,是控制當前主執行緒
的停止,你可以試著用個非同步執行緒去試一下,根本沒用!!!TMD。想要單獨的暫停啟動頁
這個方法是可以的。
上程式碼:AppDelegate
的程式碼
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
UINavigationController * nvc = [[UINavigationController alloc] initWithRootViewController:[[MainViewController alloc]init]];
self.window .rootViewController = nvc;
[self updateVersion];
[self.window makeKeyAndVisible];
return YES;
}
// 檢查版版本更新
-(void)updateVersion {
NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults];
[userDefault setValue:@"洲洲哥" forKey:USERID];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"-----------------AppDelegate----------------AppDelegate----------------");
NSDictionary *dict = @{@"KEYS":@"WANGHUIZHOU"};
// 傳送接收成功的通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"installType" object:nil userInfo:dict];
});
}
loginViewCOntroller
程式碼
// 巨集定義得到當前的Window
#define CurrentWindow [self getCurrentWindowView]
- (instancetype)init
{
self = [super init];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(installProcess:) name:@"installType" object:nil];
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"標題";
self.launchView = [[UIView alloc] initWithFrame:self.view.bounds];
UIImageView * backImageView = [[UIImageView alloc] initWithFrame:self.launchView.bounds];
backImageView.image = [UIImage imageNamed:@"launch"];
[self.launchView addSubview:backImageView];
self.launchView.backgroundColor = [UIColor redColor];
[CurrentWindow addSubview:self.launchView];
}
-(void)installProcess:(NSNotification *)notification {
[UIView animateWithDuration:0.25 animations:^{
[self.launchView removeFromSuperview];
NSLog(@"-----END----%@",USERID);
}];
}
/**獲取當前window*/
- (UIWindow *)getCurrentWindowView {
UIWindow * window = [[UIApplication sharedApplication] keyWindow];
if (window.windowLevel != UIWindowLevelNormal)
{
NSArray *windows = [[UIApplication sharedApplication] windows];
for(UIWindow * tmpWin in windows)
{
if (tmpWin.windowLevel == UIWindowLevelNormal)
{
window = tmpWin;
break;
}
}
}
return window;
}
好了,程式碼到此結束。
有問題可以新增QQ:1290925041
也可以新增洲洲哥的微信公眾號
更多訊息
更多信iOS開發資訊 請以關注洲洲哥 的微信公眾號,不定期有乾貨推送:
這裡寫圖片描述