标签:
main
#import <UIKit/UIKit.h> #import "AppDelegate.h" //UIApplication对象:这是应用程序的象征,是iOS程序第一个创建的对象。每一个程序都有自己的UIApplication对象 //UIApplication对象的创建:[UIApplication sharedApplication],利用这个单例对象,能进行应用级别的操作 //UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])) //第一个参数:argv数组元素的个数 //第二个参数:程序名(位置-沙盒) //第三个参数:指定应用程序第一个类名,若为nil,则UIApplication类作为默认类(委托者) //第四个参数:指定应用代理类AppDelegate class,应遵循UIApplicationDelegate协议 //UIApplicationMain作用 //1.主要是根据principalClassName和delegateClassName)来创建UIApplication对象和delegate对象,并将delegate对象赋值给UIApplication对象的delegate属性 //2.然后建立应用程序的Main Runloop(事件循环) //3.程序正常退出后,UIApplicationMain函数才返回 int main(int argc, char * argv[]) { @autoreleasepool { [UIApplication sharedApplication];//利用这个单例创建UIApplication对象 NSLog(@"%i",argc);//1个元素 NSLog(@"%s",argv[0]);//沙盒位置 return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); } }
AppDelegate.h
#import <UIKit/UIKit.h> //UIApplicationDelegate遵循协议 //三者继承关系 //UIWindow:UIView:UIResponder //iOS支持单一的窗口。 //UIWindow 是特殊的UIView,通常一个app只有一个UIWindow. //iOS程序启动后,创建的第一个视图空间就是UIWindow。然后创建view... @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end
AppDelegate.m
#import "AppDelegate.h" @interface AppDelegate () @end @implementation AppDelegate -(void)dealloc { [super dealloc]; _window = nil; } //循环事件 //测试-运行-home-进入程序 //2015-08-24 18:02:15.410 测试1[5754:150034] 程序启动完成 //2015-08-24 18:02:15.414 测试1[5754:150034] 进入活动状态(获取焦点) //2015-08-24 18:02:35.603 测试1[5754:150034] 即将进入非活动状态(失去焦点,不能响应事件,例如打电话) //2015-08-24 18:02:36.124 测试1[5754:150034] 进入后台(保存数据或者状态) //2015-08-24 18:02:50.651 测试1[5754:150034] 即将从后台进入前台(恢复数据或状态) //2015-08-24 18:02:51.161 测试1[5754:150034] 进入活动状态(获取焦点) //2015-08-24 18:03:31.251 测试1[5754:150034] 即将进入非活动状态(失去焦点,不能响应事件,例如打电话) //2015-08-24 18:03:31.779 测试1[5754:150034] 进入后台(保存数据或者状态) #pragma make 1程序启动完成 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. NSLog(@"%s", __func__); NSLog(@"程序启动完成"); //代码区 //一个iOS程序之所以能显示在屏幕上,就是因为有UIWindow,否则就看不见任何UI控件 //匹配屏幕大小[UIScreen mainScreen].bounds] self.window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; self.window.backgroundColor=[UIColor whiteColor]; //将self.window在当前设备显示 [self.window makeKeyAndVisible]; UIView*view1=[[UIView alloc]initWithFrame:CGRectMake(100, 80, 100, 100)]; view1.backgroundColor=[UIColor redColor]; //将view1添加到window,引用计数器+1 [self.window addSubview:view1]; [view1 release]; UIView*view2=[[UIView alloc]initWithFrame:CGRectMake(100, 200, 100, 100)]; view2.backgroundColor=[UIColor greenColor]; [self.window addSubview:view2]; [view2 release]; UIView*view3=[[UIView alloc]initWithFrame:CGRectMake(0, 310, 100, 100)]; view3.backgroundColor=[UIColor yellowColor]; [self.window addSubview:view3]; [view3 release]; UIView*view4=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)]; view4.backgroundColor=[UIColor blueColor]; [view3 addSubview:view4]; [view4 release]; UIView*view5=[[UIView alloc]initWithFrame:CGRectMake(20, 20, 50, 50)]; view5.backgroundColor=[UIColor grayColor]; [view3 addSubview:view5]; [view5 release]; UIView *view6 = [[UIView alloc]initWithFrame:CGRectMake(10, 10, 100, 30)]; view6.backgroundColor = [UIColor orangeColor]; //查看多少个view; NSLog(@"%@",[self.window subviews]); //insertSubview //插入子视图到指定索引位置insertSubview [view3 insertSubview:view6 atIndex:1]; //视图插入在指定视图之上 [view3 insertSubview:view6 aboveSubview:view5]; //视图插入在指定视图之下 [view3 insertSubview:view6 belowSubview:view5]; //exchangeSubviewAtIndex:withSubviewAtIndex: [view3 exchangeSubviewAtIndex:1 withSubviewAtIndex:2]; //将视图移动到最上层bringSubviewToFront [view3 bringSubviewToFront:view4]; //将视图移动到最下层sendSubviewToBack [view3 sendSubviewToBack:view4]; //将所有子视图删除removeFromSuperview //子视图的父视图也被删除 //[view3 removeFromSuperview]; //隐藏 view5.hidden=YES; //tag view3.tag=10; NSLog(@"%@",[view3 superview]); // <UIWindow: 0x7fb3d0d2c810; frame = (0 0; 375 667); NSLog(@"%@",[self.window viewWithTag:10]); //<UIView: 0x7f7fabd29cf0; frame = (0 310; 100 100); tag = 10; layer = //是否支持用户响应 //view3.userInteractionEnabled=YES; //裁剪clipsToBounds view3.clipsToBounds=YES; //动画 //1开始动画 [UIView beginAnimations:nil context:nil]; //2设置动画持续时间 [UIView setAnimationDuration:3]; //3动画内容 CGRect frame=CGRectMake(view1.frame.origin.x+100, view1.frame.origin.y, view1.frame.size.width, view1.frame.size.height); view1.frame=frame; //4提交动画 [UIView commitAnimations]; //使用block实现动画 [UIView animateWithDuration:3 animations:^{ CGRect frame=CGRectMake(view1.frame.origin.x, view1.frame.origin.y+100, view1.frame.size.width, view1.frame.size.height); view1.frame=frame; //平移 view3.transform=CGAffineTransformMakeTranslation(50, 100); //缩放 view2.transform=CGAffineTransformMakeScale(0.5, 0.5); //旋转 view3.transform=CGAffineTransformMakeRotation(M_PI); } completion:^(BOOL finished) { if(finished){ NSLog(@"%@", NSStringFromCGRect(view2.frame)); // view1.hidden = YES; [UIView animateWithDuration:2 animations:^{ view1.alpha = 0; view3.layer.cornerRadius = 50; view2.layer.cornerRadius = 50; }completion:nil]; } }]; return YES; } #pragma make 3即将进入非活动状态(失去焦点,不能响应事件,例如打电话) - (void)applicationWillResignActive:(UIApplication *)application { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. NSLog(@"即将进入非活动状态(失去焦点,不能响应事件,例如打电话)"); } #pragma make 4进入后台(保存数据或者状态) - (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. NSLog(@"进入后台(保存数据或者状态)"); } #pragma make 5即将从后台进入前台(恢复数据或状态) - (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. NSLog(@"即将从后台进入前台(恢复数据或状态)"); } #pragma make 2进入活动状态(获取焦点) - (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. NSLog(@"进入活动状态(获取焦点)"); } #pragma make 即将退出程序 - (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. NSLog(@"即将退出程序"); } #pragma make 内存过低时,发出内存警告(释放不必要的内存) - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application { NSLog(@"内存警告"); } @end
标签:
原文地址:http://www.cnblogs.com/sunbinboke/p/4758555.html