课程要点:ios
建立一个iOS工程app
PS:接下来简单介绍一下工程里的文件与磁盘里文件的对应关系框架
AppDelegate类函数
建立工程事后我们能够看到系统给我们建立了不少以前我们没有接触过的类,但近一段时间我们只会用到AppDelegate类,其余类以及文件你们目前不须要了解。之后会慢慢的告诉你们。学习
AppDelegate类在工程里的做用相当重要,由于他管理着软件的整个生命周期(例如这个软件的启动,结束,进入后台,进入前台等等)。每次软件作不一样的动做,就会调用AppDelegate.m相应的方法,我们在这些方法内作我们想作的操做就行。目前不须要理解AppDelegate里的东西为何要这样写。只要搞明白怎么用就行。接下来是AppDelegate里相应方法的调用时间。就目前而言我们经常使用的第一个- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法。其余的可作了解。ui
// AppDelegate里第一个进入系统的函数,应用程序完成初始化后调用。 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { return YES; } // 即将进入后台 - (void)applicationWillResignActive:(UIApplication *)application { // 应用程序即将从活跃状态切换到不活跃状态这个函数(消息)就会被调用。这也会在某些临时状态发生(好比来了电话,SMS短信,这个函数也会被调用)或者 当用户退出了应用程序,它开始切换到后台模式 // 咱们应该作什么?用这个方法咱们要作暂停正常运行的任务。关闭定时器,下降OpenGL ES的分辨率。暂停游戏。 // 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. //从应用程序到前台(按Home后)或者锁屏等等 NSLog(@"function %@ is calling",NSStringFromSelector(_cmd)); } // 已经进入后台 - (void)applicationDidEnterBackground:(UIApplication *)application { // 用这个方法来释放共享资源,保存用户数据,做废定时器,保存足够多用户状态信息以便程序被终止,咱们下次还能恢复以前的状态信息 // 若是你的应用程序支持后台调用,当用户退出应用程序(按Home键等)时这个方法就会被调用而不调用applicationWillTerminate消息 // 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(@"function %@ is calling",NSStringFromSelector(_cmd)); } // 即将切换到活跃状态(前台) - (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(@"function %@ is calling",NSStringFromSelector(_cmd)); } // 已经进入活跃状态(前台) - (void)applicationDidBecomeActive:(UIApplication *)application { // 从新启动被暂停的任务。若是应用程序在后台,那么这里要刷新用户界面(UI) // 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(@"function %@ is calling",NSStringFromSelector(_cmd)); } //终止调用函数(ios4.0不支持多任务,按Home键会调用这个函数杀死应用程序;ios4.0后按Home键将挂起应用程序这个函数就不会调用,若想调用就必须在plist给Application does not run in background键赋值TRUE) - (void)applicationWillTerminate:(UIApplication *)application { // 当应用程序中止时会调用这个函数,在适当状况下保存数据。这个函数只有在<ios4.0时调用或者ios>4.0设置了不能后台模式时 // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. NSLog(@"function %@ is calling",NSStringFromSelector(_cmd)); }
UIKit框架以及UIWindowthis
iOS:苹果移动设备的操做系统 OS是mac的操做系统atom
UIKit->里面包含了全部能够看的见得视图控件spa
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { /* * window : 在软件内至关于一个平台或者载体,他承载着软件内全部的试图。一个应用程序一般只有一个窗口,但也有例外。 * 做用: 它包含了应用程序的可视化的内容 它为视图和其余应用程序对象在触摸事件中提供了关键性的做用 它与视图控制器一块儿协做来呈现数据 */ //工程一建立完成,系统会自动会实例化一个window,只有设置了[self.window makeKeyAndVisible]; 才能在方法内给window添加试图。 NSLog(@"自动建立的window=%@",self.window); //在控制台能够看到输出的并非空,由此可验证系统确实自动给生成了window对象 [self.window makeKeyAndVisible]; //如下三行代码是建立一个红色试图并把他放在window上 UIView *view = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 200, 333)]; view.backgroundColor = [UIColor redColor]; [self.window addSubview:view]; return YES; }
运行后,模拟器截图:操作系统
若我将 [self.window makeKeyAndVisible]; 给注释掉,运行后,模拟器截图:
PS:这个证实了若是没有[self.window makeKeyAndVisible];,确实不能在AppDelegate.m中的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法里给window添加试图。
在window上添加第一个试图UIView
/* * 创将一个试图并将试图放到window上的最基本三步走。 一、建立一个试图对象 二、告诉系统你要将这个试图对象放在父试图的哪一个位置,以及这个试图对象的宽高 三、告诉系统这个试图对象的父试图是谁。 * PS:这三步是必不可少的。切记 */ //建立一个试图对象 UIView *view = [[UIView alloc]init]; //告诉系统你要将这个试图对象放在父试图的哪一个位置,以及这个试图对象的宽高。
/*
* frame: 这个属性,用来肯定试图对象的x,y,宽,高。frame他是CGRect类型,CGRect里存放在这四个元素。
* CGRect: 这是一个结构体,内容以下:
struct CGRect {
CGPoint origin;
CGSize size;
};typedef struct CGRect CGRect;
* CGPoint:这也是一个结构体,用来肯定试图对象的x,y,内容以下:
struct CGPoint {
CGFloat x;
CGFloat y;
};typedef struct CGPoint CGPoint;
* CGSize:一样是一个结构体,用来确实试图对象的宽和高,内容以下:
struct CGSize {
CGFloat width;
CGFloat height;
};typedef struct CGSize CGSize
*/
//除了有
view.frame = CGRectMake(100, 100, 200, 333);
//告诉系统这个试图对象的父试图是谁。经过addSubView将view放到了self.window中
[self.window addSubview:view];
/* * UIView的经常使用属性 * backGroundColor:背景颜色 * alpha:透明度 * subViews:子试图集合 子试图:就是说放在view上面的试图就都叫作他的子试图,子试图能够有多个。人能够有多个子女。 * hidden:是否隐藏 * tag:标签值 * superview:父试图 父试图:就是承载view的那个试图,一个试图只有一个父试图。人只有一个父亲。 * multipleTouchEnabled:是否开启多点触摸 * userInteractionEnabled:是否响应触摸事件 * PS:目前前六种必须所有掌握,后两种,目前不经常使用,但必须知道是什么意思。 */ //backGroundColor view.backgroundColor = [UIColor redColor]; //透明度 0是彻底透明 1是彻底不透明 view.alpha = 0.5f; //子试图集合 由此得到放在view上的全部试图 NSArray *viewArray = [view subviews]; NSLog(@"放在view上的试图有%ld个",viewArray.count); NSArray *windowArray = [self.window subviews]; NSLog(@"放在self.window上的试图有%ld个",windowArray.count); //PS:输出self.window的子试图时,会有两个,一个是我们建立的试图,而另外一个就是window上自带的一个试图,就比如桌子上的桌布。 //hidden YES就隐藏,NO就不隐藏 view.hidden = YES; view.hidden = NO; //tag 标签值,给一个试图设置标签值咱就能够经过标签值来寻找一个试图 view.tag = 888; for (UIView *view in self.window.subviews) { if (view.tag == 888) { NSLog(@"找到了我标记的试图=%@",view); } } //superview 找到该试图的父试图 NSLog(@"view的父试图是%@",[view superview]);
UIView的经常使用方法:
//将视图从父视图中移除 - (void)removeFromSuperview [self.lblTest removeFromSuperview]; //插入一个视图到指定位置 - (void)insertSubview:(UIView *)view atIndex:(NSInteger)index
NSInteger myIndex = 0; if(self.myView1.subviews.count>0) { myIndex = self.myView1.subviews.count; } [self.myView1 insertSubview:self.lblTest atIndex:myIndex]; //将index1和index2位置的两个视图互换位置 - (void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2 NSInteger index1 = [self.view.subviews indexOfObject:self.lblTest1]; NSInteger index2 = [self.view.subviews indexOfObject:self.lblTest2]; [self.view exchangeSubviewAtIndex:index1 withSubviewAtIndex:index2]; //添加子视图 - (void)addSubview:(UIView *)view [self.myView1 addSubview:self.lblTest]; //插入视图到指定位置 - (void)insertSubview:(UIView *)view atIndex:(NSInteger)index [self.view insertSubview:self.lblTest atIndex:0]; //插入视图到指定视图的下面 - (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview [self.view insertSubview:self.lblTest1 belowSubview:self.lblTest2]; //插入视图到指定视图的上面 - (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview [self.view insertSubview:self.lblTest1 aboveSubview:self.lblTest2]; //将指定子视图移到最顶层 - (void)bringSubviewToFront:(UIView *)view [self.view bringSubviewToFront:self.lblTest]; //将指定子视图移到最底层 - (void)sendSubviewToBack:(UIView *)view [self.view sendSubviewToBack:self.lblTest]; //根据视图的tag查找视图 - (UIView *)viewWithTag:(NSInteger)tag [self.lblTest setTag:5]; [self.view viewWithTag:5].alpha = 0; //取得视图下的全部子视图 @property(nonatomic, readonly, copy) NSArray *subviews NSArray *arrView = [self.view subviews]; for (UIView *view1 in arrView) { NSLog(@"%@",view1); }
PS:以前我们说过OC是一门面向对象的语言,为何要面向对象呢,由于对象本身可以实现一些功能,好比如今咱们学的试图,我们给一个坐标就能本身找到位置,给什么个颜色就能本身把本身染成什么颜色。这即是面向对象的一种体现。
NSTimer(定时器)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { /* * NSTimer:定时器 * intervarl: 设置定时时间 * target:接收时间到了谁来接收这个信号 * selector: 时间到了要作什么 * userInfor:要传入的参数,通常置nil * repeats: 是否重复 YES重复 NO不重复 */
// 若是此时@selector(start:),start后面有冒号(有冒号就表明这个方法有参数),实现的时候咱就用- (void)start:(NSTimer *)timer,不然- (void)start;
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(start:) userInfo:@"时间到" repeats:YES]; //让定时器当即调用指定方法 // [timer fire]; // //释放这个定时器// [timer invalidate]; // // //开启定时器 // timer.fireDate = [NSDate distantPast]; // //关闭定时器 // timer.fireDate = [NSDate distantFuture]; return YES; }
//在设置定时器的时候,这个方法已经用@selector声明过了,若是此时你不把他实现,运行就会奔溃。
- (void)start:(NSTimer *)timer
{
NSLog(@"%@",[timer userInfo]);
}
以上是我的看法,如有错误欢迎指正,在学习中如有不理解欢迎骚扰 QQ:2314858225