This repository was archived by the owner on Jan 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathEKActivityIndicatorView.j
More file actions
223 lines (181 loc) · 5.44 KB
/
EKActivityIndicatorView.j
File metadata and controls
223 lines (181 loc) · 5.44 KB
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
@implementation EKActivityIndicatorView : CPView
{
BOOL _isAnimating;
BOOL _shouldUseCSS;
CPString _CSSProperty;
int _step;
CPTimer _timer;
CPColor _color;
float _colorRed;
float _colorGreen;
float _colorBlue;
}
- (id)initWithFrame:(CGRect)aFrame
{
self = [super initWithFrame:aFrame];
if (self)
{
_isAnimating = NO;
_shouldUseCSS = NO;
[self setColor:[CPColor blackColor]];
[self setUseCSS:YES];
}
return self;
}
- (BOOL)checkCSS
{
var properties = ["transform", "webkitTransform", "oTransform", "MozTransform", "msTransform"];
for (var i = 0, property; property = properties[i++];)
{
if (typeof self._DOMElement.style[property] != "undefined")
{
_CSSProperty = property;
break;
}
}
}
- (void)setUseCSS:(BOOL)shouldUseCSS
{
// --- Check if we can use CSS3 for rotation
if (!_CSSProperty)
[self checkCSS];
_shouldUseCSS = shouldUseCSS;
if (!_isAnimating)
return;
[self stopAnimating];
[self startAnimating];
}
- (void)setColor:(CPColor)aColor
{
_color = aColor;
_colorRed = [aColor redComponent];
_colorGreen = [aColor greenComponent];
_colorBlue = [aColor blueComponent];
if (_shouldUseCSS)
[self setNeedsDisplay:YES];
}
- (void)setObjectValue:(BOOL)aValue
{
switch (aValue)
{
case YES:
[self startAnimating];
break;
case NO:
[self stopAnimating];
break;
}
}
- (void)startAnimating
{
if (_isAnimating)
return;
_isAnimating = YES;
_step = 1;
[self setNeedsDisplay:YES];
_timer = [CPTimer scheduledTimerWithTimeInterval:0.1
target:self
selector:@selector(timerDidFire)
userInfo:nil
repeats:YES];
}
- (void)stopAnimating
{
if (!_isAnimating)
return;
_isAnimating = NO;
[_timer invalidate];
[self setNeedsDisplay:YES];
if (_shouldUseCSS && _CSSProperty)
self._DOMElement.style[_CSSProperty] = "rotate(0deg)";
}
- (BOOL)isAnimating
{
return _isAnimating;
}
- (CPColor)color
{
return _color;
}
- (void)timerDidFire
{
if (_step == 12)
_step = 1;
else
_step++;
// --- Redraw canvas if browser shouldn't / can't rotate
if (!_CSSProperty || !_shouldUseCSS)
return [self setNeedsDisplay:YES];
// --- Animate with rotation if the browser is smart enough
var rad = _step / 12 * 2 * Math.PI,
radString = "rotate(" + rad + "rad)";
self._DOMElement.style[_CSSProperty] = radString;
}
- (void)drawRect:(CGrect)rect
{
var size = MIN(rect.size.height, rect.size.width),
c = [[CPGraphicsContext currentContext] graphicsPort];
CGContextClearRect(c, rect);
if (!_isAnimating)
return;
var thickness = size * 0.1,
length = size * 0.28,
radius = thickness / 2,
lineRect = CGRectMake(size / 2 - thickness / 2, 0, thickness, length),
minx = CGRectGetMinX(lineRect),
midx = CGRectGetMidX(lineRect),
maxx = CGRectGetMaxX(lineRect),
miny = CGRectGetMinY(lineRect),
midy = CGRectGetMidY(lineRect),
maxy = CGRectGetMaxY(lineRect),
delta = [];
CGContextSetFillColor(c, [CPColor blackColor]);
function fillWithOpacity(opacity)
{
CGContextSetFillColor(c, [CPColor colorWithRed:_colorRed green:_colorGreen blue:_colorBlue alpha:opacity]);
}
for (var i = 1; i <= 12; i++)
{
for (var j = 1; j <= 6; j++)
{
delta[j] = (_step <= j) ? 12-j : -j;
}
if (i == _step) CGContextSetFillColor(c, _color);
else if (i == _step + delta[1]) fillWithOpacity(0.9);
else if (i == _step + delta[2]) fillWithOpacity(0.8);
else if (i == _step + delta[3]) fillWithOpacity(0.7);
else if (i == _step + delta[4]) fillWithOpacity(0.6);
else if (i == _step + delta[5]) fillWithOpacity(0.5);
else if (i == _step + delta[6]) fillWithOpacity(0.4);
else fillWithOpacity(0.3);
CGContextBeginPath(c);
CGContextMoveToPoint(c, minx, midy);
CGContextAddArcToPoint(c, minx, miny, midx, miny, radius);
CGContextAddArcToPoint(c, maxx, miny, maxx, midy, radius);
CGContextAddArcToPoint(c, maxx, maxy, midx, maxy, radius);
CGContextAddArcToPoint(c, minx, maxy, minx, midy, radius);
CGContextClosePath(c);
CGContextFillPath(c);
CGContextTranslateCTM(c, size / 2, size / 2);
CGContextRotateCTM(c, 30 * (Math.PI / 180));
CGContextTranslateCTM(c, -size / 2, -size / 2);
}
}
@end
var EKActivityIndicatorViewColor = @"EKActivityIndicatorViewColor";
@implementation EKActivityIndicatorView (CPCoding)
- (id)initWithCoder:(CPCoder)aCoder
{
self = [super initWithCoder:aCoder];
if (self)
{
[self setColor:[aCoder decodeObjectForKey:EKActivityIndicatorViewColor]];
}
return self;
}
- (void)encodeWithCoder:(CPCoder)aCoder
{
[super encodeWithCoder:aCoder];
[aCoder encodeObject:[self color] forKey:EKActivityIndicatorViewColor];
}
@end