运行循环

#import "ViewController.h"

 

@interface ViewController ()

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

}

 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    // 异步函数 + 全局并发队列

    

    dispatch_async(dispatch_get_global_queue(0, 0), ^{

        //

        

        NSLog(@"touchesBegan%@",[NSThread currentThread]);

        

        // 创建一个定时器.定时器就是一个事件源.

        NSTimer *timer = [NSTimer timerWithTimeInterval:2 target:self selector:@selector(test) userInfo:nil repeats:YES];

        

        // 需要将事件源添加到一个运行循环中

        [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];

        

       // [[NSRunLoop currentRunLoop] run];// 开启一个死循环

        

        //没有这句话就不能暂停  不能暂停执行耗时操作

        CFRunLoopRun();

        

        NSLog(@"touchesEnd%@",[NSThread currentThread]);

        

    });

}

                      

                      

- (void)test

{

    //定义了一个变量

    static int number = 0;

    

    NSLog(@"-----------%d %@",number,[NSThread currentThread]);

    //这个变量是递增的

    number ++;

    //判断如果这个变量增加到5的时候  下面的方法会停止运行循环

    if (number == 5) {

        

        // CFRunLoopGetCurrent() 活的当前的运行循环

        // CFRunLoopStop 停止当前的运行循环.

        CFRunLoopStop(CFRunLoopGetCurrent()); // 这句代码执行之后,就关闭运行循环了.就把运行循环这个死循环移除了,可以执行后续的代码.

    }

    

}

@end

转载于:https://www.cnblogs.com/R-X-L/p/4780060.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值