| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- //
- // MOLuckyFloatingView.m
- // MiMoLive
- //
- // Created by SuperC on 2025/7/23.
- //
- #define kShowViewHeight ((SCREENWIDTH - 32.0 * 2.0) * 32.0 / 314.0)
- #define kShowHeadWidth 20.0
- #define kMOSuperGiftViewWidth (SCREENWIDTH - 32.0 * 2.0)
- #define kShowGiftFont 12.0
- #define kShowGiftBigFont 16.0
- #import "MOLuckyFloatingView.h"
- @interface MOLuckyFloatingView ()
- @property (nonatomic, strong) UIView *bgView;
- @property (nonatomic, strong) UIImageView *bgImgView;
- @property (nonatomic, strong) UIImageView *headImgView;
- @property (nonatomic, strong) UIScrollView *contentScrollView;
- @property (nonatomic, strong) UILabel *contentLab;
- @end
- @implementation MOLuckyFloatingView
- - (instancetype)initWithFrame:(CGRect)frame{
- self = [super initWithFrame:frame];
- if (self)
- {
- self.backgroundColor = [UIColor clearColor];
- [self setupUI];
- }
- return self;
- }
- - (void)setupUI{
- [self addSubview:self.bgView];
- self.clipsToBounds = NO;
- [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.right.equalTo(self);
- make.height.equalTo(@(kShowViewHeight));
- make.centerY.equalTo(self);
- }];
-
- [self.bgView addSubview:self.bgImgView];
- [self.bgImgView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.right.equalTo(self.bgView);
- make.bottom.equalTo(self.bgView).offset(0.0);
- make.height.equalTo(@(kShowViewHeight));
- }];
- self.bgView.clipsToBounds = NO;
-
- [self addSubview:self.headImgView];
- [self.headImgView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(self.bgView).offset(58.0);
- make.width.height.equalTo(@(kShowHeadWidth));
- make.centerY.equalTo(self);
- }];
- self.headImgView.layer.cornerRadius = kShowHeadWidth / 2.0;
- self.headImgView.layer.masksToBounds = YES;
-
- [self.bgView addSubview:self.contentScrollView];
- [self.contentScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(self.headImgView.mas_right).offset(5.0);
- make.right.equalTo(self.bgView).offset(-12.0);
- make.centerY.equalTo(self.bgView);
- make.height.equalTo(@24.0);
- }];
-
- [self.contentScrollView addSubview:self.contentLab];
- [self.contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(self.contentScrollView).offset(0.0);
- make.right.equalTo(self.contentScrollView).offset(0.0);
- make.centerY.equalTo(self.bgView);
- make.height.equalTo(@24.0);
- }];
- }
- - (void)setViewModel:(MORtmEntity *)viewModel{
- _viewModel = viewModel;
-
- MORtmJosnEntity *jsonEntity = (MORtmJosnEntity *)viewModel.data;
- MORtmUser *user = jsonEntity.luxury.user;
- MORtmUser *anchor = jsonEntity.luxury.anchor;
-
- self.bgView.hidden = NO;
- self.headImgView.hidden = NO;
-
- NSString *contentStr;
- if (jsonEntity.type == 10){//全服红包飘屏
- [self.headImgView sd_setImageWithURL:[NSURL URLWithString:jsonEntity.redEnvelope.senderAvatar] placeholderImage:[UIImage imageNamed:@"icon_mine_placeHolder"]];
-
- contentStr = [NSString stringWithFormat:NSLocalString(@"mimo_red_packet_all_room_rtm_tip"),jsonEntity.redEnvelope.senderNickname];
- self.contentLab.attributedText = [self colorfulStringWith:contentStr AndNameStr:jsonEntity.redEnvelope.senderNickname];
-
- [self.bgImgView setImage:[UIImage imageNamed:@"icon_red_piao_top"]];
- }
- else if(jsonEntity.type == 4 ||
- jsonEntity.type == 17){
- //Super Lucky 飘屏
-
- [self.bgImgView setImage:[UIImage imageNamed:@"icon_super_lucky_bg_top"]];
-
- MORtmUser *user = jsonEntity.activitySuperLucky2.user;
-
- [self.headImgView sd_setImageWithURL:[NSURL URLWithString:user.avatar] placeholderImage:[UIImage imageNamed:@"icon_mine_placeHolder"]];
-
-
- NSString *contentStr = [NSString stringWithFormat:@"%@ is %@ win %zd diamonds",user.username,[MOTextAESTools smallTip],jsonEntity.activitySuperLucky2.diamond];
- self.contentLab.attributedText = [self colorfulStringWith:contentStr Num:jsonEntity.activitySuperLucky2.diamond name:user.username];
- }
- }
- - (void)toJudgeNeedAnimationScrollView{
- MORtmJosnEntity *jsonEntity = (MORtmJosnEntity *)self.viewModel.data;
-
- // 取消之前的动画
- [self.contentScrollView.layer removeAllAnimations];
- self.contentScrollView.contentOffset = CGPointZero;
-
- MORtmUser *user = jsonEntity.luxury.user;
- MORtmUser *anchor = jsonEntity.luxury.anchor;
-
- NSString *contentStr;
- if(jsonEntity.type == 8){
- MOGiftInfo *giftInfo = [[MOSvgaSourceManage shareManager] getGiftInfoWithCode:jsonEntity.luxury.giftCode];
-
- if(giftInfo){
- contentStr = [NSString stringWithFormat:@"Wow! %@ send %@ to %@",user.username,giftInfo.giftName,anchor.username];
- }
- else{
- contentStr = [NSString stringWithFormat:@"Wow! %@ send gift to %@",user.username,anchor.username];
- }
- }
- else if(jsonEntity.type == 4 ||
- jsonEntity.type == 17){
- //SuperLucky
- MORtmUser *user = jsonEntity.activitySuperLucky2.user;
- contentStr = [NSString stringWithFormat:@"%@ is %@ win %zd diamonds",user.username,[MOTextAESTools smallTip],jsonEntity.activitySuperLucky2.diamond];
- }
-
- CGFloat contentWidth = [MOTools getWidthWithString:contentStr font:[MOTextTools regularFont:kShowGiftFont]] + 15.0;
- if(jsonEntity.type == 4 ||
- jsonEntity.type == 17){
- MORtmUser *tempUser = jsonEntity.activitySuperLucky2.user;
- NSAttributedString *tempStr = [self colorfulStringWith:contentStr Num:jsonEntity.activitySuperLucky2.diamond name:tempUser.username];
- contentWidth = [MOTextTools getTheAttributedTextWidthWith:tempStr] + 15.0;
- }
-
- CGFloat realSpacing = kMOSuperGiftViewWidth - 58.0 - kShowHeadWidth - 5.0 - 12.0;
-
- self.contentScrollView.contentSize = CGSizeMake(contentWidth, 24.0);
- if(contentWidth > realSpacing){
- [UIView animateKeyframesWithDuration:2
- delay:0
- options:UIViewAnimationOptionAutoreverse //动画重复自动反向,需要和上面这个一起用
- |UIViewAnimationOptionCurveLinear //动画的时间曲线,滚动字幕线性比较合理
- animations:^{
- // 计算移动的距离
- CGPoint point = self.contentScrollView.contentOffset;
- point.x = contentWidth - realSpacing;
- self.contentScrollView.contentOffset = point;
- }
- completion:^(BOOL finished) {
- // 重置动画,将 contentOffset 重置为初始值
- self.contentScrollView.contentOffset = CGPointZero;
- }];
- }
- }
- - (NSAttributedString *)colorfulStringWith:(NSString *)contentStr AndNameStr:(NSString *)nameStr{
- NSString *needStr;
- needStr = contentStr;
-
- // 创建属性字符串,整体颜色为白色
- NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:needStr attributes:@{NSForegroundColorAttributeName: [MOTools colorWithHexString:@"#FFFFFF" alpha:1.0],NSFontAttributeName : [MOTextTools regularFont:kShowGiftFont]}];
-
- NSRange redRange = [needStr rangeOfString:nameStr];
- if(redRange.location != NSNotFound){
- // 将最后三个字符的颜色设置为红色
- [attributedString addAttribute:NSForegroundColorAttributeName value:[MOTools colorWithHexString:@"#19FFB2" alpha:1.0] range:redRange];
- [attributedString addAttribute:NSFontAttributeName value:[MOTextTools mediumFont:kShowGiftFont] range:redRange];
- }
- return attributedString;
- }
- - (NSAttributedString *)colorfulStringWith:(NSString *)contentStr Num:(NSInteger)num name:(NSString *)nameStr{
-
- NSString *needStr;
-
- if(num == 0){
- needStr = contentStr;
- // 创建属性字符串,整体颜色为白色
- NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:needStr attributes:@{NSForegroundColorAttributeName: [MOTools colorWithHexString:@"#F4AEFF" alpha:1.0],NSFontAttributeName : [MOTextTools regularFont:kShowGiftFont]}];
- return attributedString;
- }
- else{
- NSString *numStr = [NSString stringWithFormat:@"%zd",num];
-
- needStr = contentStr;
-
- // 创建属性字符串,整体颜色为白色
- NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:needStr attributes:@{NSForegroundColorAttributeName: [MOTools colorWithHexString:@"#F4AEFF" alpha:1.0],NSFontAttributeName : [MOTextTools regularFont:kShowGiftFont]}];
-
- NSRange blueRange = [needStr rangeOfString:nameStr];
- if(blueRange.location != NSNotFound){
- // 将最后三个字符的颜色设置为红色
- [attributedString addAttribute:NSForegroundColorAttributeName value:[MOTools colorWithHexString:@"#19FFB2" alpha:1.0] range:blueRange];
- [attributedString addAttribute:NSFontAttributeName value:[MOTextTools mediumFont:kShowGiftFont] range:blueRange];
- }
-
- NSRange redRange = [needStr rangeOfString:numStr];
- if(redRange.location != NSNotFound){
- // 将最后三个字符的颜色设置为红色
- [attributedString addAttribute:NSForegroundColorAttributeName value:[MOTools colorWithHexString:@"#19FFB2" alpha:1.0] range:redRange];
- // 设置字体大小的属性
- [attributedString addAttribute:NSFontAttributeName value:[MOTextTools mediumFont:kShowGiftBigFont] range:redRange];
- }
- return attributedString;
- }
- }
- #pragma mark - Lazy
- - (UIView *)bgView{
- if(!_bgView){
- _bgView = [[UIView alloc] init];
- _bgView.backgroundColor = [UIColor clearColor];
- }
- return _bgView;
- }
- - (UIImageView *)bgImgView{
- if (!_bgImgView)
- {
- _bgImgView = [[UIImageView alloc] init];
- _bgImgView.contentMode = UIViewContentModeScaleToFill;
- [_bgImgView setImage:[UIImage imageNamed:@"icon_super_gift_bg_top"]];
- }
- return _bgImgView;
- }
- - (UIImageView *)headImgView{
- if (!_headImgView)
- {
- _headImgView = [[UIImageView alloc] init];
- _headImgView.contentMode = UIViewContentModeScaleAspectFill;
- [_headImgView setImage:[UIImage imageNamed:@"icon_mine_placeHolder"]];
- }
- return _headImgView;
- }
- - (UILabel *)contentLab{
- if (!_contentLab)
- {
- _contentLab = [[UILabel alloc] init];
- _contentLab.text = @"";
- _contentLab.font = [MOTextTools getTheFontWithSize:kShowGiftFont AndFontName:kNormalContentFontStr];
- _contentLab.textColor = [MOTools colorWithHexString:@"#FFC4C3" alpha:1.0];
- _contentLab.textAlignment = NSTextAlignmentRight;
- _contentLab.numberOfLines = 1;
-
- }
- return _contentLab;
- }
- - (UIScrollView *)contentScrollView{
- if(!_contentScrollView){
- _contentScrollView = [[UIScrollView alloc] init];
- _contentScrollView.contentSize = CGSizeMake(SCREENWIDTH, 24.0);
- _contentScrollView.showsHorizontalScrollIndicator = NO;
- }
- return _contentScrollView;
- }
- @end
|