您的位置:首页 > 产品设计 > UI/UE

IOS开发之UIView

2016-04-05 00:00 441 查看
摘要: 创建两个view,一个view在另一个里面,当程序进入非活跃状态时候,两个View变化不同的颜色
#import "AppDelegate.h"@interface  AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {// Override point for customization after application launch.//1.创建window,设置frame坐标是(0,0)大小是屏幕大小//[UIScreen mainScreen].bounds]中bounds的类型是CGRect;//x和y值是0,w和h分别是屏幕的宽度和屏幕的高度_window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];//2.设置背景颜色[_window setBackgroundColor:[UIColor blackColor]];//在这儿来创建界面//==============UIView=============//UIView是所有视图类直接或者间接的父类,UIView所有的属性和方法其他视图类都有//(1)创建一个UIView对象:UIView *view1 = [[UIView alloc] init];//(2)frame属性://视图想要想要显示在界面上,必须设置frame属性,告诉系统显示的位置和大小;//frame属性中的坐标是相对坐标,将当前视图添加到哪个视图上,就是相对于哪个视图的坐标//OC中所有的结构体都对应一个make方法来快速的创建结构体变量[view1 setFrame:CGRectMake(0,20,200 ,100)];//(3)设置背景颜色(通过类方法创建颜色)[view1 setBackgroundColor:[UIColor cyanColor]];//(4)将这个视图对象view1添加到指定的视图上[_window addSubview:view1];[view1 setTag:11];//=============再创建一个View==================//(1)创建一个UIView对象,并且设置其frameUIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(10, 30, 50, 50)];//(2)设置背景颜色[view2 setBackgroundColor:[UIColor redColor]];//(3)将视图添加到另外一个视图上//_window是view2的父视图,view2就是_window的子视图[_window addSubview:view2];//(4)设置tag值://作用:a.父视图通过tag值获取指定的视图,如果不设置所有视图的tag值都是0//b.[view2 setTag:10];//3.设置为主窗口并显示[_window makeKeyAndVisible];return YES;}- (void)applicationWillResignActive:(UIApplication *)application {//1.通过tag值去获取当前视图上的子视图;[[_window viewWithTag:10] setBackgroundColor:[UIColor greenColor]];//2.获取当前视图上的所有子视图NSArray *viewArray = [_window subviews];NSLog(@"%@",viewArray);//遍历数组中所有的视图for (UIView *view in viewArray) {if (view.tag == 11) {[view setBackgroundColor:[UIColor yellowColor]];}else if(view.tag == 10){view.backgroundColor = [UIColor whiteColor];}}}- (void)applicationDidEnterBackground:(UIApplication *)application {// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.}- (void)applicationWillEnterForeground:(UIApplication *)application {// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.}- (void)applicationDidBecomeActive:(UIApplication *)application {// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.}- (void)applicationWillTerminate:(UIApplication *)application {// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.}@end
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息