IOS NSTimer 定時器用法總結

來源:互聯網
上載者:User

標籤:

NSTimer在IOS開發中會經常用到,尤其是小型遊戲,然而對於初學者時常會注意不到其中的記憶體釋放問題,將其基本用法總結如下:

一、初始化方法:有五種初始化方法,分別是

+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo;

- (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;

  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;

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];

注意:這五種初始化方法的異同:

        1、參數repeats是指定是否迴圈執行,YES將迴圈,NO將只執行一次。

        2、timerWithTimeInterval這兩個類方法建立出來的對象如果不用 addTimer: forMode方法手動加入主迴圈池中,將不會迴圈執行。並且如果不手動調用fair,則定時器不會啟動。

        3、scheduledTimerWithTimeInterval這兩個方法不需要手動調用fair,會自動執行,並且自動加入主迴圈池。

        4、init方法需要手動加入迴圈池,它會在設定的啟動時間啟動。

二、成員變數

@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;

擷取參數資訊

三、關於記憶體釋放

如果我們啟動了一個定時器,在某個介面釋放前,將這個定時器停止,甚至置為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]是唯一的方法將定時器從迴圈池中移除。


IOS NSTimer 定時器用法總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.