
iPhone中没有提供进度条控件,但是可以通过UIView下自己开发,下面的代码是我根据网上的代码改造的,可以做出上面这样的进度条,样子还是不错的。 .h文件
#import <UIKit/UIKit.h>
@interface BYProgressBarObject : NSObject
{float _value;//进度条的数值 0-1NSString * _labelText;//进度条上显示的文字int _level;//所处的等级 2,1,0
}
@property(nonatomic,assign) float value;
@property(nonatomic,copy) NSString * labelText;
@property(nonatomic,assign) int level;
@end
@interface BYProgressBar : UIView
{float minValue, maxValue;float lowValue,highValue;float currentValue;UIColor *lineColor, *progressRemainingColor, *progressColor;NSString * _displayString;
}
@property (readwrite) float minValue, maxValue, currentValue,lowValue,highValue;
@property (nonatomic, retain) UIColor *lineColor, *progressRemainingColor, *progressColor;
-(void)setNewRect:(CGRect)newFrame;
-(void)updateProgress:(float)valueLabelString:(NSString *) labelStringLevel:(int) level;
-(void)updateProgress:(BYProgressBarObject *) progressObject;
@end
.m文件
#import "BYProgressBar.h"
#import "Skin.h"
@implementation BYProgressBarObject
@synthesize value=_value,level=_level,labelText=_labelText;
@end
@implementation BYProgressBar
@synthesize minValue, maxValue, currentValue,lowValue,highValue;
@synthesize lineColor, progressRemainingColor, progressColor;
- (id)initWithFrame:(CGRect)frame
{if (self = [super initWithFrame:frame]){minValue = 0;maxValue = 1;currentValue = 0;self.backgroundColor = [UIColor clearColor];lineColor = [[UIColor whiteColor] retain];progressColor = [[UIColor darkGrayColor] retain];progressRemainingColor = [ProgreeBar_BackColor retain];//[[UIColor lightGrayColor] retain];}return self;
}
- (void)drawRect:(CGRect)rect
{CGContextRef context = UIGraphicsGetCurrentContext();CGContextSetLineWidth(context, 1);CGContextSetStrokeColorWithColor(context,[lineColor CGColor]);CGContextSetFillColorWithColor(context, [[progressRemainingColor colorWithAlphaComponent:.7] CGColor]);float radius = (rect.size.height / 2) - 2;CGContextMoveToPoint(context, 2, rect.size.height/2);
CGContextAddArcToPoint(context, 2, 2, radius + 2, 2, radius);CGContextAddLineToPoint(context, rect.size.width - radius - 2, 2);CGContextAddArcToPoint(context, rect.size.width - 2, 2, rect.size.width - 2, rect.size.height / 2, radius);CGContextFillPath(context);CGContextSetFillColorWithColor(context, [progressRemainingColor CGColor]);
CGContextMoveToPoint(context, rect.size.width - 2, rect.size.height/2);CGContextAddArcToPoint(context, rect.size.width - 2, rect.size.height - 2, rect.size.width - radius - 2, rect.size.height - 2, radius);CGContextAddLineToPoint(context, radius + 2, rect.size.height - 2);CGContextAddArcToPoint(context, 2, rect.size.height - 2, 2, rect.size.height/2, radius);CGContextFillPath(context);CGContextMoveToPoint(context, 2, rect.size.height/2);CGContextAddArcToPoint(context, 2, 2, radius + 2, 2, radius);CGContextAddLineToPoint(context, rect.size.width - radius - 2, 2);CGContextAddArcToPoint(context, rect.size.width - 2, 2, rect.size.width - 2, rect.size.height / 2, radius);CGContextAddArcToPoint(context, rect.size.width - 2, rect.size.height - 2, rect.size.width - radius - 2, rect.size.height - 2, radius);CGContextAddLineToPoint(context, radius + 2, rect.size.height - 2);CGContextAddArcToPoint(context, 2, rect.size.height - 2, 2, rect.size.height/2, radius);CGContextStrokePath(context);CGContextSetFillColorWithColor(context, [[progressColor colorWithAlphaComponent:.78] CGColor]);
radius = radius - 2;CGContextMoveToPoint(context, 4, rect.size.height/2);float amount = (currentValue/(maxValue - minValue)) * (rect.size.width);if (amount >= radius + 4 && amount <= (rect.size.width - radius - 4)) {CGContextAddArcToPoint(context, 4, 4, radius + 4, 4, radius);CGContextAddLineToPoint(context, amount, 4);//CGContextAddLineToPoint(context, amount, radius + 4);CGContextAddArcToPoint(context, amount + radius + 4, 4, amount + radius + 4, rect.size.height/2, radius);
CGContextFillPath(context);CGContextSetFillColorWithColor(context, [progressColor CGColor]);CGContextMoveToPoint(context, 4, rect.size.height/2);CGContextAddArcToPoint(context, 4, rect.size.height - 4, radius + 4, rect.size.height - 4, radius);CGContextAddLineToPoint(context, amount, rect.size.height - 4);CGContextAddArcToPoint(context, amount + radius + 4, rect.size.height - 4, amount + radius + 4, rect.size.height/2, radius);//CGContextAddLineToPoint(context, amount, radius + 4);CGContextFillPath(context);} else if (amount > radius + 4) {CGContextAddArcToPoint(context, 4, 4, radius + 4, 4, radius);CGContextAddLineToPoint(context, rect.size.width - radius - 4, 4);CGContextAddArcToPoint(context, rect.size.width - 4, 4, rect.size.width - 4, rect.size.height/2, radius);CGContextFillPath(context);CGContextSetFillColorWithColor(context, [progressColor CGColor]);CGContextMoveToPoint(context, 4, rect.size.height/2);CGContextAddArcToPoint(context, 4, rect.size.height - 4, radius + 4, rect.size.height - 4, radius);CGContextAddLineToPoint(context, rect.size.width - radius - 4, rect.size.height - 4);CGContextAddArcToPoint(context, rect.size.width - 4, rect.size.height - 4, rect.size.width - 4, rect.size.height/2, radius);CGContextFillPath(context);} else if (amount < radius + 4 && amount > 0) {CGContextAddArcToPoint(context, 4, 4, radius + 4, 4, radius);CGContextAddLineToPoint(context, radius + 4, rect.size.height/2);CGContextFillPath(context);CGContextSetFillColorWithColor(context, [progressColor CGColor]);CGContextMoveToPoint(context, 4, rect.size.height/2);CGContextAddArcToPoint(context, 4, rect.size.height - 4, radius + 4, rect.size.height - 4, radius);CGContextAddLineToPoint(context, radius + 4, rect.size.height/2);CGContextFillPath(context);}//显示文字if (_displayString ) {//画每个块上的文字CGContextSetLineWidth(context, 1.0); //设置矩形填充颜色:白色 CGContextSetRGBFillColor (context, 255.0/255.0, 255.0/255.0,255.0/255.0, 1.0); //设置字体 UIFont * font = [UIFont systemFontOfSize:14];CGSize labelSize = [_displayString sizeWithFont:fontconstrainedToSize:rect.sizelineBreakMode:UILineBreakModeCharacterWrap];//在指定的矩形区域内画文字CGRect textRect=CGRectMake(0, (rect.size.height-labelSize.height)/2, rect.size.width, rect.size.height);[_displayString drawInRect:textRect withFont:font lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentCenter];}
}
-(void)setNewRect:(CGRect)newFrame
{self.frame = newFrame;[self setNeedsDisplay];
}
-(void)setMinValue:(float)newMin
{minValue = newMin;[self setNeedsDisplay];
}
-(void)setMaxValue:(float)newMax
{maxValue = newMax;[self setNeedsDisplay];
}
-(void)setCurrentValue:(float)newValue
{currentValue = newValue;[self setNeedsDisplay];
}
-(void)setLowValue:(float)newValue
{lowValue = newValue;[self setNeedsDisplay];
}
-(void)setHighValue:(float)newValue
{highValue = newValue;[self setNeedsDisplay];
}
-(void)setLineColor:(UIColor *)newColor
{[newColor retain];[lineColor release];lineColor = newColor;[self setNeedsDisplay];
}
-(void)setProgressColor:(UIColor *)newColor
{[newColor retain];[progressColor release];progressColor = newColor;[self setNeedsDisplay];
}
-(void)setProgressRemainingColor:(UIColor *)newColor
{[newColor retain];[progressRemainingColor release];progressRemainingColor = newColor;[self setNeedsDisplay];
}
- (void)dealloc
{[lineColor release];[progressColor release];[progressRemainingColor release];[super dealloc];
}
-(void)updateProgress:(BYProgressBarObject *) progressObject
{[self updateProgress:progressObject.value LabelString:progressObject.labelText Level:progressObject.level];
}
-(void)updateProgress:(float)valueLabelString:(NSString *) labelStringLevel:(int) level
{currentValue = value;UIColor * newColor=nil;switch (level) {case 0://低newColor=Status_Bar_Low_Orange;break;case 1://中newColor=Status_Bar_Middle_Yello;break;case 2://高newColor=Status_Bar_High_Green;break;default:break;}[newColor retain];[progressColor release];progressColor = newColor;_displayString=labelString;[self setNeedsDisplay];
}
@end