runloop的基本使用

#import "ViewController.h"


@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@property (nonatomic, strong) NSThread *thread;

@end


@implementation ViewController



- (IBAction)oneBtnClick:(id)sender {

    

  self.thread =   [[NSThread alloc]initWithTarget:self selector:@selector(operation1) object:nil];

    [self.thread start];

}

- (IBAction)twoBtnClick:(id)sender {

    

    [self performSelector:@selector(operation2) onThread:self.thread withObject:nil waitUntilDone:YES];

}


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

{

    //Runloop的自动释放池子

    /*

     第一次创建:启动的时候

     最后一次销毁:退出

     其他:

        runloop即将进入休眠状态的时候会销毁,重新创建一个空的自动释放池

     */

    NSLog(@"---start0----");

    /*

    //添加在runloop的默认运行模式下面的

    [self.imageView performSelector:@selector(setImage:) withObject:[UIImage imageNamed:@"Snip20151127_133"] afterDelay:2.0 inModes:@[NSDefaultRunLoopMode,UITrackingRunLoopMode]];

     */

}


-(void)operation1

{

//    while (1) {

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

//    }

    

    //开启runloop

    //创建子线程对应的runloop

    NSRunLoop *runloop = [NSRunLoop currentRunLoop];

    

//    [runloop addPort:[NSMachPort port] forMode:NSDefaultRunLoopMode];

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

    [runloop addTimer:timer forMode:NSDefaultRunLoopMode];

    //默认是没有开启的,需要手动开启

    [runloop run];

    

    //runloop要运行起来,mode里面至少要有一个source或者是timer

    

    NSLog(@"---end----");

}


-(void)operation2

{

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

}


-(void)test

{

    NSLog(@"---test---");

}

@end


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值