UIWindow和UIView

来源:互联网 发布:淘宝助理类目空白 编辑:程序博客网 时间:2024/05/21 21:42

基础:

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

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

创建一个UIWindow:

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. 1.创建一个全频的window  
  2. self.window = [[UIWindow alloc] initWithFram:[UIScreen mainScreen].bounds];  
  3. 2.在window中放入根控制器  
  4. self.window.rootViewControl = rootViewControl;  
  5. 3.将window设置为keyWindow并显示window  
  6. [self.window makeKeyAndVisible];  

获取当前的keyWindow

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;  


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

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. iphone屏幕分辨率:  
  2. iphone4前的设备:320*480  
  3. iphone44s:640*960  
  4. iphone5:640*1136  
  5. ipad   ipad2:1024*768  
  6. ipad3  ipad4:2048*1536  
  7. ipad mini:1024*768  

UIView中常用的结构体:

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. CGPoint point = CGPointMake(x,y);//左上角坐标的位置  
  2. CGSize size = CGSizeMake(width,height);//大小  
  3. CGRect rect = CGRectMake(x,y,width,height);//位置和大小  

UIView的常用属性:

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. frame: 相对父视图的位置和大小  
  2. bounds:相对自身的位置和大小,所以bounds的x和y永远为0  
  3. center:子视图的中点坐标相对父视图的位置  
  4. transform:可以通过这个属性控制视图的放大缩小和旋转  
  5. superview:获取父视图  
  6. subviews:获取所有子视图  
  7. alpha:视图的透明度(01)  
  8. tag:视图的标志,设置了tag后,可以通过viewWithTag方法拿到这个视图  
  9. userInteractionEnabled:是否响应用户事件  


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

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. CGAffineTransform transform = rootView.transform;  
  2. rootVIew.transform = CGAffineTransformMakeScale(0.50.5);//缩放  
  3. rootView.transform = CGAffineTransformScale(transform,0.5,0.5)//在原来的基础上再缩放  
  4. rootView.transform = CGAffineTransformMakeRotation(M_2_PI);//旋转传入的角度是弧度制的  
  5. rootView.transform = CGAffineTransformRotate(transform,M_PI_4);  
  6. rootView.transform = CGAffineTransformMakeTranslation(100100);//平移  
  7. rootView.transform = CGAffineTransformTranslate(transform, 100100);  

UIView的常用方法:

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. - (void)removeFromSuperview;将视图从父视图中移除  
  2. - (void)insertSubview:(UIView *)view atIndex:(NSInteger)index;插入一个视图到指定位置,视图越在下面,index越小  
  3. - (void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2;将index1和index2位置的两个视图互换位置  
  4.   
  5. - (void)addSubview:(UIView *)view;添加视图到父视图  
  6. - (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview;插入视图到指定视图的下面  
  7. - (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview;插入视图到指定视图上面  
  8.   
  9. - (void)bringSubviewToFront:(UIView *)view;把视图移到最顶层  
  10. - (void)sendSubviewToBack:(UIView *)view;把视图移到最底层  
  11.   
  12. - (UIView *)viewWithTag:(NSInteger)tag;     根据视图的tag属性找到搜索视图  

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

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. UIVIew中支持动画的属性有以下几个:  
  2. frame:可以通过frame来改变视图的尺寸和位置  
  3. bounds:可以通过bounds来改变视图的尺寸  
  4. center:可以通过center来改变视图的位置  
  5. transform:可以通过transform来使视图翻转和缩放,平移  
  6. alpha:可以通过alpha修改视图的透明度  
  7. backgroundColor:改变视图的背景颜色  
  8. contentStetch:改变视图内容如何拉伸  
  9. 1.  
  10.     [UIView beginAnimations:nil context:nil];//开始动画  
  11.     [UIView setAnimationDuration:2];//持续时间  
  12.     //动画  
  13.     CGAffineTransform transform = sender.transform;  
  14.     sender.transform = CGAffineTransformMakeTranslation(100100);  
  15.     sender.transform = CGAffineTransformTranslate(transform, 100100);  
  16.       
  17.     [UIView commitAnimations];//提交动画  
  18.     2.使用block  
  19.     [UIView animateWithDuration:2 animations:^{  
  20.         CGAffineTransform transform = sender.transform;  
  21.         sender.transform = CGAffineTransformMakeTranslation(100100);  
  22.         sender.transform = CGAffineTransformTranslate(transform, 100100);  
  23.     }];  
  24.     3.使用block  
  25.     [UIView animateWithDuration:2 animations:^{  
  26.         CGAffineTransform transform = sender.transform;  
  27.         sender.transform = CGAffineTransformMakeTranslation(100100);  
  28.         sender.transform = CGAffineTransformTranslate(transform, 100100);  
  29.     } completion:^(BOOL finished) {  
  30.         //动画完成后调用的代码段  
  31.     }];  
  32. 动画常用设置  
  33. [UIView setAnimationDuration:2];动画持续时间  
  34. [UIView setAnimationRepeatCount:1];动画重复次数  
0 0
原创粉丝点击