This repository was archived by the owner on Jul 31, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSegmentedCircleView.m
More file actions
93 lines (75 loc) · 3.24 KB
/
Copy pathSegmentedCircleView.m
File metadata and controls
93 lines (75 loc) · 3.24 KB
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
//
// SegmentedCircleView.m
// Carbook
//
// Created by user on 17.11.15.
// Copyright © 2015 Notinum. All rights reserved.
//
#import "SegmentedCircleView.h"
@implementation ValueColorDTO
- (id)initWithValue:(CGFloat)value color:(UIColor *)color {
if (self = [super init]) {
_value = value;
_color = color;
}
return self;
}
@end
@interface SegmentedCircleView()
@property (nonatomic) CGFloat duration;
@property (nonatomic) CGFloat strokeWidth;
@property (nonatomic) CGFloat availableSpace;
@property (nonatomic, strong) UIBezierPath *circlePath;
@end
@implementation SegmentedCircleView
- (id)initWithFrame:(CGRect)frame duration:(CGFloat)duration strokeWidth:(CGFloat)strokeWidth valueColorArray:(NSArray<ValueColorDTO *> *)valueColorArray {
if (self = [super initWithFrame:frame]) {
_duration = duration;
_strokeWidth = strokeWidth;
_availableSpace = 1.;
CGPoint arcCenter = CGPointMake(CGRectGetMidX(self.bounds),
CGRectGetMidY(self.bounds));
CGFloat radius = CGRectGetMidX(self.bounds) > CGRectGetMidY(self.bounds) ? CGRectGetMidY(self.bounds) - _strokeWidth / 2. : CGRectGetMidX(self.bounds) - _strokeWidth / 2.;
self.circlePath = [UIBezierPath bezierPathWithArcCenter:arcCenter
radius:radius
startAngle:-M_PI/2
endAngle:-M_PI*2.5
clockwise:NO];
[self addBackgroundLayer];
CGFloat total = 0;
for (ValueColorDTO *valueColor in valueColorArray) {
total += valueColor.value;
}
for (ValueColorDTO *valueColor in valueColorArray) {
[self addValueLayer:valueColor.color percentage:valueColor.value/total];
}
}
return self;
}
- (void)addBackgroundLayer {
CAShapeLayer *backgroundLayer = [CAShapeLayer layer];
backgroundLayer.path = _circlePath.CGPath;
backgroundLayer.strokeColor = [[UIColor colorWithWhite:.3 alpha:.3] CGColor];
backgroundLayer.fillColor = [[UIColor clearColor] CGColor];
backgroundLayer.lineWidth = _strokeWidth;
[self.layer addSublayer:backgroundLayer];
}
- (void)addValueLayer:(UIColor *) color percentage:(CGFloat)percentage {
CAShapeLayer *percentageLayer = [CAShapeLayer layer];
percentageLayer.path = _circlePath.CGPath;
percentageLayer.strokeColor = [color CGColor];
percentageLayer.fillColor = [[UIColor clearColor] CGColor];
percentageLayer.lineWidth = _strokeWidth;
percentageLayer.strokeEnd = 0;
[self.layer addSublayer:percentageLayer];
CABasicAnimation *percentageLayerAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
percentageLayerAnimation.duration = _duration;
percentageLayerAnimation.fromValue = @(percentageLayer.strokeEnd);
percentageLayerAnimation.toValue = @(_availableSpace);
[percentageLayer addAnimation:percentageLayerAnimation forKey:@"strokeEndAnimation"];
percentageLayer.strokeEnd = _availableSpace;
_availableSpace -= percentage;
if (_availableSpace < 0)
_availableSpace = 0;
}
@end