NSTimer在IOS开发中会常常用到,尤为是小型游戏,然而对于初学者时常会注意不到其中的内存释放问题,将其基本用法总结以下:ide
1、初始化方法:有五种初始化方法,分别是oop
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo;ui
- (void)viewDidLoad { [super viewDidLoad]; //初始化一个Invocation对象 NSInvocation * invo = [NSInvocation invocationWithMethodSignature:[[self class] instanceMethodSignatureForSelector:@selector(init)]]; [invo setTarget:self]; [invo setSelector:@selector(myLog)]; NSTimer * timer = [NSTimer timerWithTimeInterval:1 invocation:invo repeats:YES]; //加入主循环池中 [[NSRunLoop mainRunLoop]addTimer:timer forMode:NSDefaultRunLoopMode]; //开始循环 [timer fire]; }
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo;.net
NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:1 invocation:invo repeats:YES];
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;code
NSTimer * timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(myLog) userInfo:nil repeats:NO]
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;对象
NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(myLog:) userInfo:@"123" repeats:YES]
- (instancetype)initWithFireDate:(NSDate *)date interval:(NSTimeInterval)ti target:(id)t selector:(SEL)s userInfo:(id)ui repeats:(BOOL)rep 游戏
NSTimer * timer = [[NSTimer alloc]initWithFireDate:[NSDate distantPast] interval:1 target:self selector:@selector(myLog:) userInfo:nil repeats:YES]; [[NSRunLoop mainRunLoop]addTimer:timer forMode:NSDefaultRunLoopMode];
注意:这五种初始化方法的异同:内存
一、参数repeats是指定是否循环执行,YES将循环,NO将只执行一次。开发
二、timerWithTimeInterval这两个类方法建立出来的对象若是不用 addTimer: forMode方法手动加入主循环池中,将不会循环执行。而且若是不手动调用fair,则定时器不会启动。文档
三、scheduledTimerWithTimeInterval这两个方法不须要手动调用fair,会自动执行,而且自动加入主循环池。
四、init方法须要手动加入循环池,它会在设定的启动时间启动。
2、成员变量
@property (copy) NSDate *fireDate;
这是设置定时器的启动时间,经常使用来管理定时器的启动与中止
//启动定时器 timer.fireDate = [NSDate distantPast]; //中止定时器 timer.fireDate = [NSDate distantFuture];
@property (readonly) NSTimeInterval timeInterval;
这个是一个只读属性,获取定时器调用间隔时间。
@property NSTimeInterval tolerance;
这是7.0以后新增的一个属性,由于NSTimer并不彻底精准,经过这个值设置偏差范围。
@property (readonly, getter=isValid) BOOL valid;
获取定时器是否有效
@property (readonly, retain) id userInfo;
获取参数信息
3、关于内存释放
若是咱们启动了一个定时器,在某个界面释放前,将这个定时器中止,甚至置为nil,都不能是这个界面释放,缘由是系统的循环池中还保有这个对象。因此咱们须要这样作:
-(void)dealloc{ NSLog(@"dealloc:%@",[self class]); } - (void)viewDidLoad { [super viewDidLoad]; timer= [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(myLog:) userInfo:nil repeats:YES]; UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 100)]; btn.backgroundColor=[UIColor redColor]; [btn addTarget:self action:@selector(btn) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; } -(void)btn{ if (timer.isValid) { [timer invalidate]; } timer=nil; [self dismissViewControllerAnimated:YES completion:nil]; }
在官方文档中咱们能够看到 [timer invalidate]是惟一的方法将定时器从循环池中移除。
专一技术,热爱生活,交流技术,也作朋友。
——珲少 QQ群:203317592