1.定時器: 過兩秒鐘 執(zhí)行一件事 (找一個對象self 執(zhí)行對象中指定的方法@selector:(nextImage))
NSTimer *timer = [NSTimer timerWithTimeInterval: 2.0 target: self selector:@selector:(nextImage) userInfo: nil repeats:YES ];
2.消息循環(huán)
NSRunLoop *runloop = [NSRunLoop currentRunLoop];
[runloop addTimer:timer forMode:NSDefaultRunLoopMode]; // 把定時
器加入到消息循環(huán)中
// NSDefaultRunLoopMode 默認模式 NSRunLoopCommonModes (一個人當兩個人來用 )
3. 這個方法相當于 【定時器 + 消息循環(huán)】
NSTimer *timer = [NSTimer scheduledTimerWithTimeInteral:2.0 target:self selector :@selector(nextImage) userInfo:nil repeats:YES];
[timer fire ]; // 立即執(zhí)行nextImage 方法
//執(zhí)行main 函數(shù)的那個人 就是主線程
? NSTimer叫做“定時器”,它的作用如下
? 在指定的時間執(zhí)行指定的任務
? 每隔一段時間執(zhí)行指定的任務
?
? 調(diào)用下面的方法就會開啟一個定時任務
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget
selector:(SEL)aSelector
userInfo:(id)userInfo
repeats:(BOOL)yesOrNo;
每隔ti秒,調(diào)用一次aTarget的aSelector方法,yesOrNo決定了是否重復執(zhí)行這個任務
通過invalidate方法可以停止定時器的工作,一旦定時器被停止了,就不能再次執(zhí)行任務。只能再創(chuàng)建一個新的定時器才能執(zhí)行新的任務
//停止定時器
- (void)invalidate; // 使無效,無價值