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