iOS(学习4) UIWindow 和 UIView

iOS应用中使用UIWindow、UIView来实现内容显示。

UIWindow:

UIWindow对象是所有UIView的根视图,管理和协调的应用程序的显示、分发事件给View。UIWindow类是UIView的子类,可以看作是特殊的UIView。一般应用程序只有一个UIWindow对象,即使有多个UIWindow对象,也只有一个UIWindow可以接受到用户的触屏事件。UIWindow初始化在appDeleDgate里面的 - (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions方法。

@implementation AppDelegate 


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

    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];  
    //self.window.backgroundColor = [UIColor grayColor]; 
    //使用对象的主窗口显示到屏幕的最前端 
    [self.window makeKeyAndVisible];  

    // ViewController 显示最前端 
    self.window.rootViewController = [[ViewController alloc] init];  

    //切换根视图控制器,让OneViewController作为根视图控制器 
    //self.window.rootViewController = [[OneViewController alloc] init]; 

    //带 .xib 文件的初始化 
    //self.window.rootViewController = [[TwoViewController alloc] initWithNibName:@"TwoViewController" bundle:[NSBundle mainBundle]]; 

    return YES;  
}  

UIVIew

UIView类继承自UIResponder,负责在屏幕上 定义一个矩形区域,视图用于展示界面及响应用户界面交互。每个视图对象都要负责渲染视图区域的内容,并响应该区域中发生的操作事件。
UIView为所有控件父类

这里做一个简单动画效果

#import "ViewController.h" 

@interface ViewController () 
{  
    BOOL flag;  

}  
@end  



@implementation ViewController 

- (void)viewDidLoad {  
    [super viewDidLoad];  

    // 当前视图颜色 
    self.view.backgroundColor = [UIColor grayColor];  

    UIView *oneView = [[UIView alloc] initWithFrame:CGRectMake(40, 20, 60, 60)];  
    //每一个View都有一个tag的属性 
    oneView.tag = 1000;  
    //设置圆角 
    oneView.layer.cornerRadius = 5;  
    //告诉layer将位于它之下的layer都遮盖住 
    oneView.layer.masksToBounds = YES;  
    //边框 颜色及 粗细 
    oneView.layer.borderColor = [UIColor greenColor].CGColor;  
    oneView.layer.borderWidth = 1.0;  


    flag = YES;  
    oneView.backgroundColor = flag?[UIColor redColor]:[UIColor yellowColor];  
    //每个视图都有添加子视图的方法:addSubview 
    [self.view addSubview:oneView];  

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];  
    button.frame = CGRectMake(60, 150, 40, 30);  
    [button setTitle:@"点击" forState:UIControlStateNormal];  
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];  
    [self.view addSubview:button];  

}  

- (void)buttonAction:(UIButton *)sender {  

    UIView *view = [self.view viewWithTag:1000];  
    flag = !flag;  
     view.backgroundColor = flag?[UIColor redColor]:[UIColor yellowColor];  
    if (!flag) {  
        [UIView animateWithDuration:2.0 animations:^{  
            view.frame = CGRectMake(200, 20, 160, 160);  
        }];  
    } else {  
        [UIView animateWithDuration:2.0 animations:^{  
            view.frame = CGRectMake(40, 20, 60, 60);  
        }];  
    }  
}  

- (void)didReceiveMemoryWarning {  
    [super didReceiveMemoryWarning];  
    // Dispose of any resources that can be recreated. 
}  

@end  

你可能感兴趣的:(ios)