Skip to content

Commit

Permalink
Reimplemented MBRoundProgressView.
Browse files Browse the repository at this point in the history
- no longer a UIProgressView subclass
  • Loading branch information
matej committed Apr 29, 2011
1 parent 0a305eb commit 1ea2644
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
10 changes: 6 additions & 4 deletions MBProgressHUD.h
Expand Up @@ -64,13 +64,15 @@ typedef enum {
/**
* A progress view for showing definite progress by filling up a circle (similar to the indicator for building in xcode).
*/
@interface MBRoundProgressView : UIProgressView {}
@interface MBRoundProgressView : UIView {
@private
float _progress;
}

/**
* Create a 37 by 37 pixel indicator.
* This is the same size as used by the larger UIActivityIndicatorView.
* Progress (0.0 to 1.0)
*/
- (id)initWithDefaultSize;
@property (nonatomic, assign) float progress;

@end

Expand Down
56 changes: 43 additions & 13 deletions MBProgressHUD.m
Expand Up @@ -161,7 +161,7 @@ - (void)updateIndicators {
}

if (mode == MBProgressHUDModeDeterminate) {
self.indicator = [[[MBRoundProgressView alloc] initWithDefaultSize] autorelease];
self.indicator = [[[MBRoundProgressView alloc] init] autorelease];
}
else if (mode == MBProgressHUDModeCustomView && self.customView != nil){
self.indicator = self.customView;
Expand All @@ -183,8 +183,6 @@ - (void)updateIndicators {
#define LABELFONTSIZE 16.0
#define LABELDETAILSFONTSIZE 12.0

#define PI 3.14159265358979323846


#pragma mark -
#pragma mark Class methods
Expand Down Expand Up @@ -622,35 +620,67 @@ - (void)setTransformForCurrentOrientation:(BOOL)animated {

@end

/////////////////////////////////////////////////////////////////////////////////////////////

@implementation MBRoundProgressView

- (id)initWithDefaultSize {
return [super initWithFrame:CGRectMake(0.0f, 0.0f, 37.0f, 37.0f)];
#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 = CGRectMake(allRect.origin.x + 2, allRect.origin.y + 2, allRect.size.width - 4,
allRect.size.height - 4);
CGRect circleRect = CGRectInset(allRect, 2.0f, 2.0f);

CGContextRef context = UIGraphicsGetCurrentContext();

// Draw background
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0); // white
CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 0.1); // translucent white
CGContextSetLineWidth(context, 2.0);
CGContextSetLineWidth(context, 2.0f);
CGContextFillEllipseInRect(context, circleRect);
CGContextStrokeEllipseInRect(context, circleRect);

// Draw progress
float x = (allRect.size.width / 2);
float y = (allRect.size.height / 2);
CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0); // white
CGContextMoveToPoint(context, x, y);
CGContextAddArc(context, x, y, (allRect.size.width - 4) / 2, -(PI / 2), (self.progress * 2 * PI) - PI / 2, 0);
CGPoint center = CGPointMake(allRect.size.width / 2, allRect.size.height / 2);
CGFloat radius = (allRect.size.width - 4) / 2;
CGFloat startAngle = - (M_PI / 2); // 90 degrees
CGFloat endAngle = (self.progress * 2 * M_PI) + startAngle;
CGContextSetRGBFillColor(context, 1.0f, 1.0f, 1.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

0 comments on commit 1ea2644

Please sign in to comment.