AudioShowView.m 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. //
  2. // AudioShowView.m
  3. // smartRhino
  4. //
  5. // Created by niuzhen on 2019/12/4.
  6. // Copyright © 2019 tederen. All rights reserved.
  7. //
  8. #import "AudioShowView.h"
  9. #define LABELTAG 1000
  10. @interface AudioShowView()
  11. @property (nonatomic, strong)UIView * bgView;
  12. @property (nonatomic, strong)UIImageView * imagV;
  13. @property (nonatomic, strong)UIImageView * soundimagV;
  14. @property (nonatomic, strong)UILabel * soundL;
  15. @property (nonatomic, strong)UILabel * sound_1;
  16. @property (nonatomic, strong)UILabel * sound_2;
  17. @property (nonatomic, strong)UILabel * sound_3;
  18. @property (nonatomic, strong)UILabel * sound_4;
  19. @property (nonatomic, strong)UILabel * sound_5;
  20. @property (nonatomic, strong)UILabel * sound_6;
  21. @property (nonatomic, strong)UILabel * sound_7;
  22. @property (nonatomic, strong)UILabel * sound_8;
  23. @property (nonatomic, strong)NSTimer * timer;
  24. @end
  25. @implementation AudioShowView
  26. TDShareInstance_implementation(AudioShowView)
  27. - (instancetype)init
  28. {
  29. self = [super init];
  30. if (self) {
  31. self.frame = CGRectMake((SCREEN_WIDTH - 192) * 0.5, (SCREEN_HEIGHT - 192) * 0.5 - 15, 192, 192);
  32. [self setCusView];
  33. }
  34. return self;
  35. }
  36. - (void)setCusView
  37. {
  38. [self layoutIfNeeded];
  39. [self addSubview:self.bgView];
  40. [self.bgView addSubview:self.imagV];
  41. [self.bgView addSubview:self.soundimagV];
  42. [self.bgView addSubview:self.soundL];
  43. [self.soundimagV addSubview:self.sound_1];
  44. [self.soundimagV addSubview:self.sound_2];
  45. [self.soundimagV addSubview:self.sound_3];
  46. [self.soundimagV addSubview:self.sound_4];
  47. [self.soundimagV addSubview:self.sound_5];
  48. [self.soundimagV addSubview:self.sound_6];
  49. [self.soundimagV addSubview:self.sound_7];
  50. [self.soundimagV addSubview:self.sound_8];
  51. NSTextAttachment *attach = [[NSTextAttachment alloc] init];
  52. attach.image = [UIImage imageNamed:@"audio_quxiao"];
  53. attach.bounds = CGRectMake(0, 0, 11.5, 11);
  54. NSAttributedString *attachString = [NSAttributedString attributedStringWithAttachment:attach];
  55. NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithAttributedString:attachString];
  56. [string appendAttributedString:[[NSAttributedString alloc] initWithString:@"上划取消"]];
  57. [string addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, string.length)];
  58. self.soundL.attributedText = string;
  59. [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
  60. make.edges.mas_offset(UIEdgeInsetsZero);
  61. }];
  62. [self.imagV mas_makeConstraints:^(MASConstraintMaker *make) {
  63. make.left.offset(52.5);
  64. make.top.offset(53);
  65. make.size.mas_offset(CGSizeMake(47, 67));
  66. }];
  67. [self.soundimagV mas_makeConstraints:^(MASConstraintMaker *make) {
  68. make.right.offset(-36);
  69. make.top.offset(60);
  70. make.size.mas_offset(CGSizeMake(40, 60));
  71. }];
  72. [self.soundL mas_makeConstraints:^(MASConstraintMaker *make) {
  73. make.bottom.offset(-17.5);
  74. make.centerX.equalTo(self.bgView);
  75. make.height.offset(20);
  76. make.width.mas_lessThanOrEqualTo(190);
  77. }];
  78. CGFloat W = 5.f;
  79. CGFloat H = 4.f;
  80. [self.sound_1 mas_makeConstraints:^(MASConstraintMaker *make) {
  81. make.left.mas_equalTo(self.soundimagV);
  82. make.size.mas_offset(CGSizeMake(W, H));
  83. make.bottom.mas_equalTo(self.soundimagV.mas_bottom).offset(0);
  84. }];
  85. [self.sound_2 mas_makeConstraints:^(MASConstraintMaker *make) {
  86. make.left.mas_equalTo(self.soundimagV);
  87. make.size.mas_offset(CGSizeMake(W * 2, 4));
  88. make.bottom.mas_equalTo(self.soundimagV.mas_bottom).offset(-H * 2);
  89. }];
  90. [self.sound_3 mas_makeConstraints:^(MASConstraintMaker *make) {
  91. make.left.mas_equalTo(self.soundimagV);
  92. make.size.mas_offset(CGSizeMake(W * 3, 4));
  93. make.bottom.mas_equalTo(self.soundimagV.mas_bottom).offset(-H * 4);
  94. }];
  95. [self.sound_4 mas_makeConstraints:^(MASConstraintMaker *make) {
  96. make.left.mas_equalTo(self.soundimagV);
  97. make.size.mas_offset(CGSizeMake(W * 4, 4));
  98. make.bottom.mas_equalTo(self.soundimagV.mas_bottom).offset(-H * 6);
  99. }];
  100. [self.sound_5 mas_makeConstraints:^(MASConstraintMaker *make) {
  101. make.left.mas_equalTo(self.soundimagV);
  102. make.size.mas_offset(CGSizeMake(W * 5, 4));
  103. make.bottom.mas_equalTo(self.soundimagV.mas_bottom).offset(-H * 8);
  104. }];
  105. [self.sound_6 mas_makeConstraints:^(MASConstraintMaker *make) {
  106. make.left.mas_equalTo(self.soundimagV);
  107. make.size.mas_offset(CGSizeMake(W * 6, 4));
  108. make.bottom.mas_equalTo(self.soundimagV.mas_bottom).offset(-H * 10);
  109. }];
  110. [self.sound_7 mas_makeConstraints:^(MASConstraintMaker *make) {
  111. make.left.mas_equalTo(self.soundimagV);
  112. make.size.mas_offset(CGSizeMake(W * 7, 4));
  113. make.bottom.mas_equalTo(self.soundimagV.mas_bottom).offset(-H * 12);
  114. }];
  115. [self.sound_8 mas_makeConstraints:^(MASConstraintMaker *make) {
  116. make.left.mas_equalTo(self.soundimagV);
  117. make.size.mas_offset(CGSizeMake(W * 8, 4));
  118. make.bottom.mas_equalTo(self.soundimagV.mas_bottom).offset(-H * 14);
  119. }];
  120. }
  121. - (void)show
  122. {
  123. self.timer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(changeImage) userInfo:nil repeats:YES];
  124. [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
  125. [[UIApplication sharedApplication].keyWindow addSubview:self];
  126. [UIView animateWithDuration:0.4 animations:^{
  127. } completion:^(BOOL finished) {}];
  128. }
  129. - (void)dismiss
  130. {
  131. [self.timer invalidate];
  132. self.timer = nil;
  133. WS(weakSelf);
  134. [UIView animateWithDuration:0.4 animations:^{
  135. } completion:^(BOOL finished) {
  136. [weakSelf removeFromSuperview];
  137. }];
  138. }
  139. - (UIView *)bgView
  140. {
  141. if (!_bgView) {
  142. _bgView = [UIView new];
  143. _bgView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"audio_Bg"]];
  144. }
  145. return _bgView;
  146. }
  147. - (UIImageView *)imagV
  148. {
  149. if (!_imagV) {
  150. _imagV = [UIImageView new];
  151. _imagV.image = [UIImage imageNamed:@"audio_huatong"];
  152. }
  153. return _imagV;
  154. }
  155. - (UIImageView *)soundimagV
  156. {
  157. if (!_soundimagV) {
  158. _soundimagV = [UIImageView new];
  159. _soundimagV.image = [UIImage imageNamed:@"audio_show"];
  160. }
  161. return _soundimagV;
  162. }
  163. - (UILabel *)soundL
  164. {
  165. if (!_soundL) {
  166. _soundL = [UILabel new];
  167. }
  168. return _soundL;
  169. }
  170. - (UILabel *)sound_1
  171. {
  172. if (!_sound_1) {
  173. _sound_1 = [UILabel new];
  174. _sound_1.tag = LABELTAG + 1;
  175. }
  176. return _sound_1;
  177. }
  178. - (UILabel *)sound_2
  179. {
  180. if (!_sound_2) {
  181. _sound_2 = [UILabel new];
  182. _sound_2.tag = LABELTAG + 2;
  183. }
  184. return _sound_2;
  185. }
  186. - (UILabel *)sound_3
  187. {
  188. if (!_sound_3) {
  189. _sound_3 = [UILabel new];
  190. _sound_3.tag = LABELTAG + 3;
  191. }
  192. return _sound_3;
  193. }
  194. - (UILabel *)sound_4
  195. {
  196. if (!_sound_4) {
  197. _sound_4 = [UILabel new];
  198. _sound_4.tag = LABELTAG + 4;
  199. }
  200. return _sound_4;
  201. }
  202. - (UILabel *)sound_5
  203. {
  204. if (!_sound_5) {
  205. _sound_5 = [UILabel new];
  206. _sound_5.tag = LABELTAG + 5;
  207. }
  208. return _sound_5;
  209. }
  210. - (UILabel *)sound_6
  211. {
  212. if (!_sound_6) {
  213. _sound_6 = [UILabel new];
  214. _sound_6.tag = LABELTAG + 6;
  215. }
  216. return _sound_6;
  217. }
  218. - (UILabel *)sound_7
  219. {
  220. if (!_sound_7) {
  221. _sound_7 = [UILabel new];
  222. _sound_7.tag = LABELTAG + 7;
  223. }
  224. return _sound_7;
  225. }
  226. - (UILabel *)sound_8
  227. {
  228. if (!_sound_8) {
  229. _sound_8 = [UILabel new];
  230. _sound_8.tag = LABELTAG + 8;
  231. }
  232. return _sound_8;
  233. }
  234. - (void)changeImage
  235. {
  236. [self.recorder updateMeters];//更新测量值
  237. float avg = [self.recorder averagePowerForChannel:0];
  238. float minValue = -60;
  239. float range = 60;
  240. float outRange = 100;
  241. if (avg < minValue) {
  242. avg = minValue;
  243. }
  244. float decibels = (avg + range) / range * outRange;
  245. NSLog(@"%f",decibels/100);
  246. NSUInteger level = decibels/10;
  247. NSLog(@"%luld",(unsigned long)level);
  248. for (NSUInteger i = 1; i <= level; i ++) {
  249. UILabel * label = (UILabel *)[self.soundimagV viewWithTag:LABELTAG + i];
  250. label.backgroundColor = [UIColor whiteColor];
  251. }
  252. for (NSUInteger i = level; i <= 8; i ++) {
  253. UILabel * label = (UILabel *)[self.soundimagV viewWithTag:LABELTAG + i];
  254. label.backgroundColor = [UIColor clearColor];
  255. }
  256. }
  257. @end