延时函数和Timer的使用

延时函数和Timer的使用


延时函数:
[NSThread sleepForTimeInterval:5.0]; //暂停5s.


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;
}

posted @ 2015-12-23 21:04  Bo-tree  阅读(401)  评论(0)    收藏  举报