WFCUVoiceRecordView.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #import "WFCUVoiceRecordView.h"
  2. #import "WFCUImage.h"
  3. @interface WFCUVoiceRecordView ()
  4. @property (nonatomic,strong)UIImageView *recordAnimationView;
  5. @property (nonatomic,strong)UILabel *textLabel;
  6. @property (nonatomic,strong)UILabel *countdownLabel;
  7. @end
  8. @implementation WFCUVoiceRecordView
  9. + (void)initialize {
  10. // UIAppearance Proxy Defaults
  11. WFCUVoiceRecordView *recordView = [self appearance];
  12. recordView.voiceMessageAnimationImages = @[@"VoiceRecordFeedback001",@"VoiceRecordFeedback002",@"VoiceRecordFeedback003",@"VoiceRecordFeedback004",@"VoiceRecordFeedback005",@"VoiceRecordFeedback006",@"VoiceRecordFeedback007",@"VoiceRecordFeedback008",@"VoiceRecordFeedback009",@"VoiceRecordFeedback010",@"VoiceRecordFeedback011",@"VoiceRecordFeedback012",@"VoiceRecordFeedback013",@"VoiceRecordFeedback014",@"VoiceRecordFeedback015",@"VoiceRecordFeedback016",@"VoiceRecordFeedback017",@"VoiceRecordFeedback018",@"VoiceRecordFeedback019",@"VoiceRecordFeedback020"];
  13. recordView.upCancelText = @"手指上滑取消发送";
  14. recordView.loosenCancelText = @"松开手指取消发送";
  15. }
  16. - (id)initWithFrame:(CGRect)frame
  17. {
  18. self = [super initWithFrame:frame];
  19. if (self) {
  20. // Initialization code
  21. UIView *bgView = [[UIView alloc] initWithFrame:self.bounds];
  22. bgView.backgroundColor = [UIColor grayColor];
  23. bgView.layer.cornerRadius = 5;
  24. bgView.layer.masksToBounds = YES;
  25. bgView.alpha = 0.6;
  26. [self addSubview:bgView];
  27. _recordAnimationView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, self.bounds.size.width - 20, self.bounds.size.height - 30)];
  28. _recordAnimationView.image = [WFCUImage imageNamed:@"VoiceRecordFeedback001"];
  29. _recordAnimationView.contentMode = UIViewContentModeScaleAspectFit;
  30. [self addSubview:_recordAnimationView];
  31. _textLabel = [[UILabel alloc] initWithFrame:CGRectMake(5,
  32. self.bounds.size.height - 30,
  33. self.bounds.size.width - 10,
  34. 25)];
  35. _textLabel.textAlignment = NSTextAlignmentCenter;
  36. _textLabel.backgroundColor = [UIColor clearColor];
  37. _textLabel.text = WFCString(@"FingerUpToCancel");
  38. [self addSubview:_textLabel];
  39. _textLabel.font = [UIFont systemFontOfSize:13];
  40. _textLabel.textColor = [UIColor whiteColor];
  41. _textLabel.layer.cornerRadius = 5;
  42. _textLabel.layer.borderColor = [[UIColor redColor] colorWithAlphaComponent:0.5].CGColor;
  43. _textLabel.layer.masksToBounds = YES;
  44. CGRect rect = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height-30);
  45. _countdownLabel = [[UILabel alloc] initWithFrame:rect];
  46. _countdownLabel.textAlignment = NSTextAlignmentCenter;
  47. _countdownLabel.hidden = YES;
  48. _countdownLabel.font = [UIFont systemFontOfSize:32];
  49. _countdownLabel.textColor = [UIColor whiteColor];
  50. [self addSubview:_countdownLabel];
  51. }
  52. return self;
  53. }
  54. #ifdef WFC_PTT
  55. - (void)setIsPtt:(BOOL)isPtt {
  56. _isPtt = isPtt;
  57. if(isPtt) {
  58. _textLabel.text = WFCString(@"ReleaseEndTalk");
  59. _upCancelText = WFCString(@"ReleaseEndTalk");
  60. _loosenCancelText = WFCString(@"ReleaseEndTalk");
  61. }
  62. }
  63. #endif
  64. #pragma mark - setter
  65. - (void)setVoiceMessageAnimationImages:(NSArray *)voiceMessageAnimationImages
  66. {
  67. _voiceMessageAnimationImages = voiceMessageAnimationImages;
  68. }
  69. - (void)setUpCancelText:(NSString *)upCancelText
  70. {
  71. _upCancelText = upCancelText;
  72. _textLabel.text = _upCancelText;
  73. }
  74. - (void)setLoosenCancelText:(NSString *)loosenCancelText
  75. {
  76. _loosenCancelText = loosenCancelText;
  77. }
  78. // 录音按钮按下
  79. -(void)recordButtonTouchDown
  80. {
  81. // 需要根据声音大小切换recordView动画
  82. _textLabel.text = _upCancelText;
  83. _textLabel.backgroundColor = [UIColor clearColor];
  84. _countdownLabel.hidden = YES;
  85. _recordAnimationView.hidden = NO;
  86. }
  87. // 手指在录音按钮内部时离开
  88. -(void)recordButtonTouchUpInside
  89. {
  90. }
  91. // 手指在录音按钮外部时离开
  92. -(void)recordButtonTouchUpOutside
  93. {
  94. }
  95. // 手指移动到录音按钮内部
  96. -(void)recordButtonDragInside
  97. {
  98. _textLabel.text = _upCancelText;
  99. _textLabel.backgroundColor = [UIColor clearColor];
  100. }
  101. // 手指移动到录音按钮外部
  102. -(void)recordButtonDragOutside
  103. {
  104. _textLabel.text = _loosenCancelText;
  105. _textLabel.backgroundColor = [UIColor redColor];
  106. }
  107. -(void)setCountdown:(int)countdown {
  108. _countdownLabel.text = [NSString stringWithFormat:@"%d", countdown];
  109. _countdownLabel.hidden = NO;
  110. _recordAnimationView.hidden = YES;
  111. }
  112. -(void)setVoiceImage:(double)voiceMeter {
  113. _recordAnimationView.image = [WFCUImage imageNamed:[_voiceMessageAnimationImages objectAtIndex:0]];
  114. double voiceSound = voiceMeter;
  115. int index = voiceSound*[_voiceMessageAnimationImages count];
  116. if (index >= [_voiceMessageAnimationImages count]) {
  117. _recordAnimationView.image = [WFCUImage imageNamed:[_voiceMessageAnimationImages lastObject]];
  118. } else if(index < 0){
  119. _recordAnimationView.image = [WFCUImage imageNamed:[_voiceMessageAnimationImages objectAtIndex:0]];
  120. } else {
  121. _recordAnimationView.image = [WFCUImage imageNamed:[_voiceMessageAnimationImages objectAtIndex:index]];
  122. }
  123. }
  124. @end