WFCUVoiceCell.m 4.3 KB

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