博主從事iOS開發(fā)3年多,看過許多大神的博客,感覺他們文筆都很好,講解很透徹,但是本人一直沒有寫過博客,今天鼓起勇氣寫下這個(gè)入門級(jí)的小東西--實(shí)用的進(jìn)度條,想用的盡管拿去.我隨手封裝的東西,如果有可以改進(jìn)的地方,你又愿意提點(diǎn)我,請(qǐng)留言,謝謝!如果不喜歡,請(qǐng)關(guān)閉本文,謝謝!
好了,廢話不多說,進(jìn)入主題!
由于目前項(xiàng)目中要用到進(jìn)度條,但是系統(tǒng)的UIProgressView的高度和背景顏色不一致,想了幾種辦法嘗試解決這個(gè)問題,但是感覺有點(diǎn)費(fèi)勁,然后,然后,我就自己搞了一個(gè),先用著.歡迎留言交流......
上代碼:
LQProgressLine.h
@interface LQProgressLine : UIView
/**
* 背景色
*/
@property (nonatomic,copy) NSString *backColor;
/**
* 已經(jīng)經(jīng)過的進(jìn)度的顏色
*/
@property (nonatomic,copy) NSString *didColor;
/**
* 進(jìn)度
*/
@property (nonatomic,assign) CGFloat progress;
/**
* 工廠方法
*
* @param bColor 背景顏色 (例如"#ffffff")
* @param dColor 已經(jīng)經(jīng)過的進(jìn)度的顏色(例如"#000000")
*
* @return 對(duì)象
*/
+(instancetype)progressLineWithBackColor:(NSString *)bColor didColor:(NSString *)dColor;
LQProgressLine.m
#import "LQProgressLine.h"
@interface LQProgressLine ()
@property (nonatomic,strong) NSArray *backColorArray;
@property (nonatomic,strong) NSArray *didColorArray;
@end
@implementation LQProgressLine
+(instancetype)progressLineWithBackColor:(NSString *)bColor didColor:(NSString *)dColor{
return [[self alloc]initWithBackColor:bColor didColor:dColor];
}
- (instancetype)initWithBackColor:(NSString *)bColor didColor: (NSString *)dColor{
if (self = [super init]) {
self.backColor = bColor;
self.backgroundColor = [UIColor colorWithRed:[self.backColorArray[0] floatValue] green:[self.backColorArray[1] floatValue] blue:[self.backColorArray[2] floatValue] alpha:1];
self.didColor = dColor;
}
return self;
}
- (void)setBackColor:(NSString *)backColor{
_backColor = backColor;
self.backColorArray = [self RGBFromStr:backColor];
}
- (void)setDidColor:(NSString *)didColor{
_didColor = didColor;
self.didColorArray = [self RGBFromStr:didColor];
}
- (void)drawRect:(CGRect)rect{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, rect.size.width); //線寬
CGContextSetRGBStrokeColor(context, [self.didColorArray[0] floatValue], [self.didColorArray[1] floatValue], [self.didColorArray[2] floatValue], 1.0); //線的顏色
CGContextBeginPath(context);
CGContextMoveToPoint(context, 0, 0); //起點(diǎn)坐標(biāo)
CGContextAddLineToPoint(context, rect.size.width*_progress, rect.size.height); //終點(diǎn)坐標(biāo)
CGContextStrokePath(context);
}
- (void)setProgress:(CGFloat)progress{
_progress = progress;
[self setNeedsDisplay];
}
- (NSArray *)RGBFromStr:(NSString *)hexColor{
NSString *str = nil;
if([hexColor rangeOfString:@"#"].length>0){
str = [hexColor substringFromIndex:1];
}else{
str = hexColor;
}
if(str.length<=0)return nil;
unsigned int red, green, blue;
NSRange range;
range.length =2;
range.location =0;
[[NSScanner scannerWithString:[str substringWithRange:range]]scanHexInt:&red];
range.location =2;
[[NSScanner scannerWithString:[str substringWithRange:range]]scanHexInt:&green];
range.location =4;
[[NSScanner scannerWithString:[str substringWithRange:range]]scanHexInt:&blue];
return @[@(red/255.0),@(green/255.0),@(blue/255.0)];
}
好了,代碼貼完,用法如下:
//創(chuàng)建
LQProgressLine*progressView = [LQProgressLine progressLineWithBackColor: @"#cccccc" didColor:@"#0096e0"];
[superView addSubview:progressView];
//在需要設(shè)置進(jìn)度的時(shí)候設(shè)置
[progressView setProgress:0.4];