Permalink
Browse files

Added color support to MBRoundProgressView.

  • Loading branch information...
1 parent 48a98d3 commit 98c85444eb4020e3bf5cb8f86c465dce8e6eec90 @matej matej committed Nov 23, 2012
Showing with 55 additions and 4 deletions.
  1. +12 −0 MBProgressHUD.h
  2. +43 −4 MBProgressHUD.m
View
@@ -431,6 +431,18 @@ typedef void (^MBProgressHUDCompletionBlock)();
*/
@property (nonatomic, assign) float progress;
+/**
+ * Indicator progress color.
+ * Defaults to white [UIColor whiteColor]
+ */
+@property (nonatomic, MB_STRONG) UIColor *progressTintColor;
+
+/**
+ * Indicator background (non-progress) color.
+ * Defaults to translucent white (alpha 0.1)
+ */
+@property (nonatomic, MB_STRONG) UIColor *backgroundTintColor;
+
/*
* Display mode - NO = round or YES = annular. Defaults to round.
*/
View
@@ -730,6 +730,11 @@ @implementation MBRoundProgressView {
BOOL _annular;
}
+#pragma mark - Properties
+
+@synthesize progressTintColor = _progressTintColor;
+@synthesize backgroundTintColor = _backgroundTintColor;
+
#pragma mark - Accessors
- (float)progress {
@@ -763,10 +768,22 @@ - (id)initWithFrame:(CGRect)frame {
self.opaque = NO;
_progress = 0.f;
_annular = NO;
+ _progressTintColor = [[UIColor alloc] initWithWhite:1.f alpha:1.f];
+ _backgroundTintColor = [[UIColor alloc] initWithWhite:1.f alpha:.1f];
+ [self registerForKVO];
}
return self;
}
+- (void)dealloc {
+ [self unregisterFromKVO];
+#if !__has_feature(objc_arc)
+ [_progressTintColor release];
+ [_backgroundTintColor release];
+ [super dealloc];
+#endif
+}
+
#pragma mark - Drawing
- (void)drawRect:(CGRect)rect {
@@ -786,20 +803,20 @@ - (void)drawRect:(CGRect)rect {
CGFloat startAngle = - ((float)M_PI / 2); // 90 degrees
CGFloat endAngle = (2 * (float)M_PI) + startAngle;
[processBackgroundPath addArcWithCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
- [[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.1f] set];
+ [_backgroundTintColor set];
[processBackgroundPath stroke];
// Draw progress
UIBezierPath *processPath = [UIBezierPath bezierPath];
processPath.lineCapStyle = kCGLineCapRound;
processPath.lineWidth = lineWidth;
endAngle = (self.progress * 2 * (float)M_PI) + startAngle;
[processPath addArcWithCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
- [[UIColor whiteColor] set];
+ [_progressTintColor set];
[processPath stroke];
} else {
// Draw background
- CGContextSetRGBStrokeColor(context, 1.0f, 1.0f, 1.0f, 1.0f); // white
- CGContextSetRGBFillColor(context, 1.0f, 1.0f, 1.0f, 0.1f); // translucent white
+ [_progressTintColor setStroke];
+ [_backgroundTintColor setFill];
CGContextSetLineWidth(context, 2.0f);
CGContextFillEllipseInRect(context, circleRect);
CGContextStrokeEllipseInRect(context, circleRect);
@@ -816,4 +833,26 @@ - (void)drawRect:(CGRect)rect {
}
}
+#pragma mark - KVO
+
+- (void)registerForKVO {
+ for (NSString *keyPath in [self observableKeypaths]) {
+ [self addObserver:self forKeyPath:keyPath options:NSKeyValueObservingOptionNew context:NULL];
+ }
+}
+
+- (void)unregisterFromKVO {
+ for (NSString *keyPath in [self observableKeypaths]) {
+ [self removeObserver:self forKeyPath:keyPath];
+ }
+}
+
+- (NSArray *)observableKeypaths {
+ return [NSArray arrayWithObjects:@"progressTintColor", @"backgroundTintColor", nil];
+}
+
+- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
+ [self setNeedsDisplay];
+}
+
@end

0 comments on commit 98c8544

Please sign in to comment.