延时函数:
[NSThread sleepForTimeInterval:5.0]; //暂停5s.
Timer的使用:
NSTimer *connectionTimer; //timer对象
//实例化timer
self.connectionTimer=[NSTimer scheduledTimerWithTimeInterval: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;
[NSThread sleepForTimeInterval:5.0]; //暂停5s.
Timer的使用:
NSTimer *connectionTimer; //timer对象
//实例化timer
self.connectionTimer=[NSTimer scheduledTimerWithTimeInterval: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;
}
[self performSelector:@selector(addShrinkImg) withObject:self afterDelay:2.0f];//performSelector: withObject:afterDelay://线程的使用
延迟函数
本文详细介绍了Objective-C中NSThread的sleepForTimeInterval方法用于实现延时功能,以及如何利用NSTimer创建定时任务。通过实例演示了如何在特定时间间隔后执行指定的操作,展示了Objective-C在时间控制方面的强大能力。
569

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



