Skip to content

Commit 9b33cfc

Browse files
sumengsumeng
authored andcommitted
增加录音动画
1 parent 1b0efbf commit 9b33cfc

4 files changed

Lines changed: 79 additions & 44 deletions

File tree

Coding_iOS/Util/Audio/AudioAmrUtil.m

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,8 @@ + (NSString *)decodeAmrToWave:(NSString *)amrFile {
4343
if ([[NSFileManager defaultManager] fileExistsAtPath:waveFile]) {
4444
[[NSFileManager defaultManager] removeItemAtPath:waveFile error:nil];
4545
}
46-
NSTimeInterval date1 = [[NSDate date] timeIntervalSince1970];
4746
NSData *waveData = DecodeAMRToWAVE([NSData dataWithContentsOfFile:amrFile]);
4847
[waveData writeToFile:waveFile atomically:YES];
49-
NSTimeInterval date2 = [[NSDate date] timeIntervalSince1970];
50-
NSLog(@"decodeAmrToWave cost:%fs", date2-date1);
5148
return waveFile;
5249
}
5350

Coding_iOS/Views/AudioView/AudioRecordView.m

Lines changed: 76 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,49 +8,62 @@
88

99
#import "AudioRecordView.h"
1010
#import "AudioManager.h"
11+
#import <QuartzCore/QuartzCore.h>
1112

1213
@interface AudioRecordView () <AudioManagerDelegate>
1314

1415
@property (nonatomic, strong) UIImageView *imageView;
1516
@property (nonatomic, assign) AudioRecordViewTouchState touchState;
1617

18+
@property (nonatomic, strong) UIView *recordBgView;
19+
@property (nonatomic, strong) UIView *spreadView;
20+
@property (nonatomic, strong) UIView *flashView;
21+
1722
@end
1823

1924
@implementation AudioRecordView
2025

21-
- (instancetype)init {
22-
self = [super init];
23-
if (self) {
24-
[self initAudioRecordView];
25-
}
26-
return self;
27-
}
28-
29-
- (instancetype)initWithCoder:(NSCoder *)coder {
30-
self = [super initWithCoder:coder];
31-
if (self) {
32-
[self initAudioRecordView];
33-
}
34-
return self;
35-
}
36-
3726
- (instancetype)initWithFrame:(CGRect)frame {
3827
self = [super initWithFrame:frame];
3928
if (self) {
40-
[self initAudioRecordView];
29+
_isRecording = NO;
30+
31+
_recordBgView = [[UIView alloc] initWithFrame:CGRectMake(-15, -15, self.frame.size.width+30, self.frame.size.height+30)];
32+
_recordBgView.backgroundColor = [UIColor colorWithRed:0.8f green:0.8f blue:0.8f alpha:0.1f];
33+
_recordBgView.layer.cornerRadius = _recordBgView.frame.size.width/2;
34+
_recordBgView.layer.borderColor = [[UIColor colorWithRGBHex:0xdddddd] CGColor];
35+
_recordBgView.layer.borderWidth = 1;
36+
_recordBgView.hidden = YES;
37+
[self addSubview:_recordBgView];
38+
39+
_spreadView = [[UIView alloc] initWithFrame:_recordBgView.frame];
40+
_spreadView.layer.cornerRadius = _recordBgView.frame.size.width/2;
41+
_spreadView.layer.borderColor = [[UIColor colorWithRGBHex:0xdddddd] CGColor];
42+
_spreadView.layer.borderWidth = 1;
43+
_spreadView.alpha = 0;
44+
[self addSubview:_spreadView];
45+
46+
_imageView = [[UIImageView alloc] initWithFrame:self.bounds];
47+
_imageView.backgroundColor = [UIColor colorWithRGBHex:0x2faeea];
48+
_imageView.layer.cornerRadius = self.frame.size.width/2;
49+
_imageView.contentMode = UIViewContentModeCenter;
50+
[self addSubview:_imageView];
51+
52+
_flashView = [[UIView alloc] initWithFrame:self.bounds];
53+
_flashView.backgroundColor = [UIColor whiteColor];
54+
_flashView.layer.cornerRadius = _flashView.frame.size.width/2;
55+
_flashView.alpha = 0;
56+
[self addSubview:_flashView];
57+
58+
[self addTarget:self action:@selector(onTouchDown:) forControlEvents:UIControlEventTouchDown];
59+
[self addTarget:self action:@selector(onTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
60+
[self addTarget:self action:@selector(onTouchUpOutside:) forControlEvents:UIControlEventTouchUpOutside];
4161
}
4262
return self;
4363
}
4464

45-
- (void)initAudioRecordView {
46-
self.backgroundColor = [UIColor colorWithRGBHex:0x2faeea];
47-
self.layer.cornerRadius = self.frame.size.width/2;
48-
49-
_isRecording = NO;
50-
51-
[self addTarget:self action:@selector(onTouchDown:) forControlEvents:UIControlEventTouchDown];
52-
[self addTarget:self action:@selector(onTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
53-
[self addTarget:self action:@selector(onTouchUpOutside:) forControlEvents:UIControlEventTouchUpOutside];
65+
- (void)dealloc {
66+
[self stop];
5467
}
5568

5669
- (void)record {
@@ -60,26 +73,29 @@ - (void)record {
6073
[AudioManager shared].delegate = self;
6174
[[AudioManager shared] recordWithValidator:_validator];
6275

76+
[self startAnimation];
77+
6378
if (_delegate && [_delegate respondsToSelector:@selector(recordViewRecordStarted:)]) {
6479
[_delegate recordViewRecordStarted:self];
6580
}
6681
}
6782

6883
- (void)stop {
6984
_isRecording = NO;
70-
[[AudioManager shared] stopPlay];
85+
[self stopAnimation];
86+
[[AudioManager shared] stopRecord];
7187
}
7288

7389
- (void)onTouchDown:(id)sender {
7490
[self record];
7591
}
7692

7793
- (void)onTouchUpInside:(id)sender {
78-
[[AudioManager shared] stopRecord];
94+
[self stop];
7995
}
8096

8197
- (void)onTouchUpOutside:(id)sender {
82-
[[AudioManager shared] stopRecord];
98+
[self stop];
8399
}
84100

85101
#pragma mark - touch
@@ -109,6 +125,36 @@ - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
109125
}
110126
}
111127

128+
#pragma mark - Animation
129+
130+
- (void)startAnimation {
131+
_recordBgView.hidden = NO;
132+
_spreadView.alpha = 1.0f;
133+
_spreadView.transform = CGAffineTransformMakeScale(1.0f, 1.0f);
134+
_flashView.alpha = 0.6f;
135+
136+
[UIView beginAnimations:@"RecordAnimation" context:nil];
137+
[UIView setAnimationDelegate:self];
138+
[UIView setAnimationDuration:2.0f];
139+
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
140+
[UIView setAnimationRepeatCount:FLT_MAX];
141+
142+
_flashView.alpha = 0;
143+
_spreadView.transform = CGAffineTransformMakeScale(1.5f, 1.5f);
144+
_spreadView.alpha = 0;
145+
146+
[UIView commitAnimations];
147+
}
148+
149+
- (void)stopAnimation {
150+
[_flashView.layer removeAllAnimations];
151+
[_spreadView.layer removeAllAnimations];
152+
153+
_recordBgView.hidden = YES;
154+
_spreadView.alpha = 0;
155+
_flashView.alpha = 0;
156+
}
157+
112158
#pragma mark - AudioManagerDelegate
113159

114160
- (void)didAudioRecordStarted:(AudioManager *)am {
@@ -122,7 +168,7 @@ - (void)didAudioRecording:(AudioManager *)am volume:(double)volume {
122168
}
123169

124170
- (void)didAudioRecordStoped:(AudioManager *)am file:(NSString *)file duration:(NSTimeInterval)duration successfully:(BOOL)successfully {
125-
_isRecording = NO;
171+
NSLog(@"didAudioRecordStoped");
126172
if (_delegate && [_delegate respondsToSelector:@selector(recordViewRecordFinished:file:duration:)]) {
127173
[_delegate recordViewRecordFinished:self file:file duration:duration];
128174
}

Coding_iOS/Views/AudioView/AudioVolumeView.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
#define kAudioVolumeViewVolumeWidth 2.0f
1212
#define kAudioVolumeViewVolumeMinHeight 3.0f
13-
#define kAudioVolumeViewVolumeMaxHeight 12.0f
13+
#define kAudioVolumeViewVolumeMaxHeight 16.0f
1414
#define kAudioVolumeViewVolumePadding 2.0f
1515
#define kAudioVolumeViewVolumeNumber 10
1616

Coding_iOS/Views/UIMessageInputView/UIMessageInputView_Voice.m

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
#import "UIMessageInputView_Voice.h"
1010
#import "AudioRecordView.h"
11-
#import "AudioPlayView.h"
1211
#import "AudioVolumeView.h"
1312

1413
typedef NS_ENUM(NSInteger, UIMessageInputView_VoiceState) {
@@ -26,7 +25,6 @@ @interface UIMessageInputView_Voice () <AudioRecordViewDelegate>
2625
@property (assign, nonatomic) UIMessageInputView_VoiceState state;
2726
@property (assign, nonatomic) int duration;
2827
@property (strong, nonatomic) NSTimer *timer;
29-
@property (strong, nonatomic) AudioPlayView *playView;
3028

3129
@end
3230

@@ -55,10 +53,6 @@ - (id)initWithFrame:(CGRect)frame {
5553
_recordView.delegate = self;
5654
[self addSubview:_recordView];
5755

58-
_playView = [[AudioPlayView alloc] initWithFrame:CGRectMake(10, 10, 50, 50)];
59-
_playView.backgroundColor = [UIColor greenColor];
60-
[self addSubview:_playView];
61-
6256
UILabel *tipLabel = [[UILabel alloc] init];
6357
tipLabel.font = [UIFont systemFontOfSize:12];
6458
tipLabel.textColor = [UIColor colorWithRGBHex:0x999999];
@@ -103,9 +97,9 @@ - (void)setState:(UIMessageInputView_VoiceState)state {
10397
_recordTipsLabel.center = CGPointMake(self.frame.size.width/2, 20);
10498

10599
if (state == UIMessageInputView_VoiceStateRecording) {
106-
_volumeLeftView.center = CGPointMake(_recordTipsLabel.frame.origin.x - _volumeLeftView.frame.size.width/2 - 10, _recordTipsLabel.center.y);
100+
_volumeLeftView.center = CGPointMake(_recordTipsLabel.frame.origin.x - _volumeLeftView.frame.size.width/2 - 12, _recordTipsLabel.center.y);
107101
_volumeLeftView.hidden = NO;
108-
_volumeRightView.center = CGPointMake(_recordTipsLabel.frame.origin.x + _recordTipsLabel.frame.size.width + _recordTipsLabel.frame.size.width/2 + 10, _recordTipsLabel.center.y);
102+
_volumeRightView.center = CGPointMake(_recordTipsLabel.frame.origin.x + _recordTipsLabel.frame.size.width + _recordTipsLabel.frame.size.width/2 + 12, _recordTipsLabel.center.y);
109103
_volumeRightView.hidden = NO;
110104
}
111105
}
@@ -154,8 +148,6 @@ - (void)recordViewRecordFinished:(AudioRecordView *)recordView file:(NSString *)
154148
}
155149
self.state = UIMessageInputView_VoiceStateReady;
156150
_duration = 0;
157-
158-
_playView.url = [NSURL fileURLWithPath:file];
159151
}
160152

161153
- (void)recordView:(AudioRecordView *)recordView touchStateChanged:(AudioRecordViewTouchState)touchState {

0 commit comments

Comments
 (0)