一、UIWindow是一种特殊的UIView,通常在一个程序中只会有一个UIWindow,但可以手动创建多个UIWindow,同时加到程序里面。UIWindow在程序中主要起到三个作用:
1、作为容器,包含app所要显示的所有视图
2、传递触摸消息到程序中view和其他对象
3、与UIViewController协同工作,方便完成设备方向旋转的支持
二、通常我们可以采取两种方法将view添加到UIWindow中:
1、addSubview
直接将view通过addSubview方式添加到window中,程序负责维护view的生命周期以及刷新,但是并不会为去理会view对应的ViewController,因此采用这种方法将view添加到window以后,我们还要保持view对应的ViewController的有效性,不能过早释放。
2、rootViewController
rootViewController时UIWindow的一个遍历方法,通过设置该属性为要添加view对应的ViewController,UIWindow将会自动将其view添加到当前window中,同时负责ViewController和view的生命周期的维护,防止其过早释放
三、WindowLevel
UIWindow在显示的时候会根据UIWindowLevel进行排序的,即Level高的将排在所有Level比他低的层级的前面。下面我们来看UIWindowLevel的定义:
const UIWindowLevel UIWindowLevelNormal; const UIWindowLevel UIWindowLevelAlert; const UIWindowLevel UIWindowLevelStatusBar; typedef CGFloat UIWindowLevel;
//1.创建了一个window对象,对象的大小是手机屏幕的大小
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
//设置背景颜色
[self.window setBackgroundColor:[UIColor whiteColor]];
//显示在界面上
[self.window makeKeyAndVisible];
//让当前窗口成为主窗口(能接收事件的窗口)
[self.window makeKeyWindow];
window1 = [[UIWindow alloc] initWithFrame:CGRectMake(20, 0, 180, 180)];
[window1 setBackgroundColor:[UIColor orangeColor]];
[window1 makeKeyAndVisible];
//ARC会把window释放掉,将window设置为全局变量,可以解决问题
//2.window level 窗口的级别
window2 = [[UIWindow alloc] initWithFrame:CGRectMake(20, 0, 80, 80)];
[window2 setBackgroundColor:[UIColor blueColor]];
[window2 makeKeyAndVisible];
//等级大小, normal < status bar < alert
[window1 setWindowLevel:UIWindowLevelStatusBar];
[window2 setWindowLevel:UIWindowLevelAlert];
//window level 是 double类型的数据
NSLog(@"%f %f %f", UIWindowLevelNormal, UIWindowLevelStatusBar, UIWindowLevelAlert);
//normal = 0, status bar = 1000, alert = 2000
[self.window setWindowLevel:2000.0001];
//3.获取当前所有的window
NSArray *winArray = [[UIApplication sharedApplication] windows];
NSLog(@"%@", winArray);
//UIView 视图
//frame 视图的位置(x, y) 大小(width, height)
CGRect frame = CGRectMake(0, 20, 100, 200);
UIView *view1 = [[UIView alloc] initWithFrame:frame];
view1.backgroundColor = [UIColor redColor];
//addSubview 会让父视图持有子视图
[self.window addSubview:view1];
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(50, 20, 150, 50)];
view2.backgroundColor = [UIColor greenColor];
// [self.window addSubview:view2];
[view1 addSubview:view2];
//bounds是以自身的坐标原点为起点,x y永远是0.0
//frame 是以父视图的坐标原点为起点
NSLog(@"frame: x=%f y=%f",view2.frame.origin.x, view2.frame.origin.y);
NSLog(@"bounds: x=%f y=%f", view2.bounds.origin.x, view2.bounds.origin.y);
//将结构体转换为NSString
NSString *frameStr = NSStringFromCGRect(view2.frame);
NSString *boundsStr = NSStringFromCGRect(view2.bounds);
NSLog(@"frame: %@", frameStr);
NSLog(@"bounds: %@", boundsStr);
//修改视图的frame,子视图也会移动,但是子视图的frame不变
view1.frame = CGRectMake(100, 200, 20, 600);
//加载xib文件
NSBundle *bundle = [NSBundle mainBundle];
NSArray *viewArray = [bundle loadNibNamed:@"View" owner:self options:nil];
//xib文件:xml格式的ib文件
//nib文件:二进制格式的ib文件
//取出视图
// UIView *view = [viewArray objectAtIndex:0];
UIView *view = [viewArray lastObject];
//添加到window上显示
[self.window addSubview:view];
//UIView 属性
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
view1.center = CGPointMake(100, 100);
//背景颜色
//R red G green B blue 取值范围是0-255
//参数接收的范围是0 - 1
view1.backgroundColor = [UIColor colorWithRed:0.4 green:0.6 blue:0.8 alpha:1];
//透明度
view1.alpha = 0.5;
//隐藏
// view1.hidden = YES;
// view1.alpha = 0;
//响应触摸事件
view1.userInteractionEnabled = YES;
//多点触摸
view1.multipleTouchEnabled = YES;
//tag
view1.tag = 101;
// [view1 removeFromSuperview];
// view1.backgroundColor = [UIColor colorWithRed:(arc4random() * 100)/255.0 green:0.6 blue:(arc4random() * 100)/255.0 alpha:1];
//transform 变形
//起始的形状
CGAffineTransform t = view1.transform;
NSLog(@"transform %@", NSStringFromCGAffineTransform(t));
//缩放
CGAffineTransform scaleT = CGAffineTransformScale(t, 0.5, 0.5);
view1.transform = scaleT;
//旋转
CGAffineTransform rotateT = CGAffineTransformRotate(t, M_PI/4);
view1.transform = rotateT;
//平移
CGAffineTransform slateT = CGAffineTransformTranslate(t, 40, -30);
view1.transform = slateT;
//恢复
view1.transform = t;
view1.transform = CGAffineTransformIdentity;
[self.window addSubview:view1];
//动画持续的时间
[UIView setAnimationRepeatCount:100];
//动画延迟执行