天天看点

UIWindow和UIView

基础:

UIView是视图的基类,UIViewController是视图控制器的基类,UIResponder是表示一个可以在屏幕上响应触摸事件的对象;

UIwindow是UIView的子类,UIWindow的主要作用:一是提供一个区域来显示UIView,二是将事件(event)的分发给UIView,一个应用基本上只有一个UIWindow,不过也有例外;

创建一个UIWindow:

1.创建一个全频的window
self.window = [[UIWindow alloc] initWithFram:[UIScreen mainScreen].bounds];
2.在window中放入根控制器
self.window.rootViewControl = rootViewControl;
3.将window设置为keyWindow并显示window
[self.window makeKeyAndVisible];
           

获取当前的keyWindow

UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
           

UIScreed可以看成就是手机屏幕,它提供了一个画布,可以在上面画各种视图控件,可以通过[[UIScreen mainScreen] bounds]来获取屏幕尺寸;

iphone屏幕分辨率:
iphone4前的设备:320*480
iphone4和4s:640*960
iphone5:640*1136
ipad   ipad2:1024*768
ipad3  ipad4:2048*1536
ipad mini:1024*768
           

UIView中常用的结构体:

CGPoint point = CGPointMake(x,y);//左上角坐标的位置
CGSize size = CGSizeMake(width,height);//大小
CGRect rect = CGRectMake(x,y,width,height);//位置和大小
           

UIView的常用属性:

frame: 相对父视图的位置和大小
bounds:相对自身的位置和大小,所以bounds的x和y永远为0
center:子视图的中点坐标相对父视图的位置
transform:可以通过这个属性控制视图的放大缩小和旋转
superview:获取父视图
subviews:获取所有子视图
alpha:视图的透明度(0-1)
tag:视图的标志,设置了tag后,可以通过viewWithTag方法拿到这个视图
userInteractionEnabled:是否响应用户事件
           

通过transform属性来对视图进行缩放,旋转

CGAffineTransform transform = rootView.transform;
rootVIew.transform = CGAffineTransformMakeScale(0.5, 0.5);//缩放
rootView.transform = CGAffineTransformScale(transform,0.5,0.5)//在原来的基础上再缩放
rootView.transform = CGAffineTransformMakeRotation(M_2_PI);//旋转传入的角度是弧度制的
rootView.transform = CGAffineTransformRotate(transform,M_PI_4);
rootView.transform = CGAffineTransformMakeTranslation(100, 100);//平移
rootView.transform = CGAffineTransformTranslate(transform, 100, 100);
           

UIView的常用方法:

- (void)removeFromSuperview;将视图从父视图中移除
- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index;插入一个视图到指定位置,视图越在下面,index越小
- (void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2;将index1和index2位置的两个视图互换位置

- (void)addSubview:(UIView *)view;添加视图到父视图
- (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview;插入视图到指定视图的下面
- (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview;插入视图到指定视图上面

- (void)bringSubviewToFront:(UIView *)view;把视图移到最顶层
- (void)sendSubviewToBack:(UIView *)view;把视图移到最底层

- (UIView *)viewWithTag:(NSInteger)tag;     根据视图的tag属性找到搜索视图
           

通过UIView的属性产生一些基本动画

UIVIew中支持动画的属性有以下几个:
frame:可以通过frame来改变视图的尺寸和位置
bounds:可以通过bounds来改变视图的尺寸
center:可以通过center来改变视图的位置
transform:可以通过transform来使视图翻转和缩放,平移
alpha:可以通过alpha修改视图的透明度
backgroundColor:改变视图的背景颜色
contentStetch:改变视图内容如何拉伸
1.
    [UIView beginAnimations:nil context:nil];//开始动画
    [UIView setAnimationDuration:2];//持续时间
    //动画
    CGAffineTransform transform = sender.transform;
    sender.transform = CGAffineTransformMakeTranslation(100, 100);
    sender.transform = CGAffineTransformTranslate(transform, 100, 100);
    
    [UIView commitAnimations];//提交动画
    2.使用block
    [UIView animateWithDuration:2 animations:^{
        CGAffineTransform transform = sender.transform;
        sender.transform = CGAffineTransformMakeTranslation(100, 100);
        sender.transform = CGAffineTransformTranslate(transform, 100, 100);
    }];
    3.使用block
    [UIView animateWithDuration:2 animations:^{
        CGAffineTransform transform = sender.transform;
        sender.transform = CGAffineTransformMakeTranslation(100, 100);
        sender.transform = CGAffineTransformTranslate(transform, 100, 100);
    } completion:^(BOOL finished) {
        //动画完成后调用的代码段
    }];
动画常用设置
[UIView setAnimationDuration:2];动画持续时间
[UIView setAnimationRepeatCount:1];动画重复次数