最新国产好看的视频,伊人天堂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)運行在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ì)的代碼可以點擊這里下載: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

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

invalidate

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

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

再次運行,還是沒有停止。原因是 Timer 添加到 Runloop 的時候,會被 Runloop 強引用:

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 的強引用(也就是 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 強引用了 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 強引用了,如果要釋放就要調(diào)用 invalidate 方法。
  • 但是我想在 DemoViewController 的 dealloc 里調(diào)用 invalidate 方法,但是 self 被 NSTimer 強引用了。
  • 所以我還是要釋放 NSTimer 先,然而不調(diào)用 invalidate 方法就不能釋放它。
  • 然而你不進入到 dealloc 方法里我又不能調(diào)用 invalidate 方法。
  • 嗯…


相關(guān)文章

最新評論

罗甸县| 崇左市| 禄丰县| 鄂尔多斯市| 拉萨市| 彝良县| 乐平市| 开江县| 廊坊市| 金堂县| 游戏| 湘乡市| 石家庄市| 平度市| 响水县| 浦江县| 岳西县| 宁城县| 溧阳市| 夏邑县| 宜丰县| 克东县| 安阳县| 张北县| 永和县| 额济纳旗| 文成县| 湟源县| 合水县| 井冈山市| 淮北市| 深泽县| 安乡县| 河北省| 渝北区| 隆尧县| 全椒县| 沅陵县| 丰台区| 大石桥市| 峨山|