其他分享
首页 > 其他分享> > iOS底层系列之<27>--Runloop<三>Runloop监听observer的方式

iOS底层系列之<27>--Runloop<三>Runloop监听observer的方式

作者:互联网

1、监听的方式一(C语言函数)

- (void)test10 {
    // 创建observer
    CFRunLoopObserverRef observer = CFRunLoopObserverCreate(kCFAllocatorDefault, kCFRunLoopBeforeWaiting | kCFRunLoopAfterWaiting, YES, 0, observeRunLoopActivities, NULL);
    // 添加observer到Runloop中
    CFRunLoopAddObserver(CFRunLoopGetMain(), observer, kCFRunLoopCommonModes);
    // 释放
    CFRelease(observer);
}

void observeRunLoopActivities(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void *info) {
    switch (activity) {
        case kCFRunLoopEntry:
            NSLog(@"kCFRunLoopEntry");
            break;
        case kCFRunLoopBeforeTimers:
            NSLog(@"kCFRunLoopBeforeTimers");
            break;
        case kCFRunLoopBeforeSources:
            NSLog(@"kCFRunLoopBeforeSources");
            break;
        case kCFRunLoopBeforeWaiting:
            NSLog(@"kCFRunLoopBeforeSources");
            break;
        case kCFRunLoopAfterWaiting:
            NSLog(@"kCFRunLoopAfterWaiting");
            break;
        case kCFRunLoopExit:
            NSLog(@"kCFRunLoopExit");
            break;
        default:
            NSLog(@"default");
            break;
    }
}

触摸点击,调用

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self test10];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"%s",__func__);
}

打印结果:
2022-01-20 10:18:02.295228+0800 super-interview[34440:418395] kCFRunLoopAfterWaiting
2022-01-20 10:18:02.296370+0800 super-interview[34440:418395] -[ViewController touchesBegan:withEvent:]
2022-01-20 10:18:02.296564+0800 super-interview[34440:418395] kCFRunLoopBeforeSources
2022-01-20 10:18:02.296982+0800 super-interview[34440:418395] kCFRunLoopAfterWaiting
2022-01-20 10:18:02.297405+0800 super-interview[34440:418395] kCFRunLoopBeforeSources
2022-01-20 10:18:02.408768+0800 super-interview[34440:418395] kCFRunLoopAfterWaiting
2022-01-20 10:18:02.409163+0800 super-interview[34440:418395] kCFRunLoopBeforeSources

2、监听方式二(Block方式)

- (void)test11 {
    // 创建observer
    CFRunLoopObserverRef observer = CFRunLoopObserverCreateWithHandler(kCFAllocatorDefault, kCFRunLoopBeforeWaiting | kCFRunLoopAfterWaiting, YES, 0, ^(CFRunLoopObserverRef observer, CFRunLoopActivity activity) {
        switch (activity) {
            case kCFRunLoopEntry:
                NSLog(@"block--kCFRunLoopEntry");
                break;
            case kCFRunLoopBeforeTimers:
                NSLog(@"block--kCFRunLoopBeforeTimers");
                break;
            case kCFRunLoopBeforeSources:
                NSLog(@"block--kCFRunLoopBeforeSources");
                break;
            case kCFRunLoopBeforeWaiting:
                NSLog(@"block--kCFRunLoopBeforeSources");
                break;
            case kCFRunLoopAfterWaiting:
                NSLog(@"block--CFRunLoopAfterWaiting");
                break;
            case kCFRunLoopExit:
                NSLog(@"block--kCFRunLoopExit");
                break;
            default:
                NSLog(@"block--default");
                break;
        }
    });
    // 添加observer到Runloop中
    CFRunLoopAddObserver(CFRunLoopGetMain(), observer, kCFRunLoopCommonModes);
    // 释放
    CFRelease(observer);
}

调用

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self test11];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"%s",__func__);
}

打印结果:
2022-01-20 10:19:11.930278+0800 super-interview[34504:419754] block–CFRunLoopAfterWaiting
2022-01-20 10:19:11.931429+0800 super-interview[34504:419754] -[ViewController touchesBegan:withEvent:]
2022-01-20 10:19:11.931620+0800 super-interview[34504:419754] block–kCFRunLoopBeforeSources
2022-01-20 10:19:11.932087+0800 super-interview[34504:419754] block–CFRunLoopAfterWaiting
2022-01-20 10:19:11.932512+0800 super-interview[34504:419754] block–kCFRunLoopBeforeSources
2022-01-20 10:19:12.045637+0800 super-interview[34504:419754] block–CFRunLoopAfterWaiting
2022-01-20 10:19:12.046146+0800 super-interview[34504:419754] block–kCFRunLoopBeforeSources

标签:10,27,20,observer,NSLog,break,01,super,Runloop
来源: https://blog.csdn.net/JH_Cao/article/details/122595645