Skip to content
Browse files

You spin my head

  • Loading branch information...
0 parents commit cd10289fb28597e05e9cfc922b7b248a50b664e7 Elias Klughammer committed May 17, 2010
Showing with 192 additions and 0 deletions.
  1. +133 −0 EKActivityIndicatorView.j
  2. +25 −0 LICENSE
  3. +34 −0 README.markdown
133 EKActivityIndicatorView.j
@@ -0,0 +1,133 @@
+@implementation EKActivityIndicatorView : CPView
+{
+ BOOL _isAnimating;
+ int _step;
+ CPTimer _timer;
+ CPColor _color;
+ float _colorRed;
+ float _colorGreen;
+ float _colorBlue;
+}
+
+- (id)initWithFrame:(CGRect)aFrame
+{
+ self = [super initWithFrame:aFrame];
+ if(self) {
+ _isAnimating = NO;
+ [self setColor:[CPColor blackColor]];
+ }
+ return self;
+}
+
+- (void)setColor:(CPColor)aColor
+{
+ _color = aColor;
+ _colorRed = [aColor redComponent];
+ _colorGreen = [aColor greenComponent];
+ _colorBlue = [aColor blueComponent];
+}
+
+- (void)startAnimating
+{
+ if (!_isAnimating) {
+ _isAnimating = YES;
+ _step = 1;
+ _timer = [CPTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timerDidFire) userInfo:nil repeats:YES];
+ }
+}
+
+- (void)stopAnimating
+{
+ if (_isAnimating) {
+ _isAnimating = NO;
+ [_timer invalidate];
+ [self setNeedsDisplay:YES];
+ }
+}
+
+- (BOOL)isAnimating
+{
+ return _isAnimating;
+}
+
+- (CPColor)color
+{
+ return _color;
+}
+
+- (void)timerDidFire
+{
+ if (_step == 12)
+ _step = 1;
+ else
+ _step++;
+
+ [self setNeedsDisplay:YES];
+}
+
+- (void)drawRect:(CGrect)rect
+{
+ var bounds = [self bounds];
+ var size = bounds.size.width;
+ var c = [[CPGraphicsContext currentContext] graphicsPort];
+
+ CGContextClearRect(c, rect);
+
+ if (_isAnimating) {
+ var thickness = bounds.size.width * 0.1;
+ var length = bounds.size.width * 0.28;
+ var radius = thickness / 2;
+ var lineRect = CGRectMake(size / 2 - thickness / 2, 0, thickness, length);
+ var minx = CGRectGetMinX(lineRect);
+ var midx = CGRectGetMidX(lineRect);
+ var maxx = CGRectGetMaxX(lineRect);
+ var miny = CGRectGetMinY(lineRect);
+ var midy = CGRectGetMidY(lineRect);
+ var maxy = CGRectGetMaxY(lineRect);
+ var delta1, delta2, delta3, delta4, delta5, delta6;
+
+ CGContextSetFillColor(c, [CPColor blackColor]);
+
+ for (i=1; i<=12; i++) {
+
+ delta1 = (_step <= 1) ? 11 : -1;
+ delta2 = (_step <= 2) ? 10 : -2;
+ delta3 = (_step <= 3) ? 9 : -3;
+ delta4 = (_step <= 4) ? 8 : -4;
+ delta5 = (_step <= 5) ? 7 : -5;
+ delta6 = (_step <= 6) ? 6 : -6;
+
+ if (i==_step)
+ CGContextSetFillColor(c, _color);
+ else if (i==_step+delta1)
+ CGContextSetFillColor(c, [CPColor colorWithRed:_colorRed green:_colorGreen blue:_colorBlue alpha:0.9]);
+ else if (i==_step+delta2)
+ CGContextSetFillColor(c, [CPColor colorWithRed:_colorRed green:_colorGreen blue:_colorBlue alpha:0.8]);
+ else if (i==_step+delta3)
+ CGContextSetFillColor(c, [CPColor colorWithRed:_colorRed green:_colorGreen blue:_colorBlue alpha:0.7]);
+ else if (i==_step+delta4)
+ CGContextSetFillColor(c, [CPColor colorWithRed:_colorRed green:_colorGreen blue:_colorBlue alpha:0.6]);
+ else if (i==_step+delta5)
+ CGContextSetFillColor(c, [CPColor colorWithRed:_colorRed green:_colorGreen blue:_colorBlue alpha:0.5]);
+ else if (i==_step+delta6)
+ CGContextSetFillColor(c, [CPColor colorWithRed:_colorRed green:_colorGreen blue:_colorBlue alpha:0.4]);
+ else
+ CGContextSetFillColor(c, [CPColor colorWithRed:_colorRed green:_colorGreen blue:_colorBlue alpha: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);
+ CGContextFillPath(c);
+ CGContextClosePath(c);
+ CGContextTranslateCTM(c, size/2, size/2);
+ CGContextRotateCTM(c, 30*(Math.PI/180));
+ CGContextTranslateCTM(c, -size/2, -size/2);
+ }
+ }
+}
+
+@end
+
25 LICENSE
@@ -0,0 +1,25 @@
+EKActivityIndicatorView.j
+
+Created by Elias Klughammer on May 16, 2010 in Ulaan Bataar, Mongolia.
+
+The MIT License
+
+Copyright (c) 2010 Elias Klughammer
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
34 README.markdown
@@ -0,0 +1,34 @@
+EKActivityIndicatorView
+==========
+
+EKActivityIndicatorView is a class for displaying those "here spins something the app isn't broken"-wheels for the [Cappuccino](http://www.cappuccino.org) framework.
+
+It works completely without images by drawing the animation with CoreGraphics. This guarantees a stunning performance and the ability of setting the view's size and color with ease. All this is brought to you by a 4 KB file.
+
+
+## Installation
+
+Simply import the file in your application's AppController or any other class:
+
+ @import "EKActivityIndicatorView.j"
+
+
+## Usage
+
+Inserting an EKActivityIndicatorView in your application is dead simple:
+
+ var spinner = [[EKActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 24, 24)];
+
+Set your favorite color:
+
+ [spinner setColor:[CPColor someColor]];
+
+Start the animation:
+
+ [spinner startAnimating];
+
+And if you have enough, stop it:
+
+ [spinner stopAnimating];
+
+

0 comments on commit cd10289

Please sign in to comment.
Something went wrong with that request. Please try again.