// // AudioShowView.m // smartRhino // // Created by niuzhen on 2019/12/4. // Copyright © 2019 tederen. All rights reserved. // #import "AudioShowView.h" #define LABELTAG 1000 @interface AudioShowView() @property (nonatomic, strong)UIView * bgView; @property (nonatomic, strong)UIImageView * imagV; @property (nonatomic, strong)UIImageView * soundimagV; @property (nonatomic, strong)UILabel * soundL; @property (nonatomic, strong)UILabel * sound_1; @property (nonatomic, strong)UILabel * sound_2; @property (nonatomic, strong)UILabel * sound_3; @property (nonatomic, strong)UILabel * sound_4; @property (nonatomic, strong)UILabel * sound_5; @property (nonatomic, strong)UILabel * sound_6; @property (nonatomic, strong)UILabel * sound_7; @property (nonatomic, strong)UILabel * sound_8; @property (nonatomic, strong)NSTimer * timer; @end @implementation AudioShowView TDShareInstance_implementation(AudioShowView) - (instancetype)init { self = [super init]; if (self) { self.frame = CGRectMake((SCREEN_WIDTH - 192) * 0.5, (SCREEN_HEIGHT - 192) * 0.5 - 15, 192, 192); [self setCusView]; } return self; } - (void)setCusView { [self layoutIfNeeded]; [self addSubview:self.bgView]; [self.bgView addSubview:self.imagV]; [self.bgView addSubview:self.soundimagV]; [self.bgView addSubview:self.soundL]; [self.soundimagV addSubview:self.sound_1]; [self.soundimagV addSubview:self.sound_2]; [self.soundimagV addSubview:self.sound_3]; [self.soundimagV addSubview:self.sound_4]; [self.soundimagV addSubview:self.sound_5]; [self.soundimagV addSubview:self.sound_6]; [self.soundimagV addSubview:self.sound_7]; [self.soundimagV addSubview:self.sound_8]; NSTextAttachment *attach = [[NSTextAttachment alloc] init]; attach.image = [UIImage imageNamed:@"audio_quxiao"]; attach.bounds = CGRectMake(0, 0, 11.5, 11); NSAttributedString *attachString = [NSAttributedString attributedStringWithAttachment:attach]; NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithAttributedString:attachString]; [string appendAttributedString:[[NSAttributedString alloc] initWithString:@"上划取消"]]; [string addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, string.length)]; self.soundL.attributedText = string; [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.mas_offset(UIEdgeInsetsZero); }]; [self.imagV mas_makeConstraints:^(MASConstraintMaker *make) { make.left.offset(52.5); make.top.offset(53); make.size.mas_offset(CGSizeMake(47, 67)); }]; [self.soundimagV mas_makeConstraints:^(MASConstraintMaker *make) { make.right.offset(-36); make.top.offset(60); make.size.mas_offset(CGSizeMake(40, 60)); }]; [self.soundL mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.offset(-17.5); make.centerX.equalTo(self.bgView); make.height.offset(20); make.width.mas_lessThanOrEqualTo(190); }]; CGFloat W = 5.f; CGFloat H = 4.f; [self.sound_1 mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.soundimagV); make.size.mas_offset(CGSizeMake(W, H)); make.bottom.mas_equalTo(self.soundimagV.mas_bottom).offset(0); }]; [self.sound_2 mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.soundimagV); make.size.mas_offset(CGSizeMake(W * 2, 4)); make.bottom.mas_equalTo(self.soundimagV.mas_bottom).offset(-H * 2); }]; [self.sound_3 mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.soundimagV); make.size.mas_offset(CGSizeMake(W * 3, 4)); make.bottom.mas_equalTo(self.soundimagV.mas_bottom).offset(-H * 4); }]; [self.sound_4 mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.soundimagV); make.size.mas_offset(CGSizeMake(W * 4, 4)); make.bottom.mas_equalTo(self.soundimagV.mas_bottom).offset(-H * 6); }]; [self.sound_5 mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.soundimagV); make.size.mas_offset(CGSizeMake(W * 5, 4)); make.bottom.mas_equalTo(self.soundimagV.mas_bottom).offset(-H * 8); }]; [self.sound_6 mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.soundimagV); make.size.mas_offset(CGSizeMake(W * 6, 4)); make.bottom.mas_equalTo(self.soundimagV.mas_bottom).offset(-H * 10); }]; [self.sound_7 mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.soundimagV); make.size.mas_offset(CGSizeMake(W * 7, 4)); make.bottom.mas_equalTo(self.soundimagV.mas_bottom).offset(-H * 12); }]; [self.sound_8 mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.soundimagV); make.size.mas_offset(CGSizeMake(W * 8, 4)); make.bottom.mas_equalTo(self.soundimagV.mas_bottom).offset(-H * 14); }]; } - (void)show { self.timer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(changeImage) userInfo:nil repeats:YES]; [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode]; [[UIApplication sharedApplication].keyWindow addSubview:self]; [UIView animateWithDuration:0.4 animations:^{ } completion:^(BOOL finished) {}]; } - (void)dismiss { [self.timer invalidate]; self.timer = nil; WS(weakSelf); [UIView animateWithDuration:0.4 animations:^{ } completion:^(BOOL finished) { [weakSelf removeFromSuperview]; }]; } - (UIView *)bgView { if (!_bgView) { _bgView = [UIView new]; _bgView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"audio_Bg"]]; } return _bgView; } - (UIImageView *)imagV { if (!_imagV) { _imagV = [UIImageView new]; _imagV.image = [UIImage imageNamed:@"audio_huatong"]; } return _imagV; } - (UIImageView *)soundimagV { if (!_soundimagV) { _soundimagV = [UIImageView new]; _soundimagV.image = [UIImage imageNamed:@"audio_show"]; } return _soundimagV; } - (UILabel *)soundL { if (!_soundL) { _soundL = [UILabel new]; } return _soundL; } - (UILabel *)sound_1 { if (!_sound_1) { _sound_1 = [UILabel new]; _sound_1.tag = LABELTAG + 1; } return _sound_1; } - (UILabel *)sound_2 { if (!_sound_2) { _sound_2 = [UILabel new]; _sound_2.tag = LABELTAG + 2; } return _sound_2; } - (UILabel *)sound_3 { if (!_sound_3) { _sound_3 = [UILabel new]; _sound_3.tag = LABELTAG + 3; } return _sound_3; } - (UILabel *)sound_4 { if (!_sound_4) { _sound_4 = [UILabel new]; _sound_4.tag = LABELTAG + 4; } return _sound_4; } - (UILabel *)sound_5 { if (!_sound_5) { _sound_5 = [UILabel new]; _sound_5.tag = LABELTAG + 5; } return _sound_5; } - (UILabel *)sound_6 { if (!_sound_6) { _sound_6 = [UILabel new]; _sound_6.tag = LABELTAG + 6; } return _sound_6; } - (UILabel *)sound_7 { if (!_sound_7) { _sound_7 = [UILabel new]; _sound_7.tag = LABELTAG + 7; } return _sound_7; } - (UILabel *)sound_8 { if (!_sound_8) { _sound_8 = [UILabel new]; _sound_8.tag = LABELTAG + 8; } return _sound_8; } - (void)changeImage { [self.recorder updateMeters];//更新测量值 float avg = [self.recorder averagePowerForChannel:0]; float minValue = -60; float range = 60; float outRange = 100; if (avg < minValue) { avg = minValue; } float decibels = (avg + range) / range * outRange; NSLog(@"%f",decibels/100); NSUInteger level = decibels/10; NSLog(@"%luld",(unsigned long)level); for (NSUInteger i = 1; i <= level; i ++) { UILabel * label = (UILabel *)[self.soundimagV viewWithTag:LABELTAG + i]; label.backgroundColor = [UIColor whiteColor]; } for (NSUInteger i = level; i <= 8; i ++) { UILabel * label = (UILabel *)[self.soundimagV viewWithTag:LABELTAG + i]; label.backgroundColor = [UIColor clearColor]; } } @end