EMMsgAudioBubbleView.m 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //
  2. // EMMsgAudioBubbleView.m
  3. // ChatDemo-UI3.0
  4. //
  5. // Created by XieYajie on 2019/2/14.
  6. // Copyright © 2019 XieYajie. All rights reserved.
  7. //
  8. #import "EMMsgAudioBubbleView.h"
  9. #define kEMMsgAudioMinWidth 30
  10. #define kEMMsgAudioMaxWidth 120
  11. @implementation EMMsgAudioBubbleView
  12. - (instancetype)initWithDirection:(EMMessageDirection)aDirection
  13. type:(EMMessageType)aType
  14. {
  15. self = [super initWithDirection:aDirection type:aType];
  16. if (self) {
  17. [self _setupSubviews];
  18. }
  19. return self;
  20. }
  21. #pragma mark - Subviews
  22. - (void)_setupSubviews
  23. {
  24. [self setupBubbleBackgroundImage];
  25. self.imgView = [[UIImageView alloc] init];
  26. self.imgView.contentMode = UIViewContentModeScaleAspectFit;
  27. self.imgView.clipsToBounds = YES;
  28. [self addSubview:self.imgView];
  29. self.imgView.animationDuration = 1.0;
  30. [self addSubview:self.imgView];
  31. [self.imgView mas_makeConstraints:^(MASConstraintMaker *make) {
  32. make.centerY.equalTo(self);
  33. make.top.equalTo(self).offset(10);
  34. make.right.equalTo(self).offset(-10);
  35. make.width.height.equalTo(@30);
  36. }];
  37. self.textLabel = [[UILabel alloc] init];
  38. self.textLabel.font = [UIFont systemFontOfSize:18];
  39. self.textLabel.numberOfLines = 0;
  40. [self addSubview:self.textLabel];
  41. [self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  42. make.top.equalTo(self).offset(10);
  43. make.left.equalTo(self).offset(10);
  44. make.right.equalTo(self.imgView.mas_left).offset(-10);
  45. make.bottom.equalTo(self).offset(-10);
  46. }];
  47. if (self.direction == EMMessageDirectionSend) {
  48. self.imgView.image = [UIImage imageNamed:@"msg_send_audio"];
  49. self.imgView.animationImages = @[[UIImage imageNamed:@"msg_send_audio01"], [UIImage imageNamed:@"msg_send_audio02"], [UIImage imageNamed:@"msg_send_audio"]];
  50. self.textLabel.textColor = [UIColor whiteColor];
  51. } else {
  52. self.imgView.image = [UIImage imageNamed:@"msg_recv_audio"];
  53. self.imgView.animationImages = @[[UIImage imageNamed:@"msg_recv_audio01"], [UIImage imageNamed:@"msg_recv_audio02"], [UIImage imageNamed:@"msg_recv_audio"]];
  54. self.textLabel.textColor = [UIColor blackColor];
  55. }
  56. }
  57. #pragma mark - Setter
  58. - (void)setModel:(EMMessageModel *)model
  59. {
  60. EMMessageType type = model.type;
  61. if (type == EMMessageTypeVoice) {
  62. EMVoiceMessageBody *body = (EMVoiceMessageBody *)model.emModel.body;
  63. self.textLabel.text = [NSString stringWithFormat:@"%d\"",(int)body.duration];
  64. if (model.isPlaying) {
  65. [self.imgView startAnimating];
  66. } else {
  67. [self.imgView stopAnimating];
  68. }
  69. CGFloat width = kEMMsgAudioMinWidth * body.duration / 10;
  70. if (width > kEMMsgAudioMaxWidth) {
  71. width = kEMMsgAudioMaxWidth;
  72. } else if (width < kEMMsgAudioMinWidth) {
  73. width = kEMMsgAudioMinWidth;
  74. }
  75. [self.textLabel mas_updateConstraints:^(MASConstraintMaker *make) {
  76. make.width.mas_equalTo(width);
  77. }];
  78. }
  79. }
  80. @end