AceSL
2020-11-21 a609d13ff3c5f4b58b286dbb9419fc9670799c99
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
//
//  MyCircleView.m
//  MyCircleView
//
//  Created by 张威 on 2020/11/20.
//  Copyright © 2020 JSPY. All rights reserved.
//
 
#import "MyCircleView.h"
 
@interface MyCircleView ()
 
@property (nonatomic, strong) NSTimer * labelTimer;
@property (nonatomic, strong) UIView * centerView;
@property (nonatomic, strong) UILabel * numLabel;
@property (nonatomic, strong) UILabel * textLabel;
@property (nonatomic, strong) UILabel * valueLabel;
 
@property (nonatomic, assign) CGFloat progressFlag;
@property (nonatomic, assign) NSInteger progressValue;
 
@property (nonatomic, strong) CAShapeLayer * borderLayer;     //虚线
@property (nonatomic, strong) CAShapeLayer * backgroundLine;  //进度条背景
@property (nonatomic, strong) CAShapeLayer * mainLine;        //进度条
@property (nonatomic, strong) CAShapeLayer * maskLayer;       //遮罩层
@property (nonatomic, strong) CAGradientLayer * rightGrain;   //渐变layer
@property (nonatomic, strong) CAGradientLayer * leftGrain;    //渐变layer
 
@property (nonatomic, strong) CABasicAnimation * pathAnimation;//加载动画
 
@property (nonatomic, strong) UIBezierPath * borderPath;       //虚线路径
@property (nonatomic, strong) UIBezierPath * bezierPath;       //进度条路径
 
@end
 
@implementation MyCircleView
 
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        _strokelineWidth = 6;
        
        //外层虚线
        [self.layer addSublayer:self.borderLayer];
        self.borderLayer.path = self.borderPath.CGPath;
        
        //进度条
        [self.layer addSublayer:self.backgroundLine];
        [self.layer addSublayer:self.mainLine];
        self.backgroundLine.path = self.bezierPath.CGPath;
        self.mainLine.path = self.bezierPath.CGPath;
        
        //添加渐变色
        [self.layer addSublayer:self.maskLayer];
        [self.maskLayer addSublayer:self.rightGrain];
        [self.maskLayer addSublayer:self.leftGrain];
        [self.maskLayer setMask:self.mainLine];
        
        [self addSubview:self.centerView];
    }
    
    return self;
}
 
- (void)circleWithProgress:(NSInteger)progress value:(CGFloat)value
{
    _progressFlag = 0;
    _progressValue = progress;
 
    //进度条添加动画
    self.pathAnimation.duration = progress/100.0f;
    self.pathAnimation.toValue = [NSNumber numberWithFloat:progress/100.f];
    [self.mainLine addAnimation:self.pathAnimation forKey:@"strokeEndAnimation"];
 
    //添加文字内容
    _valueLabel.text = [NSString stringWithFormat:@"%.1fGB/128GB", value];
 
    if (progress > 0){
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            self->_labelTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(NameLbChange) userInfo:nil repeats:YES];
            [[NSRunLoop currentRunLoop] run];
        });
    }
}
 
#pragma mark - timer methods -
 
- (void)NameLbChange
{
    dispatch_async(dispatch_get_main_queue(), ^{
        self->_numLabel.attributedText = [self labelStytle:self->_progressFlag];
    });
    
    _progressFlag += 1;
    
    if (_progressFlag > _progressValue) {
        [_labelTimer invalidate];
        _labelTimer = nil;
    }
}
 
- (NSMutableAttributedString *)labelStytle:(NSInteger)value
{
    if (value > _progressValue) {
        value = _progressValue;
    }
    
    NSString * pace = [NSString stringWithFormat:@"%ld%@", value, @"%"];
    NSMutableAttributedString * pace1 = [[NSMutableAttributedString alloc] initWithString:pace];
    [pace1 addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:40] range:NSMakeRange(0, pace.length - 1)];
    [pace1 addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:20] range:NSMakeRange(pace.length - 1, 1)];
    NSRange titleRange = NSMakeRange(0, pace.length);
    [pace1 addAttribute:NSForegroundColorAttributeName
                  value:[UIColor whiteColor]
                  range:titleRange];
    return pace1;
}
 
 
#pragma mark - lazy -
 
- (UIView *)centerView
{
    if (!_centerView) {
        _centerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width - _strokelineWidth*2 - 30, self.bounds.size.width - _strokelineWidth*2 - 30)];
        _centerView.center = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);
        _centerView.backgroundColor = [UIColor colorWithRed:85/255.0 green:172/255.0 blue:234/255.0 alpha:1.0];
        _centerView.layer.masksToBounds = YES;
        _centerView.layer.cornerRadius = (self.bounds.size.width - _strokelineWidth*2 - 30) / 2;
        
        [_centerView addSubview:self.numLabel];
        [_centerView addSubview:self.textLabel];
        [_centerView addSubview:self.valueLabel];
    }
    
    return _centerView;
}
 
- (UILabel *)valueLabel
{
    if (!_valueLabel) {
        _valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, _centerView.bounds.size.height/2 + 40, _centerView.bounds.size.width, 14)];
        _valueLabel.text = @"40GB/128GB";
        _valueLabel.textColor = [UIColor whiteColor];
        _valueLabel.backgroundColor = [UIColor clearColor];
        _valueLabel.textAlignment = NSTextAlignmentCenter;
        _valueLabel.font = [UIFont systemFontOfSize:14];
    }
    
    return _valueLabel;
}
 
- (UILabel *)textLabel
{
    if (!_textLabel) {
        _textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, _centerView.bounds.size.height/2 - 40 - 12, _centerView.bounds.size.width, 12)];
        _textLabel.text = @"已使用";
        _textLabel.textColor = [UIColor whiteColor];
        _textLabel.backgroundColor = [UIColor clearColor];
        _textLabel.textAlignment = NSTextAlignmentCenter;
        _textLabel.font = [UIFont systemFontOfSize:12];
    }
    
    return _textLabel;
}
 
- (UILabel *)numLabel
{
    if(!_numLabel) {
        _numLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, _centerView.bounds.size.width, 40)];
        _numLabel.center = CGPointMake(_centerView.bounds.size.width/2, _centerView.bounds.size.height/2);
        _numLabel.textAlignment = NSTextAlignmentCenter;
        _numLabel.backgroundColor = [UIColor clearColor];
        _numLabel.font = [UIFont boldSystemFontOfSize:40];
        _numLabel.textColor = [UIColor whiteColor];
        _numLabel.text = @"0";
    }
    
    return _numLabel;
}
 
- (CAShapeLayer *)borderLayer
{
    if (!_borderLayer) {
        _borderLayer = [[CAShapeLayer alloc] init];
        _borderLayer.fillColor = [UIColor clearColor].CGColor;
        _borderLayer.strokeColor = [UIColor colorWithRed:85/255.0 green:172/255.0 blue:234/255.0 alpha:1.0].CGColor;
        _borderLayer.lineWidth = 2.f;
        _borderLayer.lineCap = @"round";
        _borderLayer.lineDashPattern = @[@2, @4];
    }
    
    return _borderLayer;
}
 
- (CAShapeLayer *)backgroundLine
{
    if(!_backgroundLine) {
        _backgroundLine = [[CAShapeLayer alloc] init];
        _backgroundLine.fillColor = [UIColor clearColor].CGColor;
        _backgroundLine.strokeColor = [UIColor colorWithRed:85/255.0 green:172/255.0 blue:234/255.0 alpha:1.0].CGColor;
        _backgroundLine.lineWidth = _strokelineWidth;
    }
    
    return _backgroundLine;
}
 
- (CAShapeLayer *)mainLine
{
    if(!_mainLine) {
        _mainLine = [[CAShapeLayer alloc] init];
        _mainLine.fillColor = [UIColor clearColor].CGColor;
        _mainLine.strokeColor = [UIColor whiteColor].CGColor;
        _mainLine.lineWidth = _strokelineWidth;
        _mainLine.lineCap = @"round";
        _mainLine.strokeStart = 0.01;
    }
    
    return _mainLine;
}
 
- (CAShapeLayer *)maskLayer
{
    if (!_maskLayer) {
        _maskLayer = [CAShapeLayer layer];
    }
    
    return _maskLayer;
}
 
- (CAGradientLayer *)rightGrain
{
    if(!_rightGrain) {
        _rightGrain = [[CAGradientLayer alloc] init];
        _rightGrain.frame = CGRectMake(self.bounds.size.width/2, 0, self.bounds.size.width/2, self.bounds.size.height);
        [_rightGrain setColors:[NSArray arrayWithObjects:
                           (id)[[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0] CGColor],
                           (id)[[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0] CGColor],
                           nil]];
        [_rightGrain setLocations:@[@0, @1.0]];
        [_rightGrain setStartPoint:CGPointMake(0.5, 0)];
        [_rightGrain setEndPoint:CGPointMake(0.5, 1)];
        _rightGrain.type = kCAGradientLayerAxial;
    }
    
    return _rightGrain;
}
 
- (CAGradientLayer *)leftGrain
{
    if (!_leftGrain) {
        _leftGrain = [[CAGradientLayer alloc] init];
        _leftGrain.frame = CGRectMake(0, 0, self.bounds.size.width/2, self.bounds.size.height);
        [_leftGrain setColors:[NSArray arrayWithObjects:
                           (id)[[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0] CGColor],
                           (id)[[UIColor colorWithRed:255/255.0 green:0/255.0 blue:0/255.0 alpha:1.0] CGColor],
                           nil]];
        [_leftGrain setLocations:@[@0.5, @1.0]];
        [_leftGrain setStartPoint:CGPointMake(0.5, 1)];
        [_leftGrain setEndPoint:CGPointMake(0.5, 0)];
        _leftGrain.type = kCAGradientLayerAxial;
    }
    
    return _leftGrain;
}
 
- (CABasicAnimation *)pathAnimation
{
    if (!_pathAnimation) {
        _pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
        _pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
        _pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
        _pathAnimation.fillMode = kCAFillModeForwards;
        _pathAnimation.removedOnCompletion = NO;
    }
    
    return _pathAnimation;
}
 
- (UIBezierPath *)borderPath
{
    if (!_borderPath) {
        _borderPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.bounds.size.width/2, self.bounds.size.width/2) radius:self.bounds.size.width/2 startAngle:-M_PI_2 endAngle:-M_PI_2 + M_PI*2 clockwise:YES];
    }
    
    return _borderPath;
}
 
- (UIBezierPath *)bezierPath
{
    if (!_bezierPath) {
        _bezierPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2) radius:self.bounds.size.width/2 - _strokelineWidth - 4 startAngle:-M_PI_2 endAngle:-M_PI_2 + M_PI*2 clockwise:YES];
    }
    
    return _bezierPath;
}
 
@end