recordingView.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //
  2. // recordingView.m
  3. // 双子星云手机
  4. //
  5. // Created by xd h on 2025/3/28.
  6. //
  7. #import "recordingView.h"
  8. #import "AudioRecorderManager.h"
  9. @interface recordingView ()
  10. @property(nonatomic,strong)UILabel * titlelabel;
  11. @property(nonatomic,strong)UILabel * timerlabel;
  12. @property(nonatomic,assign)NSInteger second;
  13. @property(nonatomic,strong)NSTimer * secondTimer;
  14. @property(nonatomic,strong)UIImageView *recordingTipImageView;
  15. @end
  16. @implementation recordingView
  17. - (id)initWithFrame:(CGRect)frame{
  18. self = [super initWithFrame:frame];
  19. _second = 0;
  20. self.backgroundColor = [UIColor hwColor:@"000000" alpha:0.6];
  21. [self drawAnyView];
  22. return self;
  23. }
  24. -(void)drawAnyView{
  25. UIView *whiteBgView = [[UIView alloc] init];
  26. whiteBgView.backgroundColor = [UIColor whiteColor];
  27. [self addSubview:whiteBgView];
  28. [whiteBgView mas_makeConstraints:^(MASConstraintMaker *make) {
  29. make.left.mas_equalTo(0.f);
  30. make.height.mas_equalTo(238.f + AdaptTabHeight);
  31. make.right.mas_equalTo(0.f);
  32. make.bottom.mas_equalTo(0.f);
  33. }];
  34. /*上圆角*/
  35. UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, SCREEN_W, 238.f + AdaptTabHeight)
  36. byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight
  37. cornerRadii:CGSizeMake(12, 12)];
  38. CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
  39. maskLayer.frame = CGRectMake(0, 0, SCREEN_W, 238.f + AdaptTabHeight);
  40. maskLayer.path = maskPath.CGPath;
  41. whiteBgView.layer.mask = maskLayer;
  42. UIButton *recordBut = [[UIButton alloc] init];
  43. [recordBut setBackgroundImage:[UIImage imageNamed:@"white_record_end_icon"] forState:UIControlStateNormal];
  44. [recordBut addTarget:self action:@selector(didClickButFun) forControlEvents:UIControlEventTouchUpInside];
  45. [whiteBgView addSubview:recordBut];
  46. [recordBut mas_makeConstraints:^(MASConstraintMaker *make) {
  47. make.width.mas_equalTo(78.f);
  48. make.height.mas_equalTo(78.f);
  49. make.centerX.mas_equalTo(0.f);
  50. make.bottom.mas_equalTo(-12.f - AdaptTabHeight);
  51. }];
  52. //
  53. _recordingTipImageView = [[UIImageView alloc] init];
  54. _recordingTipImageView.image = [UIImage imageNamed:@"recording_tip"];
  55. [whiteBgView addSubview:_recordingTipImageView];
  56. [_recordingTipImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  57. make.width.mas_equalTo(323.f*AUTOSCALE);
  58. make.height.mas_equalTo(52.f*AUTOSCALE);
  59. make.centerX.mas_equalTo(0.f);
  60. make.bottom.equalTo(recordBut.mas_top).offset(-20);
  61. }];
  62. //标题
  63. _titlelabel = [[UILabel alloc] init];
  64. [whiteBgView addSubview:_titlelabel];
  65. _titlelabel.font = [UIFont systemFontOfSize:14.f];
  66. _titlelabel.textAlignment = NSTextAlignmentCenter;
  67. [_titlelabel setTextColor:HW0A132BColor];
  68. [_titlelabel mas_makeConstraints:^(MASConstraintMaker *make) {
  69. make.left.mas_equalTo(12.0);
  70. make.top.mas_equalTo(15.0);
  71. make.height.mas_equalTo(20.0);
  72. make.right.mas_equalTo(-12.f);
  73. }];
  74. _timerlabel = [[UILabel alloc] init];
  75. [whiteBgView addSubview:_timerlabel];
  76. _timerlabel.font = [UIFont systemFontOfSize:12.f];
  77. _timerlabel.text = @"00:00:00";
  78. _timerlabel.textAlignment = NSTextAlignmentCenter;
  79. [_timerlabel setTextColor:[UIColor hwColor:@"979797"]];
  80. [_timerlabel mas_makeConstraints:^(MASConstraintMaker *make) {
  81. make.left.mas_equalTo(12.0);
  82. make.top.equalTo(_titlelabel.mas_bottom).offset(10);
  83. make.height.mas_equalTo(20.0);
  84. make.right.mas_equalTo(-12.f);
  85. }];
  86. }
  87. #pragma mark 开始录音
  88. - (void)beginRecordFunWith:(NSInteger)curIndex;
  89. {
  90. NSString *curName = [[NSString alloc] initWithFormat:@"record%ld.m4a",curIndex];
  91. _titlelabel.text = curName;
  92. [AudioRecorderManager sharedManager].curRecordName = curName;
  93. [[AudioRecorderManager sharedManager] startRecording];
  94. //_secondTimer in
  95. _secondTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerChange) userInfo:nil repeats:YES];
  96. [[NSRunLoop currentRunLoop] addTimer:_secondTimer forMode:NSRunLoopCommonModes];
  97. [self startBreathingAnimationWithImageView:_recordingTipImageView];
  98. }
  99. #pragma mark 一秒一次的timer检测
  100. - (void)timerChange{
  101. _second +=1;
  102. KWeakSelf
  103. mainBlock(^{
  104. [weakSelf setTimeStringFun];
  105. });
  106. }
  107. #pragma mark 设置时间
  108. - (void)setTimeStringFun
  109. {
  110. // 时长 totalTime
  111. NSInteger hour = 0;
  112. NSInteger min = 0;
  113. NSInteger second = 0;
  114. hour = _second / 60 /60;
  115. min = (_second -60*hour)/60;
  116. second = _second -60*hour - 60 *min;
  117. NSString* totalTimeStr = [[NSString alloc] initWithFormat:@"%02ld:%02ld:%02ld",hour,min,second];
  118. _timerlabel.text = totalTimeStr;
  119. }
  120. - (void)didClickButFun
  121. {
  122. [self stopBreathingAnimationWithImageView:_recordingTipImageView];
  123. [_secondTimer invalidate];
  124. _secondTimer = nil;
  125. [[AudioRecorderManager sharedManager] stopRecording];
  126. if (_didClickRecordEndFun) {
  127. _didClickRecordEndFun();
  128. }
  129. }
  130. - (void)startBreathingAnimationWithImageView:(UIImageView *)imageView {
  131. CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
  132. animation.duration = 2.0;
  133. animation.values = @[@1.0, @1.01, @1.02, @0.98, @1.0]; // 缩放值序列
  134. animation.keyTimes = @[@0, @0.25, @0.5, @0.75, @1]; // 关键帧时间点
  135. animation.repeatCount = HUGE_VALF;
  136. [imageView.layer addAnimation:animation forKey:@"keyframeScale"];
  137. }
  138. - (void)stopBreathingAnimationWithImageView:(UIImageView *)imageView
  139. {
  140. [imageView.layer removeAllAnimations];
  141. }
  142. @end