zoukankan      html  css  js  c++  java
  • UIWindow

     
    UIWindow是一种特殊的UIView,通常在一个app中只会有一个UIWindow。
    在IOS应用中,我们使用UIWindow和UIView来呈现界面。UIWindow并不包含任何默认的内容,但它是被当做UIView的容器,用于放置应用中所有的UIView。从继承关系来看,UIWindow继承自UIView,所以UIWIndow除了具有UIView的所有功能外,还增加了一些特有的属性和方法。
     
    UIWindow的主要作用:
    1.作为UIView的最顶层容器,包含应用显示所需要的所有UIView。
    2.传递触摸消息和键盘事件给UIView(
     
    关于系统对UIWindow的使用。
    通常在一个程序中只会有一个UIWindow,但有些时候我们调用系统的控件(例如UIAlertView)时,IOS系统为了保证UIAlertView在所有的界面之上,它会临时创建一个新的UIWindow,通过将其UIWindow的UIWindowLevel设置的更高,让UIWindow盖在所有的应用界面之上(熟悉html的朋友应该知道,网上上面的“遮罩效果”,就是通过设置元素的z-index属性,来控制层级的上下关系,应该是一个道理)。
     

    如何获取UIWindow

    (1)[UIApplication sharedApplication].windows  在本应用中打开的UIWindow列表,这样就可以接触应用中的任何一个UIView对象(平时输入文字弹出的键盘,就处在一个新的UIWindow中);

    (2)[UIApplication sharedApplication].keyWindow(获取应用程序的主窗口)用来接收键盘以及非触摸类的消息事件的UIWindow,而且程序中每个时刻只能有一个UIWindow是keyWindow;

    注:经过代码验证,非keyWindow 也是可以接受键盘消息的;

    提示:如果某个UIWindow内部的文本框不能输入文字,可能是因为这个UIWindow不是keyWindow;

     
    (3)view.window获得某个UIView所在的UIWindow。
     

    UIWindowLevel

    我们知道UIWindow 有三个层级,分别是Normal ,StatusBar,Alert.输出他们三个层级的值,我们发现从左到右依次是0,1000,2000,也就是说Normal级别是最低的,StatusBar处于中级,Alert级别最高。而通常我们的程序的界面都是处于Normal这个级别的,系统顶部的状态栏应该是处于StatusBar级别,UIActionSheet和UIAlertView这些通常都是用来中断正常流程,提醒用户等操作,因此位于Alert级别。

     
    根据window显示级别优先原则,级别高的会显示在最上层,级别低的在下面,我们程序正常显示的view在最底层;
    _window.windowLevel = UIWindowLevelAlert+1;     并且window不需要add
     

    keyWindow

    官方文档中是这样解释的 “The key window is the one that is designated to receive keyboard and other non-touch related events. Only one window at a time may be the key window." 翻译过来就是说,keyWindow是指定的用来接收键盘以及非触摸类的消息,而且程序中每一个时刻只能有一个window是keyWindow。

    观察UIWindow的文档,我们可以发现里面有四个关于window变化的通知:

      UIWindowDidBecomeVisibleNotification

      UIWindowDidBecomeHiddenNotification

      UIWindowDidBecomeKeyNotification

      UIWindowDidResignKeyNotification

      这四个通知对象中的object都代表当前已显示(隐藏),已变成keyWindow(非keyWindow)的window对象,其中的userInfo则是空的。于是我们可以注册这个四个消息,再打印信息来观察keyWindow的变化以及window的显示,隐藏的变动

    变成keywindow 的流程是这样的

    1.程序默认的window先显示出来

    2.默认的window再变成keywindow

    3.AlertView 的window显示出来

    4.默认的window变成keywindow

     
    5.最终AlertView的window变成keywindow
     
    window的几种通知
     [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(orientationChanged:)
                                                     name:UIDeviceOrientationDidChangeNotification
                                                   object:nil];
     
     
     
    - (void)orientationChanged:(NSNotification*)noti {
       
        UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
        NSString *orientationDes = nil;
        switch (orientation) {
            case UIDeviceOrientationLandscapeLeft:
                orientationDes = @"UIInterfaceOrientationLandscapeRight";
                break;
            case UIDeviceOrientationLandscapeRight:
                orientationDes = @"UIInterfaceOrientationLandscapeLeft";
                break;
            case UIDeviceOrientationPortrait:
                orientationDes = @"UIInterfaceOrientationPortrait";
                break;
            case UIDeviceOrientationPortraitUpsideDown:
                orientationDes = @"UIInterfaceOrientationPortraitUpsideDown";
                break;
            default:
                orientationDes = @"";
                break;
        }
       
        NSLog(@"system ver: %@, orientaion: %@, window bounds: %@",
              [UIDevice currentDevice].systemVersion,
              orientationDes,
              NSStringFromCGRect(self.window.bounds));
    }
     
    调用makeKeyAndVisible方法,使整个程序界面可见。执行这行代码之前,window默认hidden=yes
    resignKeyWindow:
         释放keyWindow即可关闭window 
           [_window resignKeyWindow];
             _window = nil;
    becomeKeyWindow
  • 相关阅读:
    Java基础6
    多行文本框回车换行设置
    CentOS7系统配置国内yum源和epel源
    System Integrity Protection (SIP) iOS10.15安装软件提示文件损坏问题解决方法
    转载:Spring Boot 不使用默认的 parent,改用自己的项目的 paren
    微软Speech语音合成技术
    反编译工具
    线性链表的代码实现
    不一样的鸡汤,你有房吗?你有车吗?你有房车吗?
    递归,迭代和回调
  • 原文地址:https://www.cnblogs.com/xsyl/p/6039321.html
Copyright © 2011-2022 走看看