录音指示器 案例一:
1.png1..h文件
typedef NS_ENUM(NSInteger, subTitleStatues) {
subTitleStatuesDefault,
subTitleStatuesCancel,
};
@interface LCRecordingView : UIView
// 根据 状态更改 显示的内容
+ (void)subTitleLabelStatues:(subTitleStatues)statues;
+ (void)show;
+ (void)dismiss;
@end
2..m文件
@interface LCRecordingView ()
@property (assign, nonatomic) CGFloat angle;
@property (weak, nonatomic) UIImageView *bordImageView;
@property (weak, nonatomic) UILabel *centerLabel;
@property (weak, nonatomic) UILabel *titleLabel;
@property (weak, nonatomic) UILabel *subTitleLabel;
@property (assign, nonatomic) NSTimeInterval seconds;
@end
@implementation LCRecordingView
static LCRecordingView *recordingView;
static NSTimer *timer;
#pragma mark - lazy
- (UIImageView *)bordImageView
{
if (!_bordImageView) {
UIImageView *imageV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"chat_bar_record_circle"]];
_bordImageView = imageV;
[self addSubview:_bordImageView];
}
return _bordImageView;
}
- (UILabel *)centerLabel
{
if (!_centerLabel) {
UILabel *label = [[UILabel alloc] init];
label.text = @"60";
label.font = [UIFont systemFontOfSize:28];
label.textColor = [UIColor yellowColor];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = NSTextAlignmentCenter;
_centerLabel = label;
[self addSubview:_centerLabel];
}
return _centerLabel;
}
- (UILabel *)titleLabel
{
if (!_titleLabel) {
UILabel *label = [[UILabel alloc] init];
label.text = @"录音倒计时";
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.font = [UIFont systemFontOfSize:16];
_titleLabel = label;
[self addSubview:_titleLabel];
}
return _titleLabel;
}
- (UILabel *)subTitleLabel
{
if (!_subTitleLabel) {
UILabel *label = [[UILabel alloc] init];
label.text = @"松开发送语音";
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.font = [UIFont systemFontOfSize:14];
_subTitleLabel = label;
[self addSubview:_subTitleLabel];
}
return _subTitleLabel;
}
#pragma mark - init
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
self.frame = [UIScreen mainScreen].bounds;
self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.4];
[self bordImageView];
[self centerLabel];
[self subTitleLabel];
[self titleLabel];
timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
}
return self;
}
+ (void)subTitleLabelStatues:(subTitleStatues)statues
{
[recordingView subTitleLabelStatues:statues];
}
- (void)subTitleLabelStatues:(subTitleStatues)statues
{
switch (statues) {
case subTitleStatuesDefault:
self.subTitleLabel.text = @"松开发送语音";
break;
case subTitleStatuesCancel:
self.subTitleLabel.text = @"松开手指,取消发送语音";
break;
default:
break;
}
}
- (void)timerAction
{
self.angle -= 3;
self.seconds ++ ;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.09];
UIView.AnimationRepeatAutoreverses = YES;
self.bordImageView.transform = CGAffineTransformMakeRotation(self.angle * (M_PI / 180.0f));
float second = [self.centerLabel.text floatValue];
if (second <= 50.0f) {
self.centerLabel.textColor = [UIColor redColor];
}else{
self.centerLabel.textColor = [UIColor yellowColor];
}
self.centerLabel.text = [NSString stringWithFormat:@"%.1fs",second-0.1];
[UIView commitAnimations];
if (second <= 0.1) {
[self.class dismiss];
[[NSNotificationCenter defaultCenter] postNotificationName:@"recordDUrationToolong" object:nil];
}
}
+ (void)show
{
LCRecordingView *rcView = [[LCRecordingView alloc] init];
UIWindow *window = [UIApplication sharedApplication].windows.lastObject;
recordingView = rcView;
[window addSubview:recordingView];
}
+ (void)dismiss
{
[timer invalidate];
timer = nil;
[recordingView removeFromSuperview];
}
- (void)layoutSubviews
{
[super layoutSubviews];
[self.bordImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self);
}];
[self.centerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.equalTo(@(150));
make.height.equalTo(@(40));
make.center.equalTo(self);
}];
[self.subTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.equalTo(self.centerLabel);
make.height.equalTo(@(20));
make.centerX.equalTo(self);
make.centerY.equalTo(self).offset(50);
}];
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.equalTo(self.subTitleLabel);
make.centerX.equalTo(self);
make.centerY.equalTo(self).offset(-50);
}];
}
@end
如何使用:
录音按钮的几种状态
[recordButton addTarget:self action:@selector(startRecord:) forControlEvents:UIControlEventTouchDown];
[recordButton addTarget:self action:@selector(endRecord:) forControlEvents:UIControlEventTouchUpInside];
[recordButton addTarget:self action:@selector(dragExitRecord:) forControlEvents:UIControlEventTouchDragExit];
[recordButton addTarget:self action:@selector(cancelRecord:) forControlEvents:UIControlEventTouchUpOutside];
[recordButton addTarget:self action:@selector(dragEnterRecord:) forControlEvents:UIControlEventTouchDragEnter];
// 1.开始录音
- (void)startRecord:(UIButton *)recordButton
{
[LCRecordingView show];
[LCRecordingView subTitleLabelStatues:subTitleStatuesDefault];
}
// 2.结束录音
- (void)endRecord:(UIButton *)recordButton
{
[LCRecordingView dismiss];
[LCRecordingView subTitleLabelStatues:subTitleStatuesDefault];
}
// 3.取消录音
- (void)cancelRecord:(UIButton *)recordButton
{
[LCRecordingView dismiss];
[LCRecordingView subTitleLabelStatues:subTitleStatuesDefault];
}
//4. 离开按钮范围
- (void)dragExitRecord:(UIButton *)recordButton
{
[LCRecordingView subTitleLabelStatues:subTitleStatuesCancel];
}
//5. 移动回按钮范围
- (void)dragEnterRecord:(UIButton *)recordButton
{
[LCRecordingView subTitleLabelStatues:subTitleStatuesCancel];
}
录音指示器 案例二:
2.png思路大致都一样, 只是多了一个监听音量的变化
- (void)updateMeters{
if ([TYHAudioRecorderUtil recorder]) {
[[TYHAudioRecorderUtil recorder] updateMeters];
}
float peakPower = [[TYHAudioRecorderUtil recorder] averagePowerForChannel:0];
double ALPHA = 0.05;
double peakPowerForChannel = pow(10, (ALPHA * peakPower));
self.indicatorView.value = peakPowerForChannel;
if (self.indicatorView.phase == AudioRecordPhaseCancelling) {
return;
}
self.indicatorView.phase = AudioRecordPhaseRecording;
self.duration ++;
if (self.duration * 0.1 > 50.0) {
[self timeLabel];
self.timeLabel.text = [NSString stringWithFormat:@"%.f", self.timeLabelText-- * 0.1];
// self.angle -= 3;
// [UIView beginAnimations:nil context:nil];
// [UIView setAnimationDuration:0.09];
// UIView.AnimationRepeatAutoreverses = YES;
// self.timeLabel.transform = CGAffineTransformMakeRotation(self.angle * (M_PI / 180.0f));
// [UIView commitAnimations];
}
if (self.duration * 0.1 > 60.0) {
// 自动发送
[[NSNotificationCenter defaultCenter] postNotificationName:@"recordDUrationToolong" object:nil];
}
}
网友评论