您的位置:首页 > 产品设计 > UI/UE

UIWindow 介绍:概述、作用、主要属性及方法

2013-12-31 15:23 579 查看

概述:

UIWindow类是 UIView 的子类,用于管理、协调应用中显示的窗口,其两个最重要的职能就是

容器,给 view 提供展示的区域;

将事件(例如:点击事件、拖拉事件等)分发给 view。

而在视图等级中,UIWindow就是视图等级中的根 view。而每一个 UIWindow都有一个窗口等级(属性名:windowLevel),如果应用中同时出现了多个 UIWindow,那么窗口等级高的 UIWindow将出现在窗口等级低的 UIWindow的上面;例如:UIAlertView 会显示在一般窗口的上面,那是因为 UIAlertView 的窗口等级比一般窗口的窗口等级高。而一般情况下,一个 iOS 应用都只有一个 UIWindow。

而当你使用 Interface Builder 来创建 UIWindow时,建议在属性检查器(Attributes inspector)中的载入选项(Launch option)中启用全屏选项,而如果全屏选项没开启的话,你创建的这个 UIWindow的尺寸大小跟目标设备的尺寸大小是不一致的,这时可能会遇到以下两个问题:

UIWindow将无法接收到在 UIWindow边界(bound)外产生的触摸事件(touch events);

view 默认不会根据 UIWindow的尺寸进行裁剪,导致 view 超出 UIWindow的边界。

而在载入选项中启用全屏选项,可避免上述的两个问题。

UIWindow的作用

iOS 应用程序通常只有一个窗口,表示为一个 UIWindow类的实例。应用程序在启动时创建这个窗口(或者从 nib 文件进行装载),并往窗口中加入一或多个视图,然后将它显示出来。窗口显示出来之后,很少需要再次引用它。

在 iOS 中,窗口对象并没有像关闭框或标题栏这样的视觉装饰,用户不能直接对其进行关闭或其它操作。所有对窗口的操作都需要通过其编程接口来实现。应用程序可以借助窗口对象来进行事件传递。窗口对象会持续跟踪当前的第一响应者对象,并在 UIApplication 对象提出请求时将事件传递它。

在 iOS 中,
UIWindow
的父类是 UIView。因此,窗口在 iOS 中也是一个视图对象,但是一般情况下不必直接操作
UIWindow
对象中与视图有关的属性。

在创建应用程序窗口时,应该总是将其初始的边框尺寸设置为整个屏幕的大小。如果窗口是从 nib 文件装载得到,Interface Builder 并不允许创建比屏幕尺寸小的窗口;然而,如果你是通过编程方式创建的窗口,则必须在创建时传入期望的边框矩形。除了屏幕矩形之外,没有理由传入其它边框矩形。屏幕矩形可以通过 UIScreen 对象来取得,具体代码如下所示:


UIWindow* aWindow = [[[UIWindowalloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];



主要属性:

keyWindow : BOOL 类型,只读,用于判断是否是当前应用的 key window (key window 是指可接收到键盘输入及其他非触摸事件的 UIWindow,一次只能有一个 key window)

rootViewController :UIViewController 类型,iOS 4.0新增属性,设置此属性后,rootViewController 所包含的 view 将被添加到 UIWindow中,iOS 4.0之前版本可采用 addSubview: 方法达到同样的效果,因为 UIWindow类本身就是 UIView 的子类。

windowLevel :UIWindowLevel 类型,多个 UIWindow的显示顺序是按照 windowLevel 从高到低排列的,windowLevel 最高的显示在最前面。比如:windowLevel 较高的 UIAlertView 警告窗口会显示在一般窗口的前面。UIWindowLevel 类型的定义如下:


const UIWindowLevel UIWindowLevelNormal; // 默认等级
const UIWindowLevel UIWindowLevelAlert; // UIAlertView 的等级
const UIWindowLevel UIWindowLevelStatusBar; // 状态栏的等级
typedef CGFloat UIWindowLevel;


从 UIWindowLevel 的定义可知,windowLevel 的值其实是一个 CGFloat 类型,我们将系统预设的三个等级打印出来,他们的值如下:


UIWindowLevelNormal:0.000000
UIWindowLevelAlert:2000.000000
UIWindowLevelStatusBar:1000.000000


UIWindowLevelNormal < UIWindowLevelStatusBar < UIWindowLevelAlert
而当你需要一个额外的 UIWindow,并且此 UIWindow需要显示在 UIAlertView 的前面,可以将此 windowLevel 设置为 :UIWindowLevelAlert + 1,那么此 UIWindow与 UIAlertView 同时显示时,此 UIWindow将显示在 UIAlertView 前面。

我们知道了多个不同 windowLevel 的 UIWindow会按照 windowLevel 从高到低进行显示排序,那如果相同 windowLevel 的多个 UIWindow又会怎么显示呢?这个问题后面的例子中会解答。

主要方法:

makeKeyWindow :将当前 UIWindow设置成应用的 key window。

makeKeyAndVisible :将当前 UIWindow设置成应用的 key window,并使得 UIWindow可见;你也可以使用 UIView 的 hidden 属性来显示或者隐藏一个 UIWindow。

相同 windowLevel 等级 UIWindow显示例子

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

- (
BOOL
)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

self.window = [[[UIWindowalloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

self.window.backgroundColor = [UIColor whiteColor];

[self.window makeKeyAndVisible];


UIWindow*windowNormal = [[UIWindowalloc] initWithFrame:CGRectMake(20, 20, 100, 200)];

windowNormal.backgroundColor = [UIColor redColor];

windowNormal.windowLevel = UIWindowLevelNormal + 1;

[windowNormal makeKeyAndVisible];


UIWindow*windowNormal2 = [[UIWindowalloc] initWithFrame:CGRectMake(40, 40, 100, 200)];

windowNormal2.backgroundColor = [UIColor blueColor];

windowNormal2.windowLevel = UIWindowLevelNormal + 1;

[windowNormal2 makeKeyAndVisible];


return
YES;

}


上面的代码运行结果:





从上面的结果中可以知道,相同 windowLevel 的两个 UIWindow,先调用 makeKeyAndVisble 的会一直显示在前面。
而我还没研究出怎么调整两个相同 windowLevel 的 UIWindow的显示顺序,只能通过调整 windowLevel?如果你知道其他方法,可以到 stackoverflow回答。

参考资料:
Apple document – UIWindowClass Reference
iPhone应用程序编程指南

via:http://www.zhenby.com/tech/ios/154
转:http://www.dkankan.com/archives/5953
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: