MOLevelUpNoticeView.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // MOLevelUpNoticeView.m
  3. // MiMoLive
  4. //
  5. // Created by SuperC on 2025/6/27.
  6. //
  7. #import "MOLevelUpNoticeView.h"
  8. #import <YYKit/YYLabel.h>
  9. @interface MOLevelUpNoticeView ()
  10. @property (nonatomic, strong) YYLabel *contentLab;
  11. @end
  12. @implementation MOLevelUpNoticeView
  13. - (instancetype)initWithFrame:(CGRect)frame{
  14. self = [super initWithFrame:frame];
  15. if (self)
  16. {
  17. self.backgroundColor = [UIColor clearColor];
  18. [self setupUI];
  19. }
  20. return self;
  21. }
  22. - (void)setupUI{
  23. self.clipsToBounds = YES;
  24. CGFloat bgImgWidth = SCREENWIDTH - 30.0 * 2.0;
  25. CGFloat bgImgHeight = bgImgWidth * 28.0 / 314.0; // 28.0 是原图的高度,314.0 是原图的宽度
  26. [self addSubview:self.bgImgView];
  27. [self.bgImgView mas_makeConstraints:^(MASConstraintMaker *make) {
  28. make.height.equalTo(@(bgImgHeight));
  29. make.width.equalTo(@(bgImgWidth));
  30. make.centerY.equalTo(self);
  31. make.centerX.equalTo(self);
  32. }];
  33. [self addSubview:self.contentScrollView];
  34. [self.contentScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
  35. make.left.equalTo(self).offset(12);
  36. make.right.equalTo(self).offset(-12.0);
  37. make.centerY.equalTo(self).offset(0.0);
  38. make.height.equalTo(@20.0);
  39. }];
  40. CGFloat width = SCREENWIDTH - 43.0 * 2.0;
  41. [self.contentScrollView addSubview:self.contentLab];
  42. [self.contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
  43. make.left.equalTo(self.contentScrollView).offset(0.0);
  44. make.right.equalTo(self.contentScrollView).offset(0.0);
  45. make.width.greaterThanOrEqualTo(@(width));
  46. make.centerY.equalTo(self).offset(0.0);
  47. make.height.equalTo(@20.0);
  48. }];
  49. }
  50. - (void)setViewModel:(MORtmEntity *)viewModel{
  51. _viewModel = viewModel;
  52. CGFloat bgImgWidth = SCREENWIDTH - 30.0 * 2.0;
  53. CGFloat bgImgHeight = bgImgWidth * 28.0 / 314.0; // 28.0 是原图的高度,314.0 是原图的宽度
  54. MORtmJosnEntity *jsonEntity = (MORtmJosnEntity *)viewModel.data;
  55. WEAKSELF
  56. if(jsonEntity.type == 26){// 用户升级通知框
  57. MORtmUser *vipUser = jsonEntity.roomUser;
  58. MORtmUserLevelUpgrade *userLevel = jsonEntity.levelNotice;
  59. if(userLevel.effectsUrl.length > 0){
  60. [self.bgImgView sd_setImageWithURL:[NSURL URLWithString:userLevel.effectsUrl] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
  61. CGFloat theImageHeight = kScaleWidth(28.0);
  62. if(image.size.width > 0){
  63. theImageHeight = image.size.height * (SCREENWIDTH - 30.0 * 2.0) / image.size.width;
  64. }
  65. kMainThreadBlock((^{
  66. [self.bgImgView mas_updateConstraints:^(MASConstraintMaker *make) {
  67. make.height.equalTo(@(theImageHeight));
  68. }];
  69. }))
  70. }];
  71. }
  72. else{
  73. [self.bgImgView setImage:[UIImage imageNamed:@"icon_rtm_level_up"]];
  74. [self.bgImgView mas_updateConstraints:^(MASConstraintMaker *make) {
  75. make.height.equalTo(@(bgImgHeight));
  76. }];
  77. }
  78. self.contentLab.attributedText = [MOTextTools createTheLevelUpNoticeWith:viewModel];
  79. }
  80. }
  81. - (void)toJudgeNeedAnimationScrollView{
  82. // 取消之前的动画
  83. [self.contentScrollView.layer removeAllAnimations];
  84. self.contentScrollView.contentOffset = CGPointZero;
  85. NSMutableAttributedString *tipAttri;
  86. MORtmJosnEntity *jsonEntity = (MORtmJosnEntity *)self.viewModel.data;
  87. if(jsonEntity.type == 26){// 用户升级通知框
  88. MORtmUser *vipUser = jsonEntity.roomUser;
  89. MORtmUserLevelUpgrade *userLevel = jsonEntity.levelNotice;
  90. tipAttri = [MOTextTools creteTextWith:self.viewModel];
  91. }
  92. if(tipAttri.length == 0){
  93. return;
  94. }
  95. CGFloat contentWidth = [MOTextTools getWidthWith:self.viewModel] + 30.0;
  96. CGFloat realSpacing = SCREENWIDTH - 30.0 * 2.0 - 12.0 * 2.0;
  97. self.contentScrollView.contentSize = CGSizeMake(contentWidth, 24.0);
  98. if(contentWidth > realSpacing){
  99. [UIView animateKeyframesWithDuration:2
  100. delay:0
  101. options:UIViewAnimationOptionAutoreverse //动画重复自动反向,需要和上面这个一起用
  102. |UIViewAnimationOptionCurveLinear //动画的时间曲线,滚动字幕线性比较合理
  103. animations:^{
  104. // 计算移动的距离
  105. CGPoint point = self.contentScrollView.contentOffset;
  106. point.x = contentWidth - realSpacing;
  107. self.contentScrollView.contentOffset = point;
  108. }
  109. completion:^(BOOL finished) {
  110. // 重置动画,将 contentOffset 重置为初始值
  111. self.contentScrollView.contentOffset = CGPointZero;
  112. }];
  113. }
  114. }
  115. #pragma mark - Lazy
  116. - (UIImageView *)bgImgView{
  117. if (!_bgImgView)
  118. {
  119. _bgImgView = [[UIImageView alloc] init];
  120. _bgImgView.contentMode = UIViewContentModeScaleToFill;
  121. [_bgImgView setImage:[UIImage imageNamed:@"icon_rtm_level_up"]];
  122. }
  123. return _bgImgView;
  124. }
  125. - (YYLabel *)contentLab{
  126. if (!_contentLab){
  127. _contentLab = [[YYLabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 20.0)];
  128. _contentLab.text = @"";
  129. _contentLab.font = [MOTextTools regularFont:12.0];
  130. _contentLab.textColor = [MOTools colorWithHexString:@"#FFC4C3" alpha:1.0];
  131. _contentLab.textAlignment = NSTextAlignmentLeft;
  132. _contentLab.numberOfLines = 1;
  133. }
  134. return _contentLab;
  135. }
  136. - (UIScrollView *)contentScrollView{
  137. if(!_contentScrollView){
  138. _contentScrollView = [[UIScrollView alloc] init];
  139. _contentScrollView.contentSize = CGSizeMake(SCREENWIDTH, 24.0);
  140. _contentScrollView.showsHorizontalScrollIndicator = NO;
  141. }
  142. return _contentScrollView;
  143. }
  144. @end