-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathAudioVolumeView.m
73 lines (60 loc) · 2.32 KB
/
AudioVolumeView.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
//
// AudioVolumeView.m
// Coding_iOS
//
// Created by sumeng on 8/2/15.
// Copyright (c) 2015 Coding. All rights reserved.
//
#import "AudioVolumeView.h"
@interface AudioVolumeView ()
@property (nonatomic, strong) NSMutableArray *volumeViews;
@property (nonatomic, strong) NSMutableArray *volumes;
@end
@implementation AudioVolumeView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
_volumes = [[NSMutableArray alloc] initWithCapacity:kAudioVolumeViewVolumeNumber];
_volumeViews = [[NSMutableArray alloc] initWithCapacity:kAudioVolumeViewVolumeNumber];
for (int i = 0; i < kAudioVolumeViewVolumeNumber; i++) {
[_volumes addObject:@0];
UIView *volumeView = [[UIView alloc] initWithFrame:CGRectMake((kAudioVolumeViewVolumeWidth+kAudioVolumeViewVolumePadding)*i, (self.frame.size.height-kAudioVolumeViewVolumeMinHeight)/2, kAudioVolumeViewVolumeWidth, kAudioVolumeViewVolumeMinHeight)];
volumeView.backgroundColor = [UIColor colorWithRGBHex:0xfb8638];
volumeView.layer.cornerRadius = volumeView.frame.size.width/2;
[self addSubview:volumeView];
[_volumeViews addObject:volumeView];
}
self.type = AudioVolumeViewTypeLeft;
}
return self;
}
- (void)addVolume:(double)volume {
if (_type == AudioVolumeViewTypeRight) {
[_volumes removeLastObject];
[_volumes insertObject:[NSNumber numberWithDouble:volume] atIndex:0];
}
else {
[_volumes removeObjectAtIndex:0];
[_volumes addObject:[NSNumber numberWithDouble:volume]];
}
[self layoutVolumes];
}
- (void)clearVolume {
[_volumes removeAllObjects];
for (int i = 0; i < _volumeViews.count; i++) {
[_volumes addObject:@0];
}
[self layoutVolumes];
}
- (void)layoutVolumes {
for (int i = 0; i < _volumeViews.count; i++) {
UIView *volumeView = _volumeViews[i];
NSNumber *volume = _volumes[i];
[volumeView setHeight:[self heightOfVolume:volume.doubleValue]];
volumeView.center = CGPointMake(volumeView.center.x, self.frame.size.height/2);
}
}
- (CGFloat)heightOfVolume:(double)volume {
return kAudioVolumeViewVolumeMinHeight + (kAudioVolumeViewVolumeMaxHeight - kAudioVolumeViewVolumeMinHeight) * volume;
}
@end