LWJ Runloop

可用runloop控制图片加载 卡顿 等问题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#import "ViewController.h"
typedef BOOL(^RunloopBlock)(void);
@interface ViewController ()
/**
存放任务的数组
*/
@property (nonatomic, retain)NSMutableArray *tasks;
/**
最大任务数
*/
@property (nonatomic, assign)NSUInteger max;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_max = 18;
_tasks = [NSMutableArray array];
[self addRunloopObserver];
NSTimer *timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(timerMothod) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
// dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, <#dispatchQueue#>);
// dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, <#intervalInSeconds#> * NSEC_PER_SEC, <#leewayInSeconds#> * NSEC_PER_SEC);
// dispatch_source_set_event_handler(timer, ^{
// <#code to be executed when timer fires#>
// });
// dispatch_resume(timer);
}
- (void)timerMothod
{
NSLog(@"timer-----");
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
}
- (void)addRunloopObserver
{
// typedef struct {
// CFIndex version;
// void * info;
// const void *(*retain)(const void *info);
// void (*release)(const void *info);
// CFStringRef (*copyDescription)(const void *info);
// } CFRunLoopObserverContext;
// 上下文结构体
CFRunLoopObserverContext context = {
0,
(__bridge void *)(self),
&CFRetain,
&CFRelease,
NULL
};
// 1.拿到当前的runloop
CFRunLoopRef runloop = CFRunLoopGetCurrent();
static CFRunLoopObserverRef defaultModeObserver;
// 创建观察者
defaultModeObserver = CFRunLoopObserverCreate(NULL, kCFRunLoopAfterWaiting, YES, 0, &callBack, &context);
// 添加当前观察者
CFRunLoopAddObserver(runloop, defaultModeObserver, kCFRunLoopDefaultMode);
// c语言与create就要有release
CFRelease(defaultModeObserver);
}
static void callBack(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void *info) {
ViewController *vc = (__bridge ViewController *)(info);
if (vc.tasks.count==0) return;
while (vc.tasks.count) {
RunloopBlock unit = vc.tasks.firstObject;
[vc.tasks removeObjectAtIndex:0];
}
}
@end