码迷,mamicode.com
首页 > Windows程序 > 详细

分析UIWindow

时间:2015-12-07 22:35:38      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:

转载自:http://www.cnblogs.com/YouXianMing/p/3811741.html

 

The UIWindow class defines an object known as a window that manages and coordinates the views an app displays on a device screen. Unless an app can display content on an external device screen, an app has only one window.

UIWindow定义了一个对象,用来管理视图的坐标系统,除非,app可以在另外一个窗口里面展示内容,否则,一个app只有一个window.

 

每一个controller都会被UIWindow接管,UIWindow一次只能接管一个controller,下面用代码验证.

首先,我们来看看,UIWindow何时接管了controller.

请写以下代码:

技术分享
//
//  RootViewController.m
//  Window
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "RootViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    if (self.view.window)
    {
        NSLog(@"viewDidLoad");
        NSLog(@"%@", NSStringFromCGRect(self.view.window.frame));
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    if (self.view.window)
    {
        NSLog(@"viewWillAppear");
        NSLog(@"%@", NSStringFromCGRect(self.view.window.frame));
    }
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    if (self.view.window)
    {
        NSLog(@"viewDidAppear");
        NSLog(@"%@", NSStringFromCGRect(self.view.window.frame));
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    if (self.view.window)
    {
        NSLog(@"viewWillDisappear");
        NSLog(@"%@", NSStringFromCGRect(self.view.window.frame));
    }
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    
    if (self.view.window)
    {
        NSLog(@"viewDidDisappear");
        NSLog(@"%@", NSStringFromCGRect(self.view.window.frame));
    }
}

@end
技术分享

打印信息,注意哦,UIWindow会在一个controller的viewDidAppear方法中才接管了当前controller,而不是在ViewDidLoad方法中,注意:)

技术分享

 

接下来,我们在导航栏控制器间切换,来观察,UIWindow如何放弃一个controller然后接管另外一个controller

技术分享
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    UINavigationController *NC =         [[UINavigationController alloc] initWithRootViewController:[RootViewController new]];
    
    self.window.rootViewController = NC;
    
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}
技术分享
技术分享
//
//  RootViewController.m
//  Window
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "RootViewController.h"
#import "SecondViewController.h"
#import "YXGCD.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    if (self.view.window)
    {
        NSLog(@"viewDidLoad");
        NSLog(@"%@", NSStringFromCGRect(self.view.window.frame));
    }
    
    [[GCDQueue mainQueue] execute:^{
        [self.navigationController pushViewController:[SecondViewController new]
                                             animated:YES];
    } afterDelay:NSEC_PER_SEC * 4];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    if (self.view.window)
    {
        NSLog(@"viewWillAppear");
        NSLog(@"%@", NSStringFromCGRect(self.view.window.frame));
    }
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    if (self.view.window)
    {
        NSLog(@"viewDidAppear");
        NSLog(@"%@", NSStringFromCGRect(self.view.window.frame));
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    if (self.view.window)
    {
        NSLog(@"viewWillDisappear");
        NSLog(@"%@", NSStringFromCGRect(self.view.window.frame));
    }
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    
    if (self.view.window)
    {
        NSLog(@"viewDidDisappear");
        NSLog(@"%@", NSStringFromCGRect(self.view.window.frame));
    }
}

@end
技术分享
技术分享
//
//  SecondViewController.m
//  Window
////  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
}

@end
技术分享

查看结果:

技术分享

结论很自然,一个UIWindow只能在接管一个controller.

 

为什么要纠结于UIWindow的这些小细节呢?因为,UIWindow有着比一切controller都要高的优先级显示权利,加载在UIWindow上面的View是不会被遮挡住的.

效果(注意查看上面的导航条的地方,也被遮盖住了):

技术分享

源码:

技术分享
//
//  RootViewController.m
//  Window
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "RootViewController.h"
#import "YXGCD.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [[GCDQueue mainQueue] execute:^{
        
        UIView *redView         = [[UIView alloc] initWithFrame:self.view.window.frame];
        redView.backgroundColor = [UIColor redColor];
        redView.alpha           = 0.f;
        [self.view.window addSubview:redView];
        
        [UIView animateWithDuration:1 animations:^{
            redView.alpha = 1.f;
        } completion:^(BOOL finished) {
            
            [UIView animateWithDuration:1 animations:^{
                redView.alpha = 0.f;
            } completion:^(BOOL finished) {
                [redView removeFromSuperview];
            }];
        }];
        
    } afterDelay:NSEC_PER_SEC * 10];
}

@end
技术分享

普通的View的加载的效果(注意上方的导航栏,它并没有变成红色):

技术分享

技术分享
//
//  RootViewController.m
//  Window
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "RootViewController.h"
#import "YXGCD.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [[GCDQueue mainQueue] execute:^{
        
        UIView *redView         = [[UIView alloc] initWithFrame:self.view.window.frame];
        redView.backgroundColor = [UIColor redColor];
        redView.alpha           = 0.f;
        [self.view addSubview:redView];
        
        [UIView animateWithDuration:1 animations:^{
            redView.alpha = 1.f;
        } completion:^(BOOL finished) {
            
            [UIView animateWithDuration:1 animations:^{
                redView.alpha = 0.f;
            } completion:^(BOOL finished) {
                [redView removeFromSuperview];
            }];
        }];
        
    } afterDelay:NSEC_PER_SEC * 10];
}

@end
技术分享

技术分享

其实呢,你如果写成这样子,也是可以遮盖导航条的呢:

技术分享

其实,就这么多东西:)

 

分析UIWindow

标签:

原文地址:http://www.cnblogs.com/Jenaral/p/5027458.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!