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
3、监听模式切换
- (void)test11 {
// 创建observer
CFRunLoopObserverRef observer = CFRunLoopObserverCreateWithHandler(kCFAllocatorDefault, kCFRunLoopAllActivities, YES, 0, ^(CFRunLoopObserverRef observer, CFRunLoopActivity activity) {
switch (activity) {
case kCFRunLoopEntry:{
CFRunLoopMode mode = CFRunLoopCopyCurrentMode(CFRunLoopGetCurrent());
NSLog(@"block--kCFRunLoopEntry--%@",mode);
CFRelease(mode);
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:{
CFRunLoopMode mode = CFRunLoopCopyCurrentMode(CFRunLoopGetCurrent());
NSLog(@"block--kCFRunLoopExit--%@",mode);
CFRelease(mode);
break;
}
default:
break;
}
});
// 添加observer到Runloop中
CFRunLoopAddObserver(CFRunLoopGetMain(), observer, kCFRunLoopCommonModes);
// 释放
CFRelease(observer);
}
2022-01-20 10:45:44.769757+0800 super-interview[35614:441896] block–kCFRunLoopExit–kCFRunLoopDefaultMode
2022-01-20 10:45:44.769919+0800 super-interview[35614:441896] block–kCFRunLoopEntry–UITrackingRunLoopMode
2022-01-20 10:45:45.005460+0800 super-interview[35614:441896] block–kCFRunLoopExit–UITrackingRunLoopMode
2022-01-20 10:45:45.005555+0800 super-interview[35614:441896] block–kCFRunLoopEntry–kCFRunLoopDefaultMode
2022-01-20 10:45:45.995863+0800 super-interview[35614:441896] block–kCFRunLoopExit–kCFRunLoopDefaultMode
2022-01-20 10:45:45.995991+0800 super-interview[35614:441896] block–kCFRunLoopEntry–UITrackingRunLoopMode
2022-01-20 10:45:46.175716+0800 super-interview[35614:441896] block–kCFRunLoopExit–UITrackingRunLoopMode
2022-01-20 10:45:46.175822+0800 super-interview[35614:441896] block–kCFRunLoopEntry–kCFRunLoopDefaultMode
2022-01-20 10:45:46.907191+0800 super-interview[35614:441896] block–kCFRunLoopExit–kCFRunLoopDefaultMode
2022-01-20 10:45:46.907321+0800 super-interview[35614:441896] block–kCFRunLoopEntry–UITrackingRunLoopMode
2022-01-20 10:45:47.751727+0800 super-interview[35614:441896] block–kCFRunLoopExit–UITrackingRunLoopMode
2022-01-20 10:45:47.751888+0800 super-interview[35614:441896] block–kCFRunLoopEntry–kCFRunLoopDefaultMode
2022-01-20 10:45:47.752669+0800 super-interview[35614:441896] block–kCFRunLoopExit–kCFRunLoopDefaultMode
2022-01-20 10:45:47.752753+0800 super-interview[35614:441896] block–kCFRunLoopEntry–UITrackingRunLoopMode
2022-01-20 10:45:48.626492+0800 super-interview[35614:441896] block–kCFRunLoopExit–UITrackingRunLoopMode
2022-01-20 10:45:48.626702+0800 super-interview[35614:441896] block–kCFRunLoopEntry–kCFRunLoopDefaultMode
4、Runloop的休眠原理
原文地址:https://blog.csdn.net/JH_Cao/article/details/122595645
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_24166.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!