WFCUVoiceRecordView.m 4.9 KB

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