forked from overtake/telegram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ITProgressIndicator.m
executable file
·344 lines (266 loc) · 9.91 KB
/
ITProgressIndicator.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
//
// ITProgressIndicatorView.m
// ITProgressIndicator
//
// Created by Ilija Tovilo on 9/25/13.
// Copyright (c) 2013 Ilija Tovilo. All rights reserved.
//
#if !__has_feature(objc_arc)
#error ARC needs to be enabled!
#endif
#import "ITProgressIndicator.h"
#pragma mark - Consts
#define kITSpinAnimationKey @"spinAnimation"
#define kITProgressPropertyKey @"progress"
// ----------------------------------------------------------------------------------------
#pragma mark - NSBezierPath+IT_Geometry
// ----------------------------------------------------------------------------------------
@interface NSBezierPath (IT_Geometry)
- (NSBezierPath*)it_rotatedBezierPath:(float) angle;
- (NSBezierPath*)it_rotatedBezierPath:(float) angle aboutPoint:(NSPoint)point;
@end
@implementation NSBezierPath (IT_Geometry)
- (NSBezierPath *)it_rotatedBezierPath:(float)angle {
return [self it_rotatedBezierPath:angle aboutPoint:NSMakePoint(NSMidX(self.bounds), NSMidY(self.bounds))];
}
- (NSBezierPath*)it_rotatedBezierPath:(float)angle aboutPoint:(NSPoint)point {
if(angle == 0.0) return self;
else
{
NSBezierPath* copy = [self copy];
NSAffineTransform *xfm = [self it_rotationTransformWithAngle:angle aboutPoint:point];
[copy transformUsingAffineTransform:xfm];
return copy;
}
}
- (NSAffineTransform *)it_rotationTransformWithAngle:(const float)angle aboutPoint:(const NSPoint)aboutPoint {
NSAffineTransform *xfm = [NSAffineTransform transform];
[xfm translateXBy:aboutPoint.x yBy:aboutPoint.y];
[xfm rotateByRadians:angle];
[xfm translateXBy:-aboutPoint.x yBy:-aboutPoint.y];
return xfm;
}
@end
// ----------------------------------------------------------------------------------------
#pragma mark - ITProgressIndicator
// ----------------------------------------------------------------------------------------
#pragma mark - Private Interface
@interface ITProgressIndicator ()
@property (nonatomic, strong, readonly) CALayer *rootLayer;
@property (nonatomic, strong, readonly) CALayer *progressIndicatorLayer;
@end
#pragma mark - Implementation
@implementation ITProgressIndicator
@synthesize progressIndicatorLayer = _progressIndicatorLayer;
#pragma mark - Init
- (id)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self) {
[self initLayers];
}
return self;
}
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self initLayers];
}
return self;
}
- (void)initLayers {
// Setting initial values
self.color = [NSColor blackColor];
self.innerMargin = 3;
self.widthOfLine = 2;
self.lengthOfLine = 3;
self.numberOfLines = 8;
self.animationDuration = 0.6;
self.isIndeterminate = YES;
self.steppedAnimation = YES;
self.hideWhenStopped = YES;
self.animates = YES;
// Init layers
_rootLayer = [CALayer layer];
self.layer = _rootLayer;
[self setWantsLayer:YES];
self.progressIndicatorLayer.frame = _rootLayer.bounds;
[_rootLayer addSublayer:self.progressIndicatorLayer];
[self reloadIndicatorContent];
[self reloadAnimation];
}
- (void)awakeFromNib {
[self reloadAnimation];
}
- (void)reloadIndicatorContent {
self.progressIndicatorLayer.contents = [self progressImage];
}
- (void)reloadAnimation {
[self.progressIndicatorLayer removeAnimationForKey:kITSpinAnimationKey];
if (self.animates) {
[self.progressIndicatorLayer addAnimation:[self keyFrameAnimationForCurrentPreferences] forKey:kITSpinAnimationKey];
}
}
#pragma mark - Drawing
- (NSImage *)progressImage {
NSImage *progressImage = [[NSImage alloc] initWithSize:self.bounds.size];
[progressImage setFlipped:NO];
[progressImage lockFocus];
{
[NSGraphicsContext saveGraphicsState];
{
[self.color set];
NSRect r = self.bounds;
NSBezierPath *line = [NSBezierPath bezierPathWithRoundedRect:
NSMakeRect((NSWidth(r) / 2) - (self.widthOfLine / 2),
(NSHeight(r) / 2) - self.innerMargin - self.lengthOfLine,
self.widthOfLine, self.lengthOfLine)
xRadius:self.widthOfLine / 2
yRadius:self.widthOfLine / 2];
void (^lineDrawingBlock)(NSUInteger line) =
^(NSUInteger lineNumber) {
NSBezierPath *lineInstance = [line copy];
lineInstance = [lineInstance it_rotatedBezierPath:((2 * M_PI) / self.numberOfLines * lineNumber) + M_PI
aboutPoint:NSMakePoint(NSWidth(r) / 2, NSHeight(r) / 2)];
if (_isIndeterminate) [[self.color colorWithAlphaComponent:1.0 - (1.0 / self.numberOfLines * lineNumber)] set];
[lineInstance fill];
};
if (!self.isIndeterminate) {
for (NSUInteger i = self.numberOfLines;
i > round(self.numberOfLines - (self.numberOfLines * self.progress));
i--)
{
lineDrawingBlock(i);
}
} else {
for (NSUInteger i = 0; i < self.numberOfLines; i++) {
lineDrawingBlock(i);
}
}
}
[NSGraphicsContext restoreGraphicsState];
}
[progressImage unlockFocus];
return progressImage;
}
#pragma mark - Helpers
- (CAKeyframeAnimation *)keyFrameAnimationForCurrentPreferences {
NSMutableArray* keyFrameValues = [NSMutableArray array];
NSMutableArray* keyTimeValues;
if (self.steppedAnimation) {
{
[keyFrameValues addObject:[NSNumber numberWithFloat:0.0]];
for (NSUInteger i = 0; i < self.numberOfLines; i++) {
[keyFrameValues addObject:[NSNumber numberWithFloat:-M_PI * (2.0 / self.numberOfLines * i)]];
[keyFrameValues addObject:[NSNumber numberWithFloat:-M_PI * (2.0 / self.numberOfLines * i)]];
}
[keyFrameValues addObject:[NSNumber numberWithFloat:-M_PI*2.0]];
}
keyTimeValues = [NSMutableArray array];
{
[keyTimeValues addObject:[NSNumber numberWithFloat:0.0]];
for (NSUInteger i = 0; i < (self.numberOfLines - 1); i++) {
[keyTimeValues addObject:[NSNumber numberWithFloat:1.0 / self.numberOfLines * i]];
[keyTimeValues addObject:[NSNumber numberWithFloat:1.0 / self.numberOfLines * (i + 1)]];
}
[keyTimeValues addObject:[NSNumber numberWithFloat:1.0 / self.numberOfLines * (self.numberOfLines - 1)]];
}
} else {
{
[keyFrameValues addObject:[NSNumber numberWithFloat:-M_PI*0.0]];
[keyFrameValues addObject:[NSNumber numberWithFloat:-M_PI*0.5]];
[keyFrameValues addObject:[NSNumber numberWithFloat:-M_PI*1.0]];
[keyFrameValues addObject:[NSNumber numberWithFloat:-M_PI*1.5]];
[keyFrameValues addObject:[NSNumber numberWithFloat:-M_PI*2.0]];
}
}
CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
[animation setRepeatCount:HUGE_VALF];
[animation setValues:keyFrameValues];
[animation setKeyTimes:keyTimeValues];
[animation setValueFunction:[CAValueFunction functionWithName: kCAValueFunctionRotateZ]];
[animation setDuration:self.animationDuration];
return animation;
}
- (void)reloadVisibility {
if (_hideWhenStopped && !_animates && _isIndeterminate) {
[self setHidden:YES];
} else {
[self setHidden:NO];
}
}
#pragma mark - NSView methods
// Animatible proxy
+ (id)defaultAnimationForKey:(NSString *)key
{
if ([key isEqualToString:kITProgressPropertyKey]) {
return [CABasicAnimation animation];
} else {
return [super defaultAnimationForKey:key];
}
}
#pragma mark - Setters & Getters
- (void)setIndeterminate:(BOOL)isIndeterminate {
_isIndeterminate = isIndeterminate;
if (!_isIndeterminate) {
self.animates = NO;
}
}
- (void)setProgress:(CGFloat)progress {
if (progress < 0 || progress > 1) {
@throw [NSException exceptionWithName:@"Invalid `progress` property value"
reason:@"`progress` property needs to be between 0 and 1"
userInfo:nil];
}
_progress = progress;
if (!self.isIndeterminate) {
[self reloadIndicatorContent];
}
}
- (void)setAnimates:(BOOL)animates {
_animates = animates;
[self reloadIndicatorContent];
[self reloadAnimation];
[self reloadVisibility];
}
- (void)setHideWhenStopped:(BOOL)hideWhenStopped {
_hideWhenStopped = hideWhenStopped;
[self reloadVisibility];
}
- (CALayer *)progressIndicatorLayer {
if (!_progressIndicatorLayer) {
_progressIndicatorLayer = [CALayer layer];
}
return _progressIndicatorLayer;
}
- (void)setLengthOfLine:(CGFloat)lengthOfLine {
_lengthOfLine = lengthOfLine;
[self reloadIndicatorContent];
}
- (void)setWidthOfLine:(CGFloat)widthOfLine {
_widthOfLine = widthOfLine;
[self reloadIndicatorContent];
}
- (void)setInnerMargin:(CGFloat)innerMargin {
_innerMargin = innerMargin;
[self reloadIndicatorContent];
}
- (void)setAnimationDuration:(CGFloat)animationDuration {
_animationDuration = animationDuration;
[self reloadAnimation];
}
- (void)setNumberOfLines:(NSUInteger)numberOfLines {
_numberOfLines = numberOfLines;
[self reloadIndicatorContent];
[self reloadAnimation];
}
- (void)setSteppedAnimation:(BOOL)steppedAnimation {
_steppedAnimation = steppedAnimation;
[self reloadAnimation];
}
- (void)setColor:(NSColor *)color {
_color = color;
[self reloadIndicatorContent];
}
@end