-
Notifications
You must be signed in to change notification settings - Fork 0
/
NTLoop.m
165 lines (132 loc) · 4.57 KB
/
NTLoop.m
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//
// NTLoop.m
// NTLoop
//
// Created by Nikita Titov <[email protected]> on 11/24/11.
// Copyright (c) 2011 Nikita Titov. All rights reserved.
//
#import "NTLoop.h"
#import <QuartzCore/QuartzCore.h>
@interface NTLoop()
@property (readwrite, nonatomic) NSInteger framesDisplayed;
@property (readwrite, copy, nonatomic) UpdateBlock updateBlock;
@property (strong, nonatomic) CADisplayLink *displayLink;
- (void)update;
@property (nonatomic) NSTimeInterval maxElapsedTime;
@property (nonatomic) NSTimeInterval lastTimestamp;
@property (nonatomic) NSTimeInterval currentTimestamp;
@property(nonatomic, readwrite) NSTimeInterval timeSinceFirstResume;
@property(nonatomic, readwrite) NSTimeInterval timeSinceLastResume;
@property(nonatomic, readwrite) NSTimeInterval timeSinceLastUpdate;
- (void)handleWillResignActive;
- (void)handleDidBecomeActive;
@end
@implementation NTLoop
@synthesize
delegate,
framesDisplayed,
paused = paused_,
updateBlock,
framesPerSecond,
pauseOnWillResignActive,
resumeOnDidBecomeActive;
@synthesize
displayLink,
maxElapsedTime,
lastTimestamp,
currentTimestamp,
timeSinceFirstResume,
timeSinceLastResume,
timeSinceLastUpdate;
- (void)setPaused:(BOOL)paused {
if (paused == NO) {
self.timeSinceLastResume = 0;
}
if ([self.delegate respondsToSelector:@selector(ntLoop:willPause:)]) {
[self.delegate ntLoop:self willPause:paused];
}
paused_ = paused;
}
+ (id)loopWithFrameInterval:(NSUInteger)frameInterval usingBlock:(UpdateBlock)block {
return [[self alloc] initWithFrameInterval:frameInterval usingBlock:block];
}
- (id)init {
self = [self initWithFrameInterval:1 usingBlock:nil];
return self;
}
- (id)initWithFrameInterval:(NSUInteger)frameInterval {
self = [self initWithFrameInterval:frameInterval usingBlock:nil];
return self;
}
- (id)initWithFrameInterval:(NSUInteger)frameInterval usingBlock:(UpdateBlock)block {
self = [super init];
if (self) {
self.updateBlock = block;
self.lastTimestamp = 0;
self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(update)];
self.displayLink.frameInterval = frameInterval;
NSTimeInterval fps = self.framesPerSecond;
self.maxElapsedTime = 1 / fps;
self.framesDisplayed = 0;
self.timeSinceFirstResume = 0;
self.timeSinceLastResume = 0;
self.timeSinceLastUpdate = 0;
self.paused = NO;
[self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
self.pauseOnWillResignActive = YES;
self.resumeOnDidBecomeActive =YES;
[[NSNotificationCenter defaultCenter] addObserver:self
selector: @selector(handleWillResignActive)
name: UIApplicationWillResignActiveNotification
object: nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector: @selector(handleDidBecomeActive)
name: UIApplicationDidBecomeActiveNotification
object: nil];
}
return self;
}
- (void)dealloc {
self.updateBlock = nil;
[self.displayLink invalidate];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (NSInteger)framesPerSecond {
NSInteger frameInterval = self.displayLink.frameInterval;
NSInteger fpsValue = (NSInteger)floor(60 / frameInterval);
return fpsValue;
}
- (NSTimeInterval)timeSinceLastUpdate {
self.currentTimestamp = CACurrentMediaTime();
NSTimeInterval dt = self.currentTimestamp - self.lastTimestamp;
self.lastTimestamp = self.currentTimestamp;
if (dt > self.maxElapsedTime) {
dt = self.maxElapsedTime;
}
return dt;
}
- (void)update {
if (!self.isPaused) {
self.framesDisplayed++;
NSTimeInterval dt = [self timeSinceLastUpdate];
self.timeSinceFirstResume += dt;
self.timeSinceLastResume += dt;
if ([self.delegate respondsToSelector:@selector(ntLoopUpdate:)]) {
[self.delegate ntLoopUpdate:self];
}
else {
self.updateBlock(dt);
}
}
}
- (void)handleWillResignActive {
if (self.pauseOnWillResignActive) {
self.paused = YES;
}
}
- (void)handleDidBecomeActive {
if (self.resumeOnDidBecomeActive) {
self.paused = NO;
}
}
@end