线程睡眠

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值