runloop(PreformSelector方法运用)

本文介绍了一个iOS应用中的触控事件处理方法,并通过触控事件启动了一个子线程来执行一些任务。文章详细解释了如何使用Objective-C语言在ViewController类中实现一个名为run的方法,该方法在子线程中运行并打印当前线程信息。
 1 @interface ViewController ()
 2 @end
 3 
 4 @implementation ViewController
 5 
 6 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
 7 {
 8     
10     [NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];
11     
12     
13     //同样开启的是子线程
14 //   NSThread *r = [[NSThread alloc]initWithTarget:self selector:@selector(run) object:nil];
15 //    [r start];
16     
17 }
18 
19 
20 - (void)run
21 {
22     
23     NSLog(@"run----%@",[NSThread currentThread]);
24     
25     
26     //为什么没执行task :注意这个方法虽然内部会自动加入runloop中,但这条子线程中的runloop还没开启,子线程中的runloop是需要手动开启的
27  //该方法内部自动将performSelector 事件添加到当前的runloop中,指定的运行模式为默认
28     [self performSelector:@selector(task) withObject:nil afterDelay:1.0];
29     
30     //开启runloop
31     [[NSRunLoop currentRunLoop]run];
32 }
33 
34 
35 - (void)task
36 {
37     
38     NSLog(@"task----%@",[NSThread currentThread]); //子线程
39 }

 

转载于:https://www.cnblogs.com/dongjue/p/5804628.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值