延时函数和Timer的使用

来源:互联网 发布:python 文件的移动 编辑:程序博客网 时间:2024/06/06 03:27
延时函数:
[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://线程的使用  延迟函数



原创粉丝点击