-
Notifications
You must be signed in to change notification settings - Fork 3
/
MSHBarView.m
73 lines (57 loc) · 1.99 KB
/
MSHBarView.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
#import "public/MSHBarView.h"
@implementation MSHBarView
-(instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.barCornerRadius = 13;
self.barSpacing = 5;
}
return self;
}
-(void)initializeWaveLayers{
[self resetWaveLayers];
[self configureDisplayLink];
}
-(void)resetWaveLayers{
self.layer.sublayers = nil;
CGFloat width = ((self.frame.size.width - self.barSpacing)/(CGFloat)self.numberOfPoints);
CGFloat barWidth = width - self.barSpacing;
if (width <= 0) width = 1;
if (barWidth <= 0) barWidth = 1;
for (int i = 0; i < self.numberOfPoints; i++) {
CALayer *layer = [[CALayer alloc] init];
layer.cornerRadius = self.barCornerRadius;
layer.frame = CGRectMake(i*width + self.barSpacing, 0, barWidth, self.frame.size.height);
if (self.waveColor) {
layer.backgroundColor = self.waveColor.CGColor;
}
[self.layer addSublayer:layer];
}
cachedNumberOfPoints = self.numberOfPoints;
}
-(void)updateWaveColor:(UIColor *)waveColor subwaveColor:(UIColor *)subwaveColor{
self.waveColor = waveColor;
for (CALayer *layer in [self.layer sublayers]) {
layer.backgroundColor = waveColor.CGColor;
}
}
- (void)redraw{
[super redraw];
if (cachedNumberOfPoints != self.numberOfPoints) {
[self resetWaveLayers];
}
int i = 0;
CGFloat width = ((self.frame.size.width - self.barSpacing)/(CGFloat)self.numberOfPoints);
CGFloat barWidth = width - self.barSpacing;
if (width <= 0) width = 1;
if (barWidth <= 0) barWidth = 1;
for (CALayer *layer in [self.layer sublayers]) {
if (!layer) continue;
if (isnan(self.points[i].y)) self.points[i].y = 0;
CGFloat barHeight = self.frame.size.height-self.points[i].y;
if (barHeight <= 0) barHeight = 1;
layer.frame = CGRectMake(i*width + self.barSpacing, self.points[i].y, barWidth, barHeight);
i++;
}
}
@end