-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathAudioRecordView.m
181 lines (145 loc) · 5.67 KB
/
AudioRecordView.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
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
//
// AudioRecordView.m
// audiodemo
//
// Created by sumeng on 7/30/15.
// Copyright (c) 2015 sumeng. All rights reserved.
//
#import "AudioRecordView.h"
#import "AudioManager.h"
#import <QuartzCore/QuartzCore.h>
@interface AudioRecordView () <AudioManagerDelegate>
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, assign) AudioRecordViewTouchState touchState;
@property (nonatomic, strong) UIView *recordBgView;
@property (nonatomic, strong) UIView *spreadView;
@property (nonatomic, strong) UIView *flashView;
@end
@implementation AudioRecordView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
_isRecording = NO;
_spreadView = [[UIView alloc] initWithFrame:CGRectMake(-8, -8, self.frame.size.width+16, self.frame.size.height+16)];
_spreadView.backgroundColor = [UIColor colorWithRGBHex:0xC6ECFD];
_spreadView.layer.cornerRadius = _spreadView.frame.size.width/2;
_spreadView.alpha = 0;
[self addSubview:_spreadView];
_recordBgView = [[UIView alloc] initWithFrame:CGRectMake(-8, -8, self.frame.size.width+16, self.frame.size.height+16)];
_recordBgView.backgroundColor = [UIColor colorWithRGBHex:0x7ACFFB];
_recordBgView.layer.cornerRadius = _recordBgView.frame.size.width/2;
_recordBgView.hidden = YES;
[self addSubview:_recordBgView];
_imageView = [[UIImageView alloc] initWithFrame:self.bounds];
_imageView.contentMode = UIViewContentModeScaleAspectFill;
_imageView.image = [UIImage imageNamed:@"keyboard_voice_record"];
[self addSubview:_imageView];
_flashView = [[UIView alloc] initWithFrame:self.bounds];
_flashView.backgroundColor = [UIColor whiteColor];
_flashView.layer.cornerRadius = _flashView.frame.size.width/2;
_flashView.alpha = 0;
[self addSubview:_flashView];
[self addTarget:self action:@selector(onTouchDown:) forControlEvents:UIControlEventTouchDown];
[self addTarget:self action:@selector(onTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
[self addTarget:self action:@selector(onTouchUpOutside:) forControlEvents:UIControlEventTouchUpOutside];
}
return self;
}
- (void)dealloc {
[self stop];
}
- (void)record {
[self stop];
[[AudioManager shared] stopPlay];
[AudioManager shared].delegate = self;
[[AudioManager shared] record];
}
- (void)stop {
_isRecording = NO;
[self stopAnimation];
[[AudioManager shared] stopRecord];
}
- (void)onTouchDown:(id)sender {
[self record];
}
- (void)onTouchUpInside:(id)sender {
[self stop];
}
- (void)onTouchUpOutside:(id)sender {
[self stop];
}
#pragma mark - touch
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
_touchState = AudioRecordViewTouchStateInside;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
UITouch *touch = [touches anyObject];
BOOL touchInside = [self pointInside:[touch locationInView:self] withEvent:nil];
BOOL touchStateChanged = NO;
if (_touchState == AudioRecordViewTouchStateInside && !touchInside) {
_touchState = AudioRecordViewTouchStateOutside;
touchStateChanged = YES;
}
else if (_touchState == AudioRecordViewTouchStateOutside && touchInside) {
_touchState = AudioRecordViewTouchStateInside;
touchStateChanged = YES;
}
if (touchStateChanged) {
if (_delegate && [_delegate respondsToSelector:@selector(recordView:touchStateChanged:)]) {
[_delegate recordView:self touchStateChanged:_touchState];
}
}
}
#pragma mark - Animation
- (void)startAnimation {
_recordBgView.hidden = NO;
_spreadView.alpha = 1.0f;
_spreadView.transform = CGAffineTransformMakeScale(1.0f, 1.0f);
_flashView.alpha = 0.4f;
[UIView beginAnimations:@"RecordAnimation" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:1.5f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationRepeatCount:FLT_MAX];
_flashView.alpha = 0;
_spreadView.transform = CGAffineTransformMakeScale(1.5f, 1.5f);
_spreadView.alpha = 0;
[UIView commitAnimations];
}
- (void)stopAnimation {
[_flashView.layer removeAllAnimations];
[_spreadView.layer removeAllAnimations];
_recordBgView.hidden = YES;
_spreadView.alpha = 0;
_flashView.alpha = 0;
}
#pragma mark - AudioManagerDelegate
- (void)didAudioRecordStarted:(AudioManager *)am {
_isRecording = YES;
[self startAnimation];
if (_delegate && [_delegate respondsToSelector:@selector(recordViewRecordStarted:)]) {
[_delegate recordViewRecordStarted:self];
}
}
- (void)didAudioRecording:(AudioManager *)am volume:(double)volume {
if (_delegate && [_delegate respondsToSelector:@selector(recordView:volume:)]) {
[_delegate recordView:self volume:volume];
}
}
- (void)didAudioRecordStoped:(AudioManager *)am file:(NSString *)file duration:(NSTimeInterval)duration successfully:(BOOL)successfully {
_isRecording = NO;
[self stop];
if (_delegate && [_delegate respondsToSelector:@selector(recordViewRecordFinished:file:duration:)]) {
[_delegate recordViewRecordFinished:self file:file duration:duration];
}
}
- (void)didAudioRecord:(AudioManager *)am err:(NSError *)err {
_isRecording = NO;
[self stop];
if (_delegate && [_delegate respondsToSelector:@selector(recordViewRecord:err:)]) {
[_delegate recordViewRecord:self err:err];
}
}
@end