MOLuckyFloatingView.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. //
  2. // MOLuckyFloatingView.m
  3. // MiMoLive
  4. //
  5. // Created by SuperC on 2025/7/23.
  6. //
  7. #define kShowViewHeight ((SCREENWIDTH - 32.0 * 2.0) * 32.0 / 314.0)
  8. #define kShowHeadWidth 20.0
  9. #define kMOSuperGiftViewWidth (SCREENWIDTH - 32.0 * 2.0)
  10. #define kShowGiftFont 12.0
  11. #define kShowGiftBigFont 16.0
  12. #import "MOLuckyFloatingView.h"
  13. @interface MOLuckyFloatingView ()
  14. @property (nonatomic, strong) UIView *bgView;
  15. @property (nonatomic, strong) UIImageView *bgImgView;
  16. @property (nonatomic, strong) UIImageView *headImgView;
  17. @property (nonatomic, strong) UIScrollView *contentScrollView;
  18. @property (nonatomic, strong) UILabel *contentLab;
  19. @end
  20. @implementation MOLuckyFloatingView
  21. - (instancetype)initWithFrame:(CGRect)frame{
  22. self = [super initWithFrame:frame];
  23. if (self)
  24. {
  25. self.backgroundColor = [UIColor clearColor];
  26. [self setupUI];
  27. }
  28. return self;
  29. }
  30. - (void)setupUI{
  31. [self addSubview:self.bgView];
  32. self.clipsToBounds = NO;
  33. [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
  34. make.left.right.equalTo(self);
  35. make.height.equalTo(@(kShowViewHeight));
  36. make.centerY.equalTo(self);
  37. }];
  38. [self.bgView addSubview:self.bgImgView];
  39. [self.bgImgView mas_makeConstraints:^(MASConstraintMaker *make) {
  40. make.left.right.equalTo(self.bgView);
  41. make.bottom.equalTo(self.bgView).offset(0.0);
  42. make.height.equalTo(@(kShowViewHeight));
  43. }];
  44. self.bgView.clipsToBounds = NO;
  45. [self addSubview:self.headImgView];
  46. [self.headImgView mas_makeConstraints:^(MASConstraintMaker *make) {
  47. make.left.equalTo(self.bgView).offset(58.0);
  48. make.width.height.equalTo(@(kShowHeadWidth));
  49. make.centerY.equalTo(self);
  50. }];
  51. self.headImgView.layer.cornerRadius = kShowHeadWidth / 2.0;
  52. self.headImgView.layer.masksToBounds = YES;
  53. [self.bgView addSubview:self.contentScrollView];
  54. [self.contentScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
  55. make.left.equalTo(self.headImgView.mas_right).offset(5.0);
  56. make.right.equalTo(self.bgView).offset(-12.0);
  57. make.centerY.equalTo(self.bgView);
  58. make.height.equalTo(@24.0);
  59. }];
  60. [self.contentScrollView addSubview:self.contentLab];
  61. [self.contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
  62. make.left.equalTo(self.contentScrollView).offset(0.0);
  63. make.right.equalTo(self.contentScrollView).offset(0.0);
  64. make.centerY.equalTo(self.bgView);
  65. make.height.equalTo(@24.0);
  66. }];
  67. }
  68. - (void)setViewModel:(MORtmEntity *)viewModel{
  69. _viewModel = viewModel;
  70. MORtmJosnEntity *jsonEntity = (MORtmJosnEntity *)viewModel.data;
  71. MORtmUser *user = jsonEntity.luxury.user;
  72. MORtmUser *anchor = jsonEntity.luxury.anchor;
  73. self.bgView.hidden = NO;
  74. self.headImgView.hidden = NO;
  75. NSString *contentStr;
  76. if (jsonEntity.type == 10){//全服红包飘屏
  77. [self.headImgView sd_setImageWithURL:[NSURL URLWithString:jsonEntity.redEnvelope.senderAvatar] placeholderImage:[UIImage imageNamed:@"icon_mine_placeHolder"]];
  78. contentStr = [NSString stringWithFormat:NSLocalString(@"mimo_red_packet_all_room_rtm_tip"),jsonEntity.redEnvelope.senderNickname];
  79. self.contentLab.attributedText = [self colorfulStringWith:contentStr AndNameStr:jsonEntity.redEnvelope.senderNickname];
  80. [self.bgImgView setImage:[UIImage imageNamed:@"icon_red_piao_top"]];
  81. }
  82. else if(jsonEntity.type == 4 ||
  83. jsonEntity.type == 17){
  84. //Super Lucky 飘屏
  85. [self.bgImgView setImage:[UIImage imageNamed:@"icon_super_lucky_bg_top"]];
  86. MORtmUser *user = jsonEntity.activitySuperLucky2.user;
  87. [self.headImgView sd_setImageWithURL:[NSURL URLWithString:user.avatar] placeholderImage:[UIImage imageNamed:@"icon_mine_placeHolder"]];
  88. NSString *contentStr = [NSString stringWithFormat:@"%@ is %@ win %zd diamonds",user.username,[MOTextAESTools smallTip],jsonEntity.activitySuperLucky2.diamond];
  89. self.contentLab.attributedText = [self colorfulStringWith:contentStr Num:jsonEntity.activitySuperLucky2.diamond name:user.username];
  90. }
  91. }
  92. - (void)toJudgeNeedAnimationScrollView{
  93. MORtmJosnEntity *jsonEntity = (MORtmJosnEntity *)self.viewModel.data;
  94. // 取消之前的动画
  95. [self.contentScrollView.layer removeAllAnimations];
  96. self.contentScrollView.contentOffset = CGPointZero;
  97. MORtmUser *user = jsonEntity.luxury.user;
  98. MORtmUser *anchor = jsonEntity.luxury.anchor;
  99. NSString *contentStr;
  100. if(jsonEntity.type == 8){
  101. MOGiftInfo *giftInfo = [[MOSvgaSourceManage shareManager] getGiftInfoWithCode:jsonEntity.luxury.giftCode];
  102. if(giftInfo){
  103. contentStr = [NSString stringWithFormat:@"Wow! %@ send %@ to %@",user.username,giftInfo.giftName,anchor.username];
  104. }
  105. else{
  106. contentStr = [NSString stringWithFormat:@"Wow! %@ send gift to %@",user.username,anchor.username];
  107. }
  108. }
  109. else if(jsonEntity.type == 4 ||
  110. jsonEntity.type == 17){
  111. //SuperLucky
  112. MORtmUser *user = jsonEntity.activitySuperLucky2.user;
  113. contentStr = [NSString stringWithFormat:@"%@ is %@ win %zd diamonds",user.username,[MOTextAESTools smallTip],jsonEntity.activitySuperLucky2.diamond];
  114. }
  115. CGFloat contentWidth = [MOTools getWidthWithString:contentStr font:[MOTextTools regularFont:kShowGiftFont]] + 15.0;
  116. if(jsonEntity.type == 4 ||
  117. jsonEntity.type == 17){
  118. MORtmUser *tempUser = jsonEntity.activitySuperLucky2.user;
  119. NSAttributedString *tempStr = [self colorfulStringWith:contentStr Num:jsonEntity.activitySuperLucky2.diamond name:tempUser.username];
  120. contentWidth = [MOTextTools getTheAttributedTextWidthWith:tempStr] + 15.0;
  121. }
  122. CGFloat realSpacing = kMOSuperGiftViewWidth - 58.0 - kShowHeadWidth - 5.0 - 12.0;
  123. self.contentScrollView.contentSize = CGSizeMake(contentWidth, 24.0);
  124. if(contentWidth > realSpacing){
  125. [UIView animateKeyframesWithDuration:2
  126. delay:0
  127. options:UIViewAnimationOptionAutoreverse //动画重复自动反向,需要和上面这个一起用
  128. |UIViewAnimationOptionCurveLinear //动画的时间曲线,滚动字幕线性比较合理
  129. animations:^{
  130. // 计算移动的距离
  131. CGPoint point = self.contentScrollView.contentOffset;
  132. point.x = contentWidth - realSpacing;
  133. self.contentScrollView.contentOffset = point;
  134. }
  135. completion:^(BOOL finished) {
  136. // 重置动画,将 contentOffset 重置为初始值
  137. self.contentScrollView.contentOffset = CGPointZero;
  138. }];
  139. }
  140. }
  141. - (NSAttributedString *)colorfulStringWith:(NSString *)contentStr AndNameStr:(NSString *)nameStr{
  142. NSString *needStr;
  143. needStr = contentStr;
  144. // 创建属性字符串,整体颜色为白色
  145. NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:needStr attributes:@{NSForegroundColorAttributeName: [MOTools colorWithHexString:@"#FFFFFF" alpha:1.0],NSFontAttributeName : [MOTextTools regularFont:kShowGiftFont]}];
  146. NSRange redRange = [needStr rangeOfString:nameStr];
  147. if(redRange.location != NSNotFound){
  148. // 将最后三个字符的颜色设置为红色
  149. [attributedString addAttribute:NSForegroundColorAttributeName value:[MOTools colorWithHexString:@"#19FFB2" alpha:1.0] range:redRange];
  150. [attributedString addAttribute:NSFontAttributeName value:[MOTextTools mediumFont:kShowGiftFont] range:redRange];
  151. }
  152. return attributedString;
  153. }
  154. - (NSAttributedString *)colorfulStringWith:(NSString *)contentStr Num:(NSInteger)num name:(NSString *)nameStr{
  155. NSString *needStr;
  156. if(num == 0){
  157. needStr = contentStr;
  158. // 创建属性字符串,整体颜色为白色
  159. NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:needStr attributes:@{NSForegroundColorAttributeName: [MOTools colorWithHexString:@"#F4AEFF" alpha:1.0],NSFontAttributeName : [MOTextTools regularFont:kShowGiftFont]}];
  160. return attributedString;
  161. }
  162. else{
  163. NSString *numStr = [NSString stringWithFormat:@"%zd",num];
  164. needStr = contentStr;
  165. // 创建属性字符串,整体颜色为白色
  166. NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:needStr attributes:@{NSForegroundColorAttributeName: [MOTools colorWithHexString:@"#F4AEFF" alpha:1.0],NSFontAttributeName : [MOTextTools regularFont:kShowGiftFont]}];
  167. NSRange blueRange = [needStr rangeOfString:nameStr];
  168. if(blueRange.location != NSNotFound){
  169. // 将最后三个字符的颜色设置为红色
  170. [attributedString addAttribute:NSForegroundColorAttributeName value:[MOTools colorWithHexString:@"#19FFB2" alpha:1.0] range:blueRange];
  171. [attributedString addAttribute:NSFontAttributeName value:[MOTextTools mediumFont:kShowGiftFont] range:blueRange];
  172. }
  173. NSRange redRange = [needStr rangeOfString:numStr];
  174. if(redRange.location != NSNotFound){
  175. // 将最后三个字符的颜色设置为红色
  176. [attributedString addAttribute:NSForegroundColorAttributeName value:[MOTools colorWithHexString:@"#19FFB2" alpha:1.0] range:redRange];
  177. // 设置字体大小的属性
  178. [attributedString addAttribute:NSFontAttributeName value:[MOTextTools mediumFont:kShowGiftBigFont] range:redRange];
  179. }
  180. return attributedString;
  181. }
  182. }
  183. #pragma mark - Lazy
  184. - (UIView *)bgView{
  185. if(!_bgView){
  186. _bgView = [[UIView alloc] init];
  187. _bgView.backgroundColor = [UIColor clearColor];
  188. }
  189. return _bgView;
  190. }
  191. - (UIImageView *)bgImgView{
  192. if (!_bgImgView)
  193. {
  194. _bgImgView = [[UIImageView alloc] init];
  195. _bgImgView.contentMode = UIViewContentModeScaleToFill;
  196. [_bgImgView setImage:[UIImage imageNamed:@"icon_super_gift_bg_top"]];
  197. }
  198. return _bgImgView;
  199. }
  200. - (UIImageView *)headImgView{
  201. if (!_headImgView)
  202. {
  203. _headImgView = [[UIImageView alloc] init];
  204. _headImgView.contentMode = UIViewContentModeScaleAspectFill;
  205. [_headImgView setImage:[UIImage imageNamed:@"icon_mine_placeHolder"]];
  206. }
  207. return _headImgView;
  208. }
  209. - (UILabel *)contentLab{
  210. if (!_contentLab)
  211. {
  212. _contentLab = [[UILabel alloc] init];
  213. _contentLab.text = @"";
  214. _contentLab.font = [MOTextTools getTheFontWithSize:kShowGiftFont AndFontName:kNormalContentFontStr];
  215. _contentLab.textColor = [MOTools colorWithHexString:@"#FFC4C3" alpha:1.0];
  216. _contentLab.textAlignment = NSTextAlignmentRight;
  217. _contentLab.numberOfLines = 1;
  218. }
  219. return _contentLab;
  220. }
  221. - (UIScrollView *)contentScrollView{
  222. if(!_contentScrollView){
  223. _contentScrollView = [[UIScrollView alloc] init];
  224. _contentScrollView.contentSize = CGSizeMake(SCREENWIDTH, 24.0);
  225. _contentScrollView.showsHorizontalScrollIndicator = NO;
  226. }
  227. return _contentScrollView;
  228. }
  229. @end