TUIRecordView.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // TRecordView.m
  3. // UIKit
  4. //
  5. // Created by kennethmiao on 2018/10/9.
  6. // Copyright © 2018 Tencent. All rights reserved.
  7. //
  8. #import "TUIRecordView.h"
  9. #import <TIMCommon/TIMDefine.h>
  10. #import "TUIChatConfig.h"
  11. @implementation TUIRecordView
  12. - (id)init {
  13. self = [super init];
  14. if (self) {
  15. [self setupViews];
  16. [self defaultLayout];
  17. }
  18. return self;
  19. }
  20. - (void)setupViews {
  21. self.backgroundColor = [UIColor clearColor];
  22. _background = [[UIView alloc] init];
  23. _background.backgroundColor = Record_Background_Color;
  24. _background.layer.cornerRadius = 5;
  25. [_background.layer setMasksToBounds:YES];
  26. [self addSubview:_background];
  27. _recordImage = [[UIImageView alloc] init];
  28. _recordImage.image = [UIImage imageNamed:TUIChatImagePath(@"record_1")];
  29. _recordImage.alpha = 0.8;
  30. _recordImage.contentMode = UIViewContentModeCenter;
  31. [_background addSubview:_recordImage];
  32. _title = [[UILabel alloc] init];
  33. _title.font = [UIFont systemFontOfSize:14];
  34. _title.textColor = [UIColor whiteColor];
  35. _title.textAlignment = NSTextAlignmentCenter;
  36. _title.layer.cornerRadius = 5;
  37. [_title.layer setMasksToBounds:YES];
  38. [_background addSubview:_title];
  39. _timeLabel = [[UILabel alloc] init];
  40. _timeLabel.font = [UIFont systemFontOfSize:14];
  41. _timeLabel.textColor = [UIColor whiteColor];
  42. _timeLabel.textAlignment = NSTextAlignmentCenter;
  43. _timeLabel.layer.cornerRadius = 5;
  44. _timeLabel.text = [NSString stringWithFormat:@"%.0f\"", MIN(60, [TUIChatConfig defaultConfig].maxAudioRecordDuration)];
  45. [_background addSubview:_timeLabel];
  46. }
  47. - (void)defaultLayout {
  48. CGSize backSize = CGSizeMake(150, 150);
  49. _title.text = TIMCommonLocalizableString(TUIKitInputRecordSlideToCancel);
  50. CGSize titleSize = [_title sizeThatFits:CGSizeMake(Screen_Width, Screen_Height)];
  51. CGSize timeSize = CGSizeMake(100, 15);
  52. if (titleSize.width > backSize.width) {
  53. backSize.width = titleSize.width + 2 * Record_Margin;
  54. }
  55. CGFloat imageHeight = backSize.height - titleSize.height - 2 * Record_Margin;
  56. [self.timeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  57. make.top.mas_equalTo(self.background).mas_offset(10);
  58. make.width.mas_equalTo(100);
  59. make.height.mas_equalTo(10);
  60. make.centerX.mas_equalTo(self.background);
  61. }];
  62. [self.recordImage mas_remakeConstraints:^(MASConstraintMaker *make) {
  63. make.top.mas_equalTo(self.timeLabel.mas_bottom).mas_offset(-13);
  64. make.centerX.mas_equalTo(self.background);
  65. make.width.mas_equalTo(backSize.width);
  66. make.height.mas_equalTo(imageHeight);
  67. }];
  68. [self.title mas_remakeConstraints:^(MASConstraintMaker *make) {
  69. make.centerX.mas_equalTo(self.background);
  70. make.top.mas_equalTo(self.recordImage.mas_bottom);
  71. make.width.mas_equalTo(backSize.width);
  72. make.height.mas_equalTo(15);
  73. }];
  74. [self.background mas_remakeConstraints:^(MASConstraintMaker *make) {
  75. make.top.mas_equalTo(self.timeLabel.mas_top).mas_offset(-3);
  76. make.bottom.mas_equalTo(self.title.mas_bottom).mas_offset(3);
  77. make.center.mas_equalTo(self);
  78. make.width.mas_equalTo(backSize.width);
  79. }];
  80. }
  81. - (void)setStatus:(RecordStatus)status {
  82. switch (status) {
  83. case Record_Status_Recording: {
  84. _title.text = TIMCommonLocalizableString(TUIKitInputRecordSlideToCancel);
  85. _title.backgroundColor = [UIColor clearColor];
  86. break;
  87. }
  88. case Record_Status_Cancel: {
  89. _title.text = TIMCommonLocalizableString(TUIKitInputRecordReleaseToCancel);
  90. _title.backgroundColor = [UIColor clearColor];
  91. break;
  92. }
  93. case Record_Status_TooShort: {
  94. _title.text = TIMCommonLocalizableString(TUIKitInputRecordTimeshort);
  95. _title.backgroundColor = [UIColor clearColor];
  96. break;
  97. }
  98. case Record_Status_TooLong: {
  99. _title.text = TIMCommonLocalizableString(TUIKitInputRecordTimeLong);
  100. _title.backgroundColor = [UIColor clearColor];
  101. break;
  102. }
  103. default:
  104. break;
  105. }
  106. }
  107. - (void)setPower:(NSInteger)power {
  108. NSString *imageName = [self getRecordImage:power];
  109. _recordImage.image = [UIImage imageNamed:TUIChatImagePath(imageName)];
  110. }
  111. - (NSString *)getRecordImage:(NSInteger)power {
  112. power = power + 60;
  113. int index = 0;
  114. if (power < 25) {
  115. index = 1;
  116. } else {
  117. index = ceil((power - 25) / 5.0) + 1;
  118. }
  119. index = MIN(index, 8);
  120. return [NSString stringWithFormat:@"record_%d", index];
  121. }
  122. @end