02 窗口与视图的基本概念和用法

一、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;
  IOS系统中定义了三个window层级,其中每一个层级又可以分好多子层级(从UIWindow的头文件中可以看到成员变量CGFloat _windowSublevel;),不过系统并没有把则个属性开出来。UIWindow的默认级别是UIWindowLevelNormal.

四,UIWindow的创建和使用

    //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);


     1. 手动创建 ( Xcode默认 ):一般创建一个窗口,不建议创建多个,会影响事件的传递。

     2. UIScreen对象充当物理屏幕的替代者:
     UIScreen *screen = [UIScreen mainScreen];
     通过[UIScreen mainScreen].bounds可获取适配尺寸。

     3.获取当前UIWindow和设置级别
     通过UIApplication获取当前keyWindow。keyWindow有且只有一个,是用来管理键盘及非触摸类的消息。
    
     
     UIWindow窗口级别:
     

五,UIView的创建和使用

//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);


     1.iOS坐标系是以左上角为原点。(X表示宽,Y表示高)
     
     2.Frame和Bounds
     Frame以其父视图为起点;Bounds即以本身的坐标原点为起点,坐标为(0,0)。
     Center表示视图中心点所在的位置。

    

     3.创建视图(xib文件和代码创建)

//加载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 属性
    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];
 


     
    

     
    

     
    
     myView.tag = 100;
    

      :

//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的属性动画:
      :
     动画属性是指当属性从一个值变为另一个值的时候,可以半自动地支持动画。

     UIView对象中支持动画的属性:
    

     UIView属性动画—代理设置:
    
     
     通过UIView调用 setAnimationDelegate: 方法来设置委托,并通过 setAnimationWillStartSelector: 和 setAnimationDidStopSelector: 方法来指定接收消息的选择器方法。

animationID —应用程序提供的字符串,用于标识一个动画块中的动画。
context —用于向委托对象传递额外的信息。

     
     
      setAnimationsEnabled: 方法来暂时禁止动画。
     areAnimationsEnabled 方法来确定当前是否激活动画。

     //动画开始
    [UIView beginAnimations:@"alpha_animation" context:nil];
   
//动画持续的时间
    [UIView setAnimationDuration:1];
    //动画执行的次数
    [
UIView setAnimationRepeatCount:100];
   
//动画延迟执行
    [UIView setAnimationDelay:5];   
    self.myView.alpha = 0;   
    //动画执行完成
    [UIView commitAnimations];

八,UIKit框架
    
     
     
    


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值