iOS UIWindowLevel随笔

2019-10-12  本文已影响0人  FieryDragon

UIWindow的显示可以通过UIWindowLevel来区分优先级,所有的window都会被加在界面上,只不过会通过优先级罗列起来,UIWindowLevel大的在上层显示,UIWindowLevel小的在下层显示。
UIWindowLevel优先级相等的情况下,看谁后实例化了,谁后实例化谁先显示。
由最上层window捕获点击事件.

 NSArray *array = @[@(1),@(3),@(5),@(4),@(2)];
    for (int i = 0; i<array.count; i++) {
        NSInteger level = [array[i] integerValue];
        UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        window.hidden = NO;
        window.backgroundColor = [UIColor redColor];
        window.windowLevel = level;
        ViewController *VC= [[ViewController alloc] init];
        VC.window = window;
        VC.level = level;
        window.rootViewController = VC;
        [window makeKeyAndVisible];
    }
    
    NSArray *windowsArray = [UIApplication sharedApplication].windows;
    for (int i = 0; i<windowsArray.count; i++) {
        UIWindow *window = [windowsArray objectAtIndex:i];
        NSLog(@"%lf",window.windowLevel);
    }
    NSLog(@"windowLevel ===%lf",[UIApplication sharedApplication].keyWindow.windowLevel);
2019-09-04 18:38:52.452417+0800 Test[13421:221809] 0.000000
2019-09-04 18:38:52.452609+0800 Test[13421:221809] 1.000000
2019-09-04 18:38:52.452725+0800 Test[13421:221809] 2.000000
2019-09-04 18:38:52.452816+0800 Test[13421:221809] 3.000000
2019-09-04 18:38:52.452900+0800 Test[13421:221809] 4.000000
2019-09-04 18:38:52.452980+0800 Test[13421:221809] 5.000000
2019-09-04 18:38:52.453074+0800 Test[13421:221809] windowLevel ===2.000000

结论: keyWindow为最后一次添加的window,Windows数据按照UIWindowLevel排序(由小至大),同展示顺序.

NSArray *array = [UIApplication sharedApplication].windows;
    for (int i = 0; i<array.count; i++) {
        UIWindow *window = [array objectAtIndex:i];
        NSLog(@"%lf",window.windowLevel);
    }
    
    NSLog(@"windowLevel ===%lf",[UIApplication sharedApplication].keyWindow.windowLevel);
    
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    keyWindow.rootViewController = nil;
    keyWindow.hidden = YES;
    keyWindow = nil;
2019-09-04 18:46:44.998076+0800 Test[13496:224965] 0.000000
2019-09-04 18:46:44.998332+0800 Test[13496:224965] 1.000000
2019-09-04 18:46:44.998557+0800 Test[13496:224965] 2.000000
2019-09-04 18:46:44.998731+0800 Test[13496:224965] 3.000000
2019-09-04 18:46:44.998890+0800 Test[13496:224965] 4.000000
2019-09-04 18:46:44.999049+0800 Test[13496:224965] 5.000000
2019-09-04 18:46:44.999269+0800 Test[13496:224965] windowLevel ===2.000000
2019-09-04 18:46:45.002183+0800 Test[13496:224965] dealloc


2019-09-04 18:47:05.488600+0800 Test[13496:224965] 0.000000
2019-09-04 18:47:05.488824+0800 Test[13496:224965] 1.000000
2019-09-04 18:47:05.488985+0800 Test[13496:224965] 3.000000
2019-09-04 18:47:05.489114+0800 Test[13496:224965] 4.000000
2019-09-04 18:47:05.489317+0800 Test[13496:224965] 5.000000
2019-09-04 18:47:05.489483+0800 Test[13496:224965] windowLevel ===5.000000
2019-09-04 18:47:05.491719+0800 Test[13496:224965] dealloc


2019-09-04 18:47:11.040809+0800 Test[13496:224965] 0.000000
2019-09-04 18:47:11.041034+0800 Test[13496:224965] 1.000000
2019-09-04 18:47:11.041197+0800 Test[13496:224965] 3.000000
2019-09-04 18:47:11.041465+0800 Test[13496:224965] 4.000000
2019-09-04 18:47:11.041629+0800 Test[13496:224965] windowLevel ===4.000000
2019-09-04 18:47:11.043707+0800 Test[13496:224965] dealloc

结论: 最后添加的window取消后, keyWindow为当前展示在最上层window,即windowLevel最大的window(Windows属性最后一位).

上一篇 下一篇

猜你喜欢

热点阅读