线程睡眠

1,睡眠方法。[NSThread sleepForTimeInterval:3];

关于程序睡眠这里收集了两种方法,看下面代码

- (IBAction)setButton:(id)sender {

    _label.text = @"对方正在思考";

 

    [self performSelector:@selector(run1) withObject:nil afterDelay:2];

 

    [NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];

    

}

 

-(void) run{

    [NSThread sleepForTimeInterval:3];

    _label.text = @"对方完成动作";

}

 

-(void) run1{

    _label.text = @"111";

}

 

第一种 [self performSelector:@selector(run1)withObject:nil afterDelay:3];是消息处理方法, 分别传入方法,传入方法参数和返回时间。

第二种 则创建了一个新线程。

 

所以能搞点有趣的代码了

- (IBAction)setButton:(id)sender {

    _label.text = @"对方正在思考";

    

    [self performSelector:@selector(run1:) withObject:@"第一次" afterDelay:3];

    [self performSelector:@selector(run1:) withObject:@"两次就熟悉了吧" afterDelay:5];

    [self performSelector:@selector(run1:) withObject:@"看什么看" afterDelay:7];

    [self performSelector:@selector(run1:) withObject:@"再看打爆你的眼睛" afterDelay:10];

}

 

-(void) run1:(NSString *) str{

 

    _label.text = str;

 

}

 

还是第二种方便。

原文地址:https://www.cnblogs.com/guanliyang/p/3821760.html