zoukankan      html  css  js  c++  java
  • 线程睡眠

    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;

     

    }

     

    还是第二种方便。

  • 相关阅读:
    程序员的健康问题
    比特币解密
    浅谈比特币
    一款能帮助程序员发现问题的软件
    微软为什么总招人黑?
    写了一个bug,最后却变成了feature,要不要修呢?
    不管你信不信,反正我信了
    Excel工作表密码保护的破解
    pip笔记(译)
    super
  • 原文地址:https://www.cnblogs.com/guanliyang/p/3821760.html
Copyright © 2011-2022 走看看