-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
UICountingLabel.m
executable file
·292 lines (212 loc) · 6.95 KB
/
UICountingLabel.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#import <QuartzCore/QuartzCore.h>
#import "UICountingLabel.h"
#if !__has_feature(objc_arc)
#error UICountingLabel is ARC only. Either turn on ARC for the project or use -fobjc-arc flag
#endif
#pragma mark - UILabelCounter
#ifndef kUILabelCounterRate
#define kUILabelCounterRate 3.0
#endif
@protocol UILabelCounter<NSObject>
-(CGFloat)update:(CGFloat)t;
@end
@interface UILabelCounterLinear : NSObject<UILabelCounter>
@end
@interface UILabelCounterEaseIn : NSObject<UILabelCounter>
@end
@interface UILabelCounterEaseOut : NSObject<UILabelCounter>
@end
@interface UILabelCounterEaseInOut : NSObject<UILabelCounter>
@end
@interface UILabelCounterEaseInBounce : NSObject<UILabelCounter>
@end
@interface UILabelCounterEaseOutBounce : NSObject<UILabelCounter>
@end
@implementation UILabelCounterLinear
-(CGFloat)update:(CGFloat)t
{
return t;
}
@end
@implementation UILabelCounterEaseIn
-(CGFloat)update:(CGFloat)t
{
return powf(t, kUILabelCounterRate);
}
@end
@implementation UILabelCounterEaseOut
-(CGFloat)update:(CGFloat)t{
return 1.0-powf((1.0-t), kUILabelCounterRate);
}
@end
@implementation UILabelCounterEaseInOut
-(CGFloat) update: (CGFloat) t
{
t *= 2;
if (t < 1)
return 0.5f * powf (t, kUILabelCounterRate);
else
return 0.5f * (2.0f - powf(2.0 - t, kUILabelCounterRate));
}
@end
@implementation UILabelCounterEaseInBounce
-(CGFloat) update: (CGFloat) t {
if (t < 4.0 / 11.0) {
return 1.0 - (powf(11.0 / 4.0, 2) * powf(t, 2)) - t;
}
if (t < 8.0 / 11.0) {
return 1.0 - (3.0 / 4.0 + powf(11.0 / 4.0, 2) * powf(t - 6.0 / 11.0, 2)) - t;
}
if (t < 10.0 / 11.0) {
return 1.0 - (15.0 /16.0 + powf(11.0 / 4.0, 2) * powf(t - 9.0 / 11.0, 2)) - t;
}
return 1.0 - (63.0 / 64.0 + powf(11.0 / 4.0, 2) * powf(t - 21.0 / 22.0, 2)) - t;
}
@end
@implementation UILabelCounterEaseOutBounce
-(CGFloat) update: (CGFloat) t {
if (t < 4.0 / 11.0) {
return powf(11.0 / 4.0, 2) * powf(t, 2);
}
if (t < 8.0 / 11.0) {
return 3.0 / 4.0 + powf(11.0 / 4.0, 2) * powf(t - 6.0 / 11.0, 2);
}
if (t < 10.0 / 11.0) {
return 15.0 /16.0 + powf(11.0 / 4.0, 2) * powf(t - 9.0 / 11.0, 2);
}
return 63.0 / 64.0 + powf(11.0 / 4.0, 2) * powf(t - 21.0 / 22.0, 2);
}
@end
#pragma mark - UICountingLabel
@interface UICountingLabel ()
@property CGFloat startingValue;
@property CGFloat destinationValue;
@property NSTimeInterval progress;
@property NSTimeInterval lastUpdate;
@property NSTimeInterval totalTime;
@property CGFloat easingRate;
@property (nonatomic, strong) CADisplayLink *timer;
@property (nonatomic, strong) id<UILabelCounter> counter;
@end
@implementation UICountingLabel
-(void)countFrom:(CGFloat)value to:(CGFloat)endValue {
if (self.animationDuration == 0.0f) {
self.animationDuration = 2.0f;
}
[self countFrom:value to:endValue withDuration:self.animationDuration];
}
-(void)countFrom:(CGFloat)startValue to:(CGFloat)endValue withDuration:(NSTimeInterval)duration {
self.startingValue = startValue;
self.destinationValue = endValue;
// remove any (possible) old timers
[self.timer invalidate];
self.timer = nil;
if(self.format == nil) {
self.format = @"%f";
}
if (duration == 0.0) {
// No animation
[self setTextValue:endValue];
[self runCompletionBlock];
return;
}
self.easingRate = 3.0f;
self.progress = 0;
self.totalTime = duration;
self.lastUpdate = [NSDate timeIntervalSinceReferenceDate];
switch(self.method)
{
case UILabelCountingMethodLinear:
self.counter = [[UILabelCounterLinear alloc] init];
break;
case UILabelCountingMethodEaseIn:
self.counter = [[UILabelCounterEaseIn alloc] init];
break;
case UILabelCountingMethodEaseOut:
self.counter = [[UILabelCounterEaseOut alloc] init];
break;
case UILabelCountingMethodEaseInOut:
self.counter = [[UILabelCounterEaseInOut alloc] init];
break;
case UILabelCountingMethodEaseOutBounce:
self.counter = [[UILabelCounterEaseOutBounce alloc] init];
break;
case UILabelCountingMethodEaseInBounce:
self.counter = [[UILabelCounterEaseInBounce alloc] init];
break;
}
CADisplayLink *timer = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateValue:)];
timer.frameInterval = 2;
[timer addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
[timer addToRunLoop:[NSRunLoop mainRunLoop] forMode:UITrackingRunLoopMode];
self.timer = timer;
}
- (void)countFromCurrentValueTo:(CGFloat)endValue {
[self countFrom:[self currentValue] to:endValue];
}
- (void)countFromCurrentValueTo:(CGFloat)endValue withDuration:(NSTimeInterval)duration {
[self countFrom:[self currentValue] to:endValue withDuration:duration];
}
- (void)countFromZeroTo:(CGFloat)endValue {
[self countFrom:0.0f to:endValue];
}
- (void)countFromZeroTo:(CGFloat)endValue withDuration:(NSTimeInterval)duration {
[self countFrom:0.0f to:endValue withDuration:duration];
}
- (void)updateValue:(NSTimer *)timer {
// update progress
NSTimeInterval now = [NSDate timeIntervalSinceReferenceDate];
self.progress += now - self.lastUpdate;
self.lastUpdate = now;
if (self.progress >= self.totalTime) {
[self.timer invalidate];
self.timer = nil;
self.progress = self.totalTime;
}
[self setTextValue:[self currentValue]];
if (self.progress == self.totalTime) {
[self runCompletionBlock];
}
}
- (void)setTextValue:(CGFloat)value
{
if (self.attributedFormatBlock != nil) {
self.attributedText = self.attributedFormatBlock(value);
}
else if(self.formatBlock != nil)
{
self.text = self.formatBlock(value);
}
else
{
// check if counting with ints - cast to int
if([self.format rangeOfString:@"%(.*)d" options:NSRegularExpressionSearch].location != NSNotFound || [self.format rangeOfString:@"%(.*)i"].location != NSNotFound )
{
self.text = [NSString stringWithFormat:self.format,(int)value];
}
else
{
self.text = [NSString stringWithFormat:self.format,value];
}
}
}
- (void)setFormat:(NSString *)format {
_format = format;
// update label with new format
[self setTextValue:self.currentValue];
}
- (void)runCompletionBlock {
if (self.completionBlock) {
self.completionBlock();
self.completionBlock = nil;
}
}
- (CGFloat)currentValue {
if (self.progress >= self.totalTime) {
return self.destinationValue;
}
CGFloat percent = self.progress / self.totalTime;
CGFloat updateVal = [self.counter update:percent];
return self.startingValue + (updateVal * (self.destinationValue - self.startingValue));
}
@end