利用Timer定时器
NSTimer *connectionTimer;//timer对象
//实例化timer
self.connectionTimer=[NSTimerscheduledTimerWithTimeInterval:1.5
target:selfselector:@selector(timerFired:) userInfo:nil repeats:NO];
[[NSRunLoop
currentRunLoop]addTimer:self.connectionTimer forMode:NSDefaultRunLoopMode];
//用timer作为延时的一种方法
do{
[[NSRunLoopcurrentRunLoop]runUntilDate:[NSDatedateWithTimeIntervalSinceNow:1.0]];
}while(!done);
//timer调用函数
-(void)timerFired:(NSTimer *)timer{
done
=YES;
}
//用timer作为延时的一种方法
[NSTimerscheduledTimerWithTimeInterval:0.5target:selfselector:@selector(handleTimer: )userInfo:nilrepeats:NO] ;
-(void)handleTimer:(NSTimer *)timer
{
~~~
}
[timerinvalidate];//这句代码用来终止timmer,
延时函数:
[NSThread sleepForTimeInterval:5.0];//暂停5s.
本文介绍了如何使用Objective-C中的NSTimer类来实现延时功能,包括使用timer对象进行延时调用和定时器的重复使用。通过实例演示了如何设置定时器的时间间隔、目标、选择器和信息参数,以及如何终止定时器。
1159

被折叠的 条评论
为什么被折叠?



