WFCUVoiceCell.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //
  2. // VoiceCell.m
  3. // WFChat UIKit
  4. //
  5. // Created by WF Chat on 2017/9/9.
  6. // Copyright © 2017年 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCUVoiceCell.h"
  9. #import <WFChatClient/WFCChatClient.h>
  10. @interface WFCUVoiceCell ()
  11. @property(nonatomic, strong) NSTimer *animationTimer;
  12. @property(nonatomic) int animationIndex;
  13. @end
  14. @implementation WFCUVoiceCell
  15. + (CGSize)sizeForClientArea:(WFCUMessageModel *)msgModel withViewWidth:(CGFloat)width {
  16. WFCCSoundMessageContent *soundContent = (WFCCSoundMessageContent *)msgModel.message.content;
  17. long duration = soundContent.duration;
  18. return CGSizeMake(50 + 30 * (MIN(MAX(0, duration-5), 20)/20.0), 30);
  19. }
  20. - (void)setModel:(WFCUMessageModel *)model {
  21. [super setModel:model];
  22. CGRect bounds = self.contentArea.bounds;
  23. if (model.message.direction == MessageDirection_Send) {
  24. self.voiceBtn.frame = CGRectMake(bounds.size.width - 30, 4, 22, 22);
  25. self.durationLabel.frame = CGRectMake(bounds.size.width - 48, 19, 18, 9);
  26. self.unplayedView.hidden = YES;
  27. } else {
  28. self.voiceBtn.frame = CGRectMake(4, 4, 22, 22);
  29. self.durationLabel.frame = CGRectMake(32, 19, 18, 9);
  30. if (model.message.status == Message_Status_Played) {
  31. self.unplayedView.hidden = YES;
  32. } else {
  33. self.unplayedView.hidden = NO;
  34. CGRect frame = [self.contentView convertRect:CGRectMake(self.contentArea.bounds.size.width + 10, 12, 10, 10) fromView:self.contentArea];
  35. self.unplayedView.frame = frame;
  36. }
  37. }
  38. WFCCSoundMessageContent *soundContent = (WFCCSoundMessageContent *)model.message.content;
  39. self.durationLabel.text = [NSString stringWithFormat:@"%ld''", soundContent.duration];
  40. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(startAnimationTimer) name:kVoiceMessageStartPlaying object:@(model.message.messageId)];
  41. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopAnimationTimer) name:kVoiceMessagePlayStoped object:nil];
  42. if (model.voicePlaying) {
  43. [self startAnimationTimer];
  44. } else {
  45. [self stopAnimationTimer];
  46. }
  47. }
  48. - (UIView *)unplayedView {
  49. if (!_unplayedView) {
  50. _unplayedView = [[UIView alloc] init];
  51. _unplayedView.layer.cornerRadius = 5.f;
  52. _unplayedView.layer.masksToBounds = YES;
  53. _unplayedView.backgroundColor = [UIColor redColor];
  54. [self.contentView addSubview:_unplayedView];
  55. }
  56. return _unplayedView;
  57. }
  58. - (UIImageView *)voiceBtn {
  59. if (!_voiceBtn) {
  60. _voiceBtn = [[UIImageView alloc] init];
  61. [self.contentArea addSubview:_voiceBtn];
  62. }
  63. return _voiceBtn;
  64. }
  65. - (UILabel *)durationLabel {
  66. if (!_durationLabel) {
  67. _durationLabel = [[UILabel alloc] init];
  68. _durationLabel.font = [UIFont systemFontOfSize:9];
  69. [self.contentArea addSubview:_durationLabel];
  70. }
  71. return _durationLabel;
  72. }
  73. - (void)startAnimationTimer {
  74. [self stopAnimationTimer];
  75. self.animationTimer = [NSTimer scheduledTimerWithTimeInterval:0.5
  76. target:self
  77. selector:@selector(scheduleAnimation:)
  78. userInfo:nil
  79. repeats:YES];
  80. [self.animationTimer fire];
  81. }
  82. - (void)scheduleAnimation:(id)sender {
  83. NSString *_playingImg;
  84. if (MessageDirection_Send == self.model.message.direction) {
  85. _playingImg = [NSString stringWithFormat:@"sent_voice_%d", (self.animationIndex++ % 3) + 1];
  86. } else {
  87. _playingImg = [NSString stringWithFormat:@"received_voice_%d", (self.animationIndex++ % 3) + 1];
  88. }
  89. [self.voiceBtn setImage:[UIImage imageNamed:_playingImg]];
  90. }
  91. - (void)stopAnimationTimer {
  92. if (self.animationTimer && [self.animationTimer isValid]) {
  93. [self.animationTimer invalidate];
  94. self.animationTimer = nil;
  95. self.animationIndex = 0;
  96. }
  97. if (self.model.message.direction == MessageDirection_Send) {
  98. [self.voiceBtn setImage:[UIImage imageNamed:@"sent_voice"]];
  99. } else {
  100. [self.voiceBtn setImage:[UIImage imageNamed:@"received_voice"]];
  101. }
  102. }
  103. - (void)dealloc {
  104. [[NSNotificationCenter defaultCenter] removeObserver:self];
  105. }
  106. @end