单军华
2018-03-29 89d748b77b478905732e60f0b4c5807c274b6565
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//
//  commenProgressView.m
//  istanbul
//
//  Created by WindShan on 2017/5/15.
//  Copyright © 2017年 WindShan. All rights reserved.
//
 
#import "commenProgressView.h"
 
@interface commenProgressView ()
 
@property (strong,nonatomic) UIView *firstView;
@property (strong,nonatomic) UIView *secondView;
@property (strong,nonatomic) UILabel *progressLabel;
 
@end
 
@implementation commenProgressView
 
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
 
+ (instancetype)initCommenProgressView{
    return [[self alloc]init];
}
 
- (UIView *)firstView{
    if (_firstView == nil){
        UIView *fView = [[UIView alloc]init];
        fView.backgroundColor = _progressBackGroundColor;
        fView.layer.masksToBounds = YES;
        fView.layer.cornerRadius = _progressCornerRadius;
        [self addSubview:fView];
        _firstView = fView;
    }
    return _firstView;
}
 
- (UIView *)secondView{
    if (_secondView == nil){
        UIView *sView = [[UIView alloc]init];
        sView.backgroundColor = _progressTintColor;
        sView.layer.masksToBounds = YES;
        sView.layer.cornerRadius = _progressCornerRadius;
        [self.firstView addSubview:sView];
        _secondView = sView;
    }
    return _secondView;
}
 
- (UILabel *)progressLabel{
    if (_progressLabel == nil){
        UILabel *lb = [[UILabel alloc]init];
        lb.textAlignment = NSTextAlignmentRight;
        CGFloat value = _progressValue * 100;
        NSString *valueStr = [[NSString stringWithFormat:@"%.1f",value]stringByAppendingString:@"%"];
        lb.text = valueStr;
        lb.textColor = [UIColor whiteColor];
        _progressLabel = lb;
        [self insertSubview:lb aboveSubview:_secondView];
    }
    return _progressLabel;
}
 
 
 
- (void)layoutSubviews{
    [super layoutSubviews];
    self.secondView.frame = CGRectMake(0, 0, self.frame.size.width * _progressValue, self.frame.size.height);
    self.firstView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    self.progressLabel.frame = CGRectMake(-10, 0, self.frame.size.width, self.frame.size.height);
    
    NSString * valueStr = [[NSString stringWithFormat:@"%.1f",_progressValue*100] stringByAppendingString:@"%"];
    self.progressLabel.text = valueStr;
    
}
 
#pragma mark - set&&get
- (void)setProgressBackGroundColor:(UIColor *)progressBackGroundColor{
    
    _progressBackGroundColor = progressBackGroundColor;
}
 
- (void)setProgressValue:(CGFloat)progressValue{
    _progressValue = progressValue;
}
 
- (void)setProgressTintColor:(UIColor *)progressTintColor{
    self.layer.borderColor = progressTintColor.CGColor;
    
    _progressTintColor = progressTintColor;
}
 
 
- (void)setProgressCornerRadius:(NSInteger)progressCornerRadius{
    
    _progressCornerRadius = progressCornerRadius;
}
 
- (void)setProgressBorderWidth:(NSInteger)progressBorderWidth{
    self.layer.borderWidth = progressBorderWidth;
    _progressBorderWidth = progressBorderWidth;
}
 
@end