自定义iOS进度条 UIProgressBar

iOS进度条 自定义 iPhone中没有提供进度条控件,但是可以通过UIView下自己开发,下面的代码是我根据网上的代码改造的,可以做出上面这样的进度条,样子还是不错的。   .h文件
#import <UIKit/UIKit.h>
@interface BYProgressBarObject : NSObject
{
 float _value;//进度条的数值 0-1
 NSString * _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)value
 LabelString:(NSString *) labelString
 Level:(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:font
 constrainedToSize:rect.size
 lineBreakMode: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)value
 LabelString:(NSString *) labelString
 Level:(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
 

转载于:https://www.cnblogs.com/liuxingzi/archive/2012/12/04/3404287.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值