Skip to content

Instantly share code, notes, and snippets.

@saturngod
Created September 18, 2011 02:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saturngod/1224648 to your computer and use it in GitHub Desktop.
Save saturngod/1224648 to your computer and use it in GitHub Desktop.
MBRoundProgressView
#import <UIKit/UIKit.h>
@interface MBRoundProgressView : UIView
- (void)setProgress:(float)progress;
- (float)progress;
@end
#import "MBRoundProgressView.h"
#define forcolor(x) x/255.0f
@interface MBRoundProgressView() {
@private
float _progress;
}
@end
@implementation MBRoundProgressView
#pragma mark -
#pragma mark Accessors
- (float)progress {
return _progress;
}
- (void)setProgress:(float)progress {
_progress = progress;
[self setNeedsDisplay];
}
#pragma mark -
#pragma mark Lifecycle
- (id)init {
return [self initWithFrame:CGRectMake(0.0f, 0.0f, 37.0f, 37.0f)];
}
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
self.opaque = NO;
}
return self;
}
#pragma mark -
#pragma mark Drawing
- (void)drawRect:(CGRect)rect {
CGRect allRect = self.bounds;
CGRect circleRect = CGRectInset(allRect, 2.0f, 2.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
// Draw background
CGContextSetRGBStrokeColor(context, forcolor(128.0f), forcolor(168.0f), forcolor(221.0f), 1.0f); // white
CGContextSetRGBFillColor(context, 1.0f, 1.0f, 1.0f, 0.1f); // translucent white
CGContextSetLineWidth(context, 2.0f);
CGContextFillEllipseInRect(context, circleRect);
CGContextStrokeEllipseInRect(context, circleRect);
// Draw progress
CGPoint center = CGPointMake(allRect.size.width / 2, allRect.size.height / 2);
CGFloat radius = (allRect.size.width - 4) / 2;
CGFloat startAngle = - ((float)M_PI / 2); // 90 degrees
CGFloat endAngle = (self.progress * 2 * (float)M_PI) + startAngle;
CGContextSetRGBFillColor(context, forcolor(128.0f), forcolor(168.0f), forcolor(221.0f), 1.0f); // white
CGContextMoveToPoint(context, center.x, center.y);
CGContextAddArc(context, center.x, center.y, radius, startAngle, endAngle, 0);
CGContextClosePath(context);
CGContextFillPath(context);
}
@end
MBRoundProgressView* rounded = [[[MBRoundProgressView alloc] init] autorelease];
[rounded setProgress:((arc4random() % 10)/9.0f)];
[self.view addSubview:rounded];
[rounded release];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment