ios nstimer实现延时_iOS中定时器NSTimer的使用
1、初始化
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;
注:不用scheduled方式初始化的,需要手動addTimer:forMode: 將timer添加到一個runloop中。
而scheduled的初始化方法將以默認(rèn)mode直接添加到當(dāng)前的runloop中.
舉例:
NSTimer *timer?= [NSTimer?scheduledTimerWithTimeInterval:10.0?target:self?selector:@selector(timerFired:)?userInfo:nil?repeats:NO];
或
NSTimer *myTimer= [NSTimertimerWithTimeInterval:3.0target:selfselector:@selector(timerFired:)userInfo:nilrepeats:NO];
[[NSRunLoopcurrentRunLoop]addTimer:myTimerforMode:NSDefaultRunLoopMode];
2、觸發(fā)(啟動)
當(dāng)定時器創(chuàng)建完(不用scheduled的,添加到runloop中后,該定時器將在初始化時指定的timeInterval秒后自動觸發(fā)。
可以使用-(void)fire;方法來立即觸發(fā)該定時器;
注:You can use this method to fire a repeating timer without interrupting its regular firing schedule. If the timer is non-repeating, it is automatically invalidated after firing, even if its scheduled fire date has not arrived.
在重復(fù)執(zhí)行的定時器中調(diào)用此方法后立即觸發(fā)該定時器,但不會中斷其之前的執(zhí)行計劃;
在不重復(fù)執(zhí)行的定時器中調(diào)用此方法,立即觸發(fā)后,就會使這個定時器失效。
3、停止
- (void)invalidate;
這個是唯一一個可以將計時器從runloop中移出的方法。
注:
NSTimer可以精確到50-100毫秒.
NSTimer不是絕對準(zhǔn)確的,而且中間耗時或阻塞錯過下一個點,那么下一個點就pass過去了.
延時函數(shù)和Timer的使用
//延時函數(shù):[NSThread sleepForTimeInterval:5.0]; //暫停5s.//Timer的使用:NSTimer *connectionTimer; //timer對象//實例化timerself.connectionTimer=[NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(timerFired:) userInfo:nil repeats:NO];
[[NSRunLoop currentRunLoop]addTimer:self.connectionTimer forMode:NSDefaultRunLoopMode]; //用timer作為延時的一種方法do{
[[NSRunLoopcurrentRunLoop]runUntilDate:[NSDatedateWithTimeIntervalSinceNow:1.0]];
}while(!done); //timer調(diào)用函數(shù)-(void)timerFired:(NSTimer *)timer{
done =YES;
}
轉(zhuǎn)自:http://magicalboy.com/objective_c_nstimer_usage/#comment-45
創(chuàng)建 NSTimer
Scheduled Timers & Using Selector
-
(
IBAction
)
startOneOffTimer:
sender
{
[
NSTimer
scheduledTimerWithTimeInterval:
2.0
target:
self
selector:
@selector
(
targetMethod:
)
userInfo:
[
self
userInfo
]
repeats:
NO
];
}
如上,如果沒有重復(fù)執(zhí)行的timer相當(dāng)于
[self performSelector:@selector(targetMethod:) withObject:nil afterDelay:2.0];
// declcare
@property
(
assign
)
NSTimer
*
repeatingTimer
;
// implements
-
(
IBAction
)
startRepeatingTimer:
sender
{
NSTimer
*
timer
=
[
NSTimer
scheduledTimerWithTimeInterval:
0.5
target:
self
selector:
@selector
(
timerFireMethod:
)
userInfo:
[
self
userInfo
]
repeats:
YES
];
self
.
repeatingTimer
=
timer
;
}
-
(
IBAction
)
stopRepeatingTimer:
sender
{
[
repeatingTimer
invalidate
];
self
.
repeatingTimer
=
nil
;
}
Unscheduled Timers & Using Invocation
// declare
@property (retain) NSTimer *unregisteredTimer;
// implements
- (IBAction)createUnregisteredTimer:sender {
NSMethodSignature *methodSignature = [self methodSignatureForSelector:@selector(invocationMethod:)];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];
[invocation setTarget:self];
[invocation setSelector:@selector(invocationMethod:)];
NSDate *startDate = [NSDate date];
[invocation setArgument:&startDate atIndex:2];
NSTimer *timer = [NSTimer timerWithTimeInterval:0.5 invocation:invocation repeats:YES];
self.unregisteredTimer = timer;
}
- (IBAction)startUnregisteredTimer:sender {
if (unregisteredTimer != nil) {
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
[runLoop addTimer:unregisteredTimer forMode:NSDefaultRunLoopMode];
}
}
- (IBAction)stopUnregisteredTimer:sender {
[unregisteredTimer invalidate];
self.unregisteredTimer = nil;
}
總結(jié)
以上是生活随笔為你收集整理的ios nstimer实现延时_iOS中定时器NSTimer的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: lftp linux,linux下使用
- 下一篇: expires为session_面试必问