LCWechat -- 自定義錄音指示器

錄音指示器 案例一:

1.png
1..h文件

typedef NS_ENUM(NSInteger, subTitleStatues) {
    subTitleStatuesDefault,
    subTitleStatuesCancel,
};

@interface LCRecordingView : UIView
// 根據(jù) 狀態(tài)更改 顯示的內(nèi)容
+ (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 = @"錄音倒計(jì)時(shí)";
        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 = @"松開發(fā)送語音";
        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 = @"松開發(fā)送語音";
            break;
        case subTitleStatuesCancel:
            self.subTitleLabel.text = @"松開手指,取消發(fā)送語音";
            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

如何使用:

錄音按鈕的幾種狀態(tài)
[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.結(jié)束錄音
- (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. 移動(dòng)回按鈕范圍
- (void)dragEnterRecord:(UIButton *)recordButton
{
     [LCRecordingView subTitleLabelStatues:subTitleStatuesCancel];
}

錄音指示器 案例二:

2.png
思路大致都一樣, 只是多了一個(gè)監(jiān)聽音量的變化
- (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) {
        // 自動(dòng)發(fā)送
        [[NSNotificationCenter defaultCenter] postNotificationName:@"recordDUrationToolong" object:nil];
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,022評(píng)論 4 61
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,716評(píng)論 25 709
  • 在曉婧的婚禮上碰到李博昊,插科打諢的談?wù)撈鹞覀兌几惺艿降奈⑽⑵痤^的經(jīng)濟(jì)危機(jī),末了我忍不住的夸贊李老板更帥了,我用的...
    明珠一顆孫小美閱讀 1,695評(píng)論 4 20
  • 摩羯座女子的通病大概就是 ,骨子里面很清高,多愁善感,表面堅(jiān)強(qiáng),內(nèi)心敏感 ,有著不依靠任何人的倔強(qiáng),偶爾又像孩子一...
    游離的村姑閱讀 532評(píng)論 0 0
  • 感恩今天所有的心想都事成了。
    心靈國(guó)度閱讀 141評(píng)論 0 0

友情鏈接更多精彩內(nèi)容