| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- //
- // MOLevelUpNoticeView.m
- // MiMoLive
- //
- // Created by SuperC on 2025/6/27.
- //
- #import "MOLevelUpNoticeView.h"
- #import <YYKit/YYLabel.h>
- @interface MOLevelUpNoticeView ()
- @property (nonatomic, strong) YYLabel *contentLab;
- @end
- @implementation MOLevelUpNoticeView
- - (instancetype)initWithFrame:(CGRect)frame{
- self = [super initWithFrame:frame];
- if (self)
- {
- self.backgroundColor = [UIColor clearColor];
- [self setupUI];
- }
- return self;
- }
- - (void)setupUI{
- self.clipsToBounds = YES;
-
- CGFloat bgImgWidth = SCREENWIDTH - 30.0 * 2.0;
- CGFloat bgImgHeight = bgImgWidth * 28.0 / 314.0; // 28.0 是原图的高度,314.0 是原图的宽度
- [self addSubview:self.bgImgView];
- [self.bgImgView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.height.equalTo(@(bgImgHeight));
- make.width.equalTo(@(bgImgWidth));
- make.centerY.equalTo(self);
- make.centerX.equalTo(self);
- }];
-
- [self addSubview:self.contentScrollView];
- [self.contentScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(self).offset(12);
- make.right.equalTo(self).offset(-12.0);
- make.centerY.equalTo(self).offset(0.0);
- make.height.equalTo(@20.0);
- }];
-
- CGFloat width = SCREENWIDTH - 43.0 * 2.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.width.greaterThanOrEqualTo(@(width));
- make.centerY.equalTo(self).offset(0.0);
- make.height.equalTo(@20.0);
- }];
- }
- - (void)setViewModel:(MORtmEntity *)viewModel{
- _viewModel = viewModel;
-
- CGFloat bgImgWidth = SCREENWIDTH - 30.0 * 2.0;
- CGFloat bgImgHeight = bgImgWidth * 28.0 / 314.0; // 28.0 是原图的高度,314.0 是原图的宽度
-
- MORtmJosnEntity *jsonEntity = (MORtmJosnEntity *)viewModel.data;
- WEAKSELF
- if(jsonEntity.type == 26){// 用户升级通知框
- MORtmUser *vipUser = jsonEntity.roomUser;
- MORtmUserLevelUpgrade *userLevel = jsonEntity.levelNotice;
-
- if(userLevel.effectsUrl.length > 0){
- [self.bgImgView sd_setImageWithURL:[NSURL URLWithString:userLevel.effectsUrl] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
- CGFloat theImageHeight = kScaleWidth(28.0);
- if(image.size.width > 0){
- theImageHeight = image.size.height * (SCREENWIDTH - 30.0 * 2.0) / image.size.width;
- }
-
- kMainThreadBlock((^{
- [self.bgImgView mas_updateConstraints:^(MASConstraintMaker *make) {
- make.height.equalTo(@(theImageHeight));
- }];
- }))
- }];
- }
- else{
- [self.bgImgView setImage:[UIImage imageNamed:@"icon_rtm_level_up"]];
- [self.bgImgView mas_updateConstraints:^(MASConstraintMaker *make) {
- make.height.equalTo(@(bgImgHeight));
- }];
- }
-
- self.contentLab.attributedText = [MOTextTools createTheLevelUpNoticeWith:viewModel];
- }
- }
- - (void)toJudgeNeedAnimationScrollView{
- // 取消之前的动画
- [self.contentScrollView.layer removeAllAnimations];
- self.contentScrollView.contentOffset = CGPointZero;
-
- NSMutableAttributedString *tipAttri;
- MORtmJosnEntity *jsonEntity = (MORtmJosnEntity *)self.viewModel.data;
- if(jsonEntity.type == 26){// 用户升级通知框
- MORtmUser *vipUser = jsonEntity.roomUser;
- MORtmUserLevelUpgrade *userLevel = jsonEntity.levelNotice;
-
- tipAttri = [MOTextTools creteTextWith:self.viewModel];
- }
-
- if(tipAttri.length == 0){
- return;
- }
-
- CGFloat contentWidth = [MOTextTools getWidthWith:self.viewModel] + 30.0;
- CGFloat realSpacing = SCREENWIDTH - 30.0 * 2.0 - 12.0 * 2.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;
- }];
- }
- }
- #pragma mark - Lazy
- - (UIImageView *)bgImgView{
- if (!_bgImgView)
- {
- _bgImgView = [[UIImageView alloc] init];
- _bgImgView.contentMode = UIViewContentModeScaleToFill;
- [_bgImgView setImage:[UIImage imageNamed:@"icon_rtm_level_up"]];
- }
- return _bgImgView;
- }
- - (YYLabel *)contentLab{
- if (!_contentLab){
- _contentLab = [[YYLabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 20.0)];
- _contentLab.text = @"";
- _contentLab.font = [MOTextTools regularFont:12.0];
- _contentLab.textColor = [MOTools colorWithHexString:@"#FFC4C3" alpha:1.0];
- _contentLab.textAlignment = NSTextAlignmentLeft;
- _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
|