-
Notifications
You must be signed in to change notification settings - Fork 1
/
CircularProgressView.m
74 lines (64 loc) · 2.03 KB
/
CircularProgressView.m
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
//
// CircularProgressView.m
// SellMyiPhone
//
// Created by Vincent on 1/12/17.
// Copyright © 2017 zssr. All rights reserved.
//
#import "CircularProgressView.h"
#define LineWidth 5.f
#define Space 7.f
#define Yellow [UIColor colorWithRed:0.9725 green:0.7412 blue:0.1725 alpha:1]
@implementation CircularProgressView
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
self.backgroundColor = [UIColor clearColor];
}
return self;
}
- (void)setProgress:(float)progress {
if (progress >= 100.f) {
_progress = 100.f;
}
_progress = progress;
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
[self drawBackground];
[self drawProgressLine];
}
- (void)drawProgressLine {
[self drawCircleDashed:NO];
}
- (void)drawBackground {
CGRect rect = self.bounds;
CGPoint center = CGPointMake(CGRectGetWidth(rect) / 2.f, CGRectGetHeight(rect) / 2.f);
CGFloat radius = MIN(CGRectGetHeight(rect), CGRectGetWidth(rect)) / 2 - Space - LineWidth;
// draw pie
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:0 endAngle:M_PI * 2 clockwise:YES];
[Yellow setFill];
[path fill];
// draw dash circle
[self drawCircleDashed:YES];
}
- (void)drawCircleDashed:(BOOL)dash {
CGRect rect = self.bounds;
CGPoint center = CGPointMake(CGRectGetWidth(rect) / 2.f, CGRectGetHeight(rect) / 2.f);
CGFloat radius = MIN(CGRectGetHeight(rect), CGRectGetWidth(rect)) / 2 - LineWidth / 2;
CGFloat endAngle = 2 * M_PI;
CGFloat startAngle = 0;
if (!dash) {
startAngle = -M_PI_2;
endAngle = M_PI * 2 * self.progress / 100.f - M_PI_2;
}
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
if (dash) {
CGFloat lengths[] = {2, 6};
[path setLineDash:lengths count:2 phase:0];
}
[path setLineWidth:LineWidth];
[Yellow setStroke];
[path stroke];
}
@end