iOS延迟启动图并且控制他的消失

2016-09-08  本文已影响294人  洲洲哥

本文首发地址
最近在搞项目的时候,遇见一个问题。
项目描述:在程序启动的时候要检查程序的版本升级,就是在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
还可添加QQ群:234812704(洲洲哥学院)
欢迎各位一块学习,提高逼格!
也可以添加洲洲哥的微信公众号

更多消息

更多信iOS开发信息 请以关注洲洲哥 的微信公众号,不定期有干货推送:

这里写图片描述
上一篇下一篇

猜你喜欢

热点阅读