最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS中的NSTimer定時器的初步使用解析

 更新時間:2016年05月03日 09:20:18   作者:李剛  
這篇文章主要介紹了iOS中的NSTimer定時器的初步使用解析,通過例子簡單講解了NSTimer的輸出與停止的方法,需要的朋友可以參考下

創(chuàng)建一個定時器(NSTimer)

- (void)viewDidLoad {
  [super viewDidLoad];
  [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(actionTimer:) userInfo:nil repeats:YES];
}

- (void)actionTimer:(NSTimer *)timer
{

}

NSTimer默認(rèn)運(yùn)行在default mode下,default mode幾乎包括所有輸入源(除NSConnection) NSDefaultRunLoopMode模式。

actionTimer方法會每隔1s中被調(diào)用一次。NSTimer使用起來是不是非常簡單。這是NSTimer比較初級的應(yīng)用。

當(dāng)主界面被滑動時NSTimer失效了

主界面被滑動是什么意思呢?就是說主界面有UITableView或者UIScrollView,滑動UITableView或者UIScrollView。這個時候NSTimer失效了。

我們來寫一個demo,在一個有UITableView的UIViewController上啟動定時器,每1s數(shù)字加1,并將這個數(shù)字顯示在UILabel上面.

- (void)viewDidLoad {
  [super viewDidLoad];
  [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(actionTimer:) userInfo:nil repeats:YES];
}

- (void)actionTimer:(NSTimer *)timer
{
  self.number++;
  self.label.text = [NSString stringWithFormat:@"%d",self.number];
  NSLog(@"%d",self.number);
}

關(guān)于UITableView和UILabel的創(chuàng)建我省去了。詳細(xì)的代碼可以點(diǎn)擊這里下載:iOSStrongDemo,iOSStrongDemo我會不斷更新,大家在github上star一下。

這樣當(dāng)用戶在拖動UITableView處于UITrackingRunLoopMode時,NSTimer就失效了,不能fire。self.label上的數(shù)字也就無法更新。

修改NSTimer的run loop

解決方法就是將其加入到UITrackingRunLoopMode模式或NSRunLoopCommonModes模式中。

[[NSRunLoop currentRunLoop] addTimer:timer forMode:UITrackingRunLoopMode];

或者

[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

NSRunLoopCommonModes:是一個模式集合,當(dāng)綁定一個事件源到這個模式集合的時候就相當(dāng)于綁定到了集合內(nèi)的每一個模式。

fire

我們先用 NSTimer 來做個簡單的計時器,每隔5秒鐘在控制臺輸出 Fire 。比較想當(dāng)然的做法是這樣的:

@interface DetailViewController ()
@property (nonatomic, weak) NSTimer *timer;
@end

@implementation DetailViewController
- (IBAction)fireButtonPressed:(id)sender {
  _timer = [NSTimer scheduledTimerWithTimeInterval:3.0f
                       target:self
                      selector:@selector(timerFire:)
                      userInfo:nil
                       repeats:YES];
  [_timer fire];
}

-(void)timerFire:(id)userinfo {
  NSLog(@"Fire");
}
@end

運(yùn)行之后確實在控制臺每隔3秒鐘輸出一次 Fire ,然而當(dāng)我們從這個界面跳轉(zhuǎn)到其他界面的時候卻發(fā)現(xiàn):控制臺還在源源不斷的輸出著 Fire ??磥?Timer 并沒有停止。

invalidate

既然沒有停止,那我們在 DemoViewController 的 dealloc 里加上 invalidate 的方法:

-(void)dealloc {
  [_timer invalidate];
  NSLog(@"%@ dealloc", NSStringFromClass([self class]));
}

再次運(yùn)行,還是沒有停止。原因是 Timer 添加到 Runloop 的時候,會被 Runloop 強(qiáng)引用:

Note in particular that run loops maintain strong references to their timers, so you don't have to maintain your own strong reference to a timer after you have added it to a run loop.
然后 Timer 又會有一個對 Target 的強(qiáng)引用(也就是 self ):

Target is the object to which to send the message specified by aSelector when the timer fires. The timer maintains a strong reference to target until it (the timer) is invalidated.
也就是說 NSTimer 強(qiáng)引用了 self ,導(dǎo)致 self 一直不能被釋放掉,所以也就走不到 self 的 dealloc 里。

既然如此,那我們可以再加個 invalidate 按鈕:

- (IBAction)invalidateButtonPressed:(id)sender {
  [_timer invalidate];
}

嗯這樣就可以了。(在 SOF 上有人說該在 invalidate 之后執(zhí)行 _timer = nil ,未能理解為什么,如果你知道原因可以告訴我:)

在 invalidate 方法的文檔里還有這這樣一段話:

You must send this message from the thread on which the timer was installed. If you send this message from another thread, the input source associated with the timer may not be removed from its run loop, which could prevent the thread from exiting properly.
NSTimer 在哪個線程創(chuàng)建就要在哪個線程停止,否則會導(dǎo)致資源不能被正確的釋放??雌饋砀鞣N坑還不少。

dealloc

那么問題來了:如果我就是想讓這個 NSTimer 一直輸出,直到 DemoViewController 銷毀了才停止,我該如何讓它停止呢?

  • NSTimer 被 Runloop 強(qiáng)引用了,如果要釋放就要調(diào)用 invalidate 方法。
  • 但是我想在 DemoViewController 的 dealloc 里調(diào)用 invalidate 方法,但是 self 被 NSTimer 強(qiáng)引用了。
  • 所以我還是要釋放 NSTimer 先,然而不調(diào)用 invalidate 方法就不能釋放它。
  • 然而你不進(jìn)入到 dealloc 方法里我又不能調(diào)用 invalidate 方法。
  • 嗯…


相關(guān)文章

最新評論

苏州市| 前郭尔| 武陟县| 天津市| 当阳市| 新化县| 康乐县| 集贤县| 石首市| 西畴县| 台南市| 铁岭县| 二手房| 天水市| 阿克| 内丘县| 清苑县| 特克斯县| 盐边县| 德保县| 兰考县| 静安区| 镇沅| 库车县| 平果县| 淮阳县| 舞钢市| 海城市| 行唐县| 松滋市| 石家庄市| 桐梓县| 罗定市| 三门县| 乌拉特后旗| 乐清市| 昌都县| 重庆市| 梁山县| 常德市| 凤翔县|