Skip to content

Commit c45743c

Browse files
committed
Merge pull request jdg#139 from jjxtra/master
Add horizontal progress bar
2 parents c24d6e7 + e726947 commit c45743c

File tree

2 files changed

+214
-0
lines changed

2 files changed

+214
-0
lines changed

MBProgressHUD.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ typedef enum {
3838
MBProgressHUDModeIndeterminate,
3939
/** Progress is shown using a round, pie-chart like, progress view. */
4040
MBProgressHUDModeDeterminate,
41+
/** Progress is shown using a horizontal progress bar */
42+
MBProgressHUDModeDeterminateHorizontalBar,
4143
/** Progress is shown using a ring-shaped progress view. */
4244
MBProgressHUDModeAnnularDeterminate,
4345
/** Shows a custom view */

MBProgressHUD.m

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@
2828
static const CGFloat kLabelFontSize = 16.f;
2929
static const CGFloat kDetailsLabelFontSize = 12.f;
3030

31+
@interface FlatProgressView : UIView
32+
33+
@property (nonatomic, readwrite) float minValue;
34+
@property (nonatomic, readwrite) float maxValue;
35+
@property (nonatomic, readwrite) float progress;
36+
@property (nonatomic, strong) UIColor *lineColor;
37+
@property (nonatomic, strong) UIColor *progressRemainingColor;
38+
@property (nonatomic, strong) UIColor *progressColor;
39+
40+
-(void)setNewRect:(CGRect)newFrame;
41+
42+
@end
3143

3244
@interface MBProgressHUD ()
3345

@@ -466,6 +478,15 @@ - (void)updateIndicators {
466478
[(UIActivityIndicatorView *)indicator startAnimating];
467479
[self addSubview:indicator];
468480
}
481+
else if (mode == MBProgressHUDModeDeterminateHorizontalBar) {
482+
[indicator removeFromSuperview];
483+
FlatProgressView* flatProgressView = [[FlatProgressView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 120.0f, 20.0f)];
484+
flatProgressView.progressColor = [UIColor whiteColor];
485+
flatProgressView.progressRemainingColor = [UIColor clearColor];
486+
flatProgressView.lineColor = [UIColor whiteColor];
487+
self.indicator = MB_AUTORELEASE(flatProgressView);
488+
[self addSubview:indicator];
489+
}
469490
else if (mode == MBProgressHUDModeDeterminate || mode == MBProgressHUDModeAnnularDeterminate) {
470491
if (!isRoundIndicator) {
471492
// Update to determinante indicator
@@ -863,3 +884,194 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
863884
}
864885

865886
@end
887+
888+
@implementation FlatProgressView
889+
890+
- (id)initWithFrame:(CGRect)frame
891+
{
892+
self = [super initWithFrame:frame];
893+
if (self)
894+
{
895+
_minValue = 0;
896+
_maxValue = 1;
897+
_progress = 0;
898+
self.backgroundColor = [UIColor clearColor];
899+
_lineColor = [UIColor whiteColor];
900+
_progressColor = [UIColor darkGrayColor];
901+
_progressRemainingColor = [UIColor lightGrayColor];
902+
}
903+
return self;
904+
}
905+
906+
- (void)dealloc
907+
{
908+
909+
#if !__has_feature(objc_arc)
910+
911+
[_lineColor release];
912+
[_progressColor release];
913+
[_progressRemainingColor release];
914+
915+
#endif
916+
917+
}
918+
919+
- (void)drawRect:(CGRect)rect
920+
{
921+
CGContextRef context = UIGraphicsGetCurrentContext();
922+
923+
// setup properties
924+
CGContextSetLineWidth(context, 2);
925+
CGContextSetStrokeColorWithColor(context,[_lineColor CGColor]);
926+
CGContextSetFillColorWithColor(context, [_progressRemainingColor CGColor]);
927+
928+
// draw line border
929+
float radius = (rect.size.height / 2) - 2;
930+
CGContextMoveToPoint(context, 2, rect.size.height/2);
931+
CGContextAddArcToPoint(context, 2, 2, radius + 2, 2, radius);
932+
CGContextAddLineToPoint(context, rect.size.width - radius - 2, 2);
933+
CGContextAddArcToPoint(context, rect.size.width - 2, 2, rect.size.width - 2, rect.size.height / 2, radius);
934+
CGContextAddArcToPoint(context, rect.size.width - 2, rect.size.height - 2, rect.size.width - radius - 2, rect.size.height - 2, radius);
935+
CGContextAddLineToPoint(context, radius + 2, rect.size.height - 2);
936+
CGContextAddArcToPoint(context, 2, rect.size.height - 2, 2, rect.size.height/2, radius);
937+
CGContextFillPath(context);
938+
939+
// draw progress background
940+
CGContextMoveToPoint(context, 2, rect.size.height/2);
941+
CGContextAddArcToPoint(context, 2, 2, radius + 2, 2, radius);
942+
CGContextAddLineToPoint(context, rect.size.width - radius - 2, 2);
943+
CGContextAddArcToPoint(context, rect.size.width - 2, 2, rect.size.width - 2, rect.size.height / 2, radius);
944+
CGContextAddArcToPoint(context, rect.size.width - 2, rect.size.height - 2, rect.size.width - radius - 2, rect.size.height - 2, radius);
945+
CGContextAddLineToPoint(context, radius + 2, rect.size.height - 2);
946+
CGContextAddArcToPoint(context, 2, rect.size.height - 2, 2, rect.size.height/2, radius);
947+
CGContextStrokePath(context);
948+
949+
// setup to draw progress color
950+
CGContextSetFillColorWithColor(context, [_progressColor CGColor]);
951+
radius = radius - 2;
952+
float amount = (self.progress/(self.maxValue - self.minValue)) * (rect.size.width);
953+
954+
// if progress is in the middle area
955+
if (amount >= radius + 4 && amount <= (rect.size.width - radius - 4))
956+
{
957+
// top
958+
CGContextMoveToPoint(context, 4, rect.size.height/2);
959+
CGContextAddArcToPoint(context, 4, 4, radius + 4, 4, radius);
960+
CGContextAddLineToPoint(context, amount, 4);
961+
CGContextAddLineToPoint(context, amount, radius + 4);
962+
963+
// bottom
964+
CGContextMoveToPoint(context, 4, rect.size.height/2);
965+
CGContextAddArcToPoint(context, 4, rect.size.height - 4, radius + 4, rect.size.height - 4, radius);
966+
CGContextAddLineToPoint(context, amount, rect.size.height - 4);
967+
CGContextAddLineToPoint(context, amount, radius + 4);
968+
969+
CGContextFillPath(context);
970+
}
971+
972+
// progress is in the right arc
973+
else if (amount > radius + 4)
974+
{
975+
float x = amount - (rect.size.width - radius - 4);
976+
977+
// top
978+
CGContextMoveToPoint(context, 4, rect.size.height/2);
979+
CGContextAddArcToPoint(context, 4, 4, radius + 4, 4, radius);
980+
CGContextAddLineToPoint(context, rect.size.width - radius - 4, 4);
981+
float angle = -acos(x/radius);
982+
if (isnan(angle)) angle = 0;
983+
CGContextAddArc(context, rect.size.width - radius - 4, rect.size.height/2, radius, M_PI, angle, 0);
984+
CGContextAddLineToPoint(context, amount, rect.size.height/2);
985+
986+
// bottom
987+
CGContextMoveToPoint(context, 4, rect.size.height/2);
988+
CGContextAddArcToPoint(context, 4, rect.size.height - 4, radius + 4, rect.size.height - 4, radius);
989+
CGContextAddLineToPoint(context, rect.size.width - radius - 4, rect.size.height - 4);
990+
angle = acos(x/radius);
991+
if (isnan(angle)) angle = 0;
992+
CGContextAddArc(context, rect.size.width - radius - 4, rect.size.height/2, radius, -M_PI, angle, 1);
993+
CGContextAddLineToPoint(context, amount, rect.size.height/2);
994+
995+
CGContextFillPath(context);
996+
}
997+
998+
// progress is in the left arc
999+
else if (amount < radius + 4 && amount > 0)
1000+
{
1001+
// top
1002+
CGContextMoveToPoint(context, 4, rect.size.height/2);
1003+
CGContextAddArcToPoint(context, 4, 4, radius + 4, 4, radius);
1004+
CGContextAddLineToPoint(context, radius + 4, rect.size.height/2);
1005+
1006+
// bottom
1007+
CGContextMoveToPoint(context, 4, rect.size.height/2);
1008+
CGContextAddArcToPoint(context, 4, rect.size.height - 4, radius + 4, rect.size.height - 4, radius);
1009+
CGContextAddLineToPoint(context, radius + 4, rect.size.height/2);
1010+
1011+
CGContextFillPath(context);
1012+
}
1013+
}
1014+
1015+
-(void)setNewRect:(CGRect)newFrame
1016+
{
1017+
self.frame = newFrame;
1018+
[self setNeedsDisplay];
1019+
}
1020+
1021+
-(void)setMinValue:(float)newMin
1022+
{
1023+
_minValue = newMin;
1024+
[self setNeedsDisplay];
1025+
}
1026+
1027+
-(void)setMaxValue:(float)newMax
1028+
{
1029+
_maxValue = newMax;
1030+
[self setNeedsDisplay];
1031+
}
1032+
1033+
-(void)setProgress:(float)newValue
1034+
{
1035+
_progress = newValue;
1036+
[self setNeedsDisplay];
1037+
}
1038+
1039+
-(void)setLineColor:(UIColor *)newColor
1040+
{
1041+
1042+
#if !__has_feature(objc_arc)
1043+
1044+
[_lineColor release];
1045+
1046+
#endif
1047+
1048+
_lineColor = newColor;
1049+
[self setNeedsDisplay];
1050+
}
1051+
1052+
-(void)setProgressColor:(UIColor *)newColor
1053+
{
1054+
#if !__has_feature(objc_arc)
1055+
1056+
[_progressColor release];
1057+
1058+
#endif
1059+
1060+
_progressColor = newColor;
1061+
[self setNeedsDisplay];
1062+
}
1063+
1064+
-(void)setProgressRemainingColor:(UIColor *)newColor
1065+
{
1066+
1067+
#if !__has_feature(objc_arc)
1068+
1069+
[_progressRemainingColor release];
1070+
1071+
#endif
1072+
1073+
_progressRemainingColor = newColor;
1074+
[self setNeedsDisplay];
1075+
}
1076+
1077+
@end

0 commit comments

Comments
 (0)