// // YMRoomGiftBannerView.m // YUMI // // Created by YUMI on 2021/11/18. // #import "XPRoomGiftBannerView.h" ///Third #import ///Tool #import "ThemeColor+Room.h" ///Model #import "GiftReceiveInfoModel.h" #import "GiftInfoModel.h" #import "XPGiftBannerUserInfoModel.h" ///View #import "NetImageView.h" @interface XPRoomAvatarNickView : UIView /// @property (nonatomic,strong) NetImageView *avatImageView; /// @property (nonatomic,strong) UILabel *nickLabel; @end @implementation XPRoomAvatarNickView - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { [self initSubViews]; [self initSubViewConstraints]; } return self; } - (void)layoutSubviews { [super layoutSubviews]; self.avatImageView.layer.cornerRadius = self.avatImageView.bounds.size.width / 2; } #pragma mark - Private Method - (void)initSubViews { [self addSubview:self.avatImageView]; [self addSubview:self.nickLabel]; } - (void)initSubViewConstraints { [self mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.mas_equalTo(self.nickLabel.mas_bottom); }]; [self.avatImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.top.mas_equalTo(self); make.height.mas_equalTo(self.avatImageView.mas_width); }]; [self.nickLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.mas_equalTo(self); make.top.mas_equalTo(self.avatImageView.mas_bottom).offset(2); make.width.mas_lessThanOrEqualTo(80); }]; } #pragma mark - Getters And Setters - (NetImageView *)avatImageView { if (!_avatImageView) { NetImageConfig * config = [[NetImageConfig alloc]init]; config.imageType = ImageTypeUserIcon; _avatImageView = [[NetImageView alloc] initWithConfig:config]; _avatImageView.userInteractionEnabled = YES; _avatImageView.layer.masksToBounds = YES; } return _avatImageView; } - (UILabel *)nickLabel { if (!_nickLabel) { _nickLabel = [[UILabel alloc] init]; _nickLabel.font = [UIFont systemFontOfSize:12]; _nickLabel.textColor = [UIColor whiteColor]; _nickLabel.textAlignment = NSTextAlignmentCenter; } return _nickLabel; } @end @interface XPRoomGiftBannerView () ///背景图 @property (nonatomic,strong) UIImageView *backImageView; ///赠送 @property (nonatomic,strong) XPRoomAvatarNickView *sendView; ///接收者 @property (nonatomic,strong) XPRoomAvatarNickView *receiveView; ///礼物 @property (nonatomic,strong) XPRoomAvatarNickView *giftView; ///光圈 @property (nonatomic,strong) UIImageView *lightImageView; ///礼物的个数 @property (nonatomic,strong) UILabel *giftCountLabel; ///赠送 @property (nonatomic,strong) UILabel *sendLabel; @end @implementation XPRoomGiftBannerView - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self initSubViews]; [self initSubViewConstraints]; } return self; } #pragma mark - Private Method - (void)initSubViews { [self addSubview:self.backImageView]; [self.backImageView addSubview:self.sendView]; [self.backImageView addSubview:self.sendLabel]; [self.backImageView addSubview:self.receiveView]; [self.backImageView addSubview:self.lightImageView]; [self.backImageView addSubview:self.giftView]; [self.backImageView addSubview:self.giftCountLabel]; } - (void)initSubViewConstraints { [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self); }]; [self.sendView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.backImageView).offset(49); make.top.equalTo(self).offset(49); make.width.mas_equalTo(40); }]; [self.sendLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.sendView.mas_right).offset(24); make.centerY.equalTo(self.sendView); }]; [self.receiveView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.sendLabel.mas_right).offset(24); make.centerY.width.equalTo(self.sendView); }]; [self.lightImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.receiveView.mas_right).offset(4); make.centerY.equalTo(self.sendView); make.width.height.equalTo(@100); }]; [self.giftView mas_makeConstraints:^(MASConstraintMaker *make) { make.width.equalTo(self.sendView); make.center.equalTo(self.lightImageView); }]; [self.giftCountLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(self.giftView); make.right.equalTo(self.backImageView).offset(-36); }]; } - (void)startLightCircleAnimation { CABasicAnimation * animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; animation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ]; animation.duration = 1; animation.cumulative = YES; animation.repeatCount = MAXFLOAT; [self.lightImageView.layer addAnimation:animation forKey:@"rotationAnimation"]; } #pragma mark - Public Method - (void)resetData { self.sendView.avatImageView.image = nil; self.sendView.nickLabel.text = nil; self.receiveView.avatImageView.image = nil; self.receiveView.nickLabel.text = nil; self.giftView.avatImageView.image = nil; self.giftView.nickLabel.text = nil; self.giftCountLabel.text = nil; [self.lightImageView stopAnimating]; } - (void)configGiftBanner:(GiftReceiveInfoModel *)receiveInfo{ if (receiveInfo) { [self startLightCircleAnimation]; NSInteger giftTotal = receiveInfo.giftGolds; GiftInfoModel *giftInfo = receiveInfo.gift; self.sendView.avatImageView.imageUrl = receiveInfo.sendUserAvatar; self.sendView.nickLabel.text = receiveInfo.sendUserNick; self.receiveView.nickLabel.text = receiveInfo.recvUserNick; self.receiveView.avatImageView.imageUrl = receiveInfo.recvUserAvatar; self.giftView.avatImageView.imageUrl = giftInfo.giftUrl; self.giftView.nickLabel.text = giftInfo.giftName; self.giftCountLabel.text = [NSString stringWithFormat:@" x %ld",receiveInfo.giftNum]; [self configAnimateImageByGiftTotal:giftTotal]; } } - (void)configGiftBanner:(GiftReceiveInfoModel *)receiveInfo users:(NSArray *)users { if (receiveInfo) { [self startLightCircleAnimation]; NSInteger giftTotal = 0; GiftInfoModel *giftInfo = receiveInfo.gift; self.sendView.avatImageView.imageUrl = receiveInfo.avatar ?: receiveInfo.sendUserAvatar; self.sendView.nickLabel.text = receiveInfo.nick; self.giftView.avatImageView.imageUrl = giftInfo.giftUrl; self.giftView.nickLabel.text = giftInfo.giftName; self.giftCountLabel.text = [NSString stringWithFormat:@" x %ld",receiveInfo.giftNum]; /*1. 普通单人 targetUid 2. 普通多人 targetUsers 3. 普通全麦 targetUids 4. 福袋 targetUid 不懂请参考此协议*/ if (receiveInfo.targetUsers.count > 0) { if (receiveInfo.isBatch) { // 非全麦 多人送礼 NSString *subTitle = @""; NSArray *targetUids = receiveInfo.targetUsers; for (GiftReceiveUserInfoModel *targetUid in targetUids) { NSInteger position = [self findPositionByUid:targetUid.uid users:users]; if (position == 0) { continue; // 收礼人已经下麦了, 则不展示 } position += 1; if (subTitle.length > 0) { subTitle = [subTitle stringByAppendingFormat:YMLocalizedString(@"XPRoomGiftBannerView0"), position]; } else { subTitle = [subTitle stringByAppendingFormat:YMLocalizedString(@"XPRoomGiftBannerView1"), position]; } } if (receiveInfo.targetUsers.count == 1) { self.receiveView.nickLabel.text = [receiveInfo.targetUsers firstObject].nick; self.receiveView.avatImageView.imageUrl = [receiveInfo.targetUsers firstObject].avatar; } else { self.receiveView.avatImageView.image = [UIImage imageNamed:@"common_avatar"]; self.receiveView.nickLabel.text = subTitle; } giftTotal = receiveInfo.giftNum * giftInfo.goldPrice * receiveInfo.targetUsers.count; } else { // 全麦 self.receiveView.avatImageView.image = [UIImage imageNamed:@"common_avatar"]; self.receiveView.nickLabel.text = YMLocalizedString(@"XPRoomGiftBannerView2"); giftTotal = receiveInfo.giftNum * giftInfo.goldPrice * receiveInfo.targetUsers.count; } }else if (receiveInfo.targetUids.count > 0) {///根据协议 普通情况 if (receiveInfo.isBatch) { NSString *subTitle = @""; NSArray *targetUids = receiveInfo.targetUids; for (NSString * targetUid in targetUids) { NSInteger position = [self findPositionByUid:targetUid.integerValue users:users]; if (position == 0) { continue; // 收礼人已经下麦了, 则不展示 } position+= 1; if (subTitle.length > 0) { subTitle = [subTitle stringByAppendingFormat:YMLocalizedString(@"XPRoomGiftBannerView3"), position]; } else { subTitle = [subTitle stringByAppendingFormat:YMLocalizedString(@"XPRoomGiftBannerView4"), position]; } } self.receiveView.avatImageView.image = [UIImage imageNamed:@"common_avatar"]; self.receiveView.nickLabel.text = subTitle; giftTotal = receiveInfo.giftNum * giftInfo.goldPrice * receiveInfo.targetUids.count; } else { // 全麦 self.receiveView.avatImageView.image = [UIImage imageNamed:@"common_avatar"]; self.receiveView.nickLabel.text = YMLocalizedString(@"XPRoomGiftBannerView5"); giftTotal = receiveInfo.giftNum * giftInfo.goldPrice * receiveInfo.targetUids.count; } }else { self.receiveView.avatImageView.imageUrl = receiveInfo.targetAvatar; self.receiveView.nickLabel.text = receiveInfo.targetNick; giftTotal = receiveInfo.giftNum * giftInfo.goldPrice; } [self configAnimateImageByGiftTotal:giftTotal]; } } - (void)configAnimateImageByGiftTotal:(NSInteger)giftTotal { if (giftTotal >= 520 && giftTotal < 4999) { self.backImageView.image = [UIImage imageNamed:@"room_gift_banner_low_bg"]; }else if (giftTotal >= 4999 && giftTotal < 9999) { self.backImageView.image = [UIImage imageNamed:@"room_gift_banner_middle_bg"]; }else if (giftTotal >= 9999) { self.backImageView.image = [UIImage imageNamed:@"room_gift_banner_high_bg"]; } } - (int)findPositionByUid:(NSInteger)uid users:(NSArray *)users{ if (uid > 0) { for (XPGiftBannerUserInfoModel *userInfo in users) { if (userInfo.uid == uid) { return userInfo.position; } } } return 0; } #pragma mark - Getters And Setters - (UIImageView *)backImageView { if (!_backImageView) { _backImageView = [[UIImageView alloc] init]; _backImageView.userInteractionEnabled = YES; } return _backImageView; } - (XPRoomAvatarNickView *)sendView { if (!_sendView) { _sendView = [[XPRoomAvatarNickView alloc] init]; } return _sendView; } - (XPRoomAvatarNickView *)receiveView { if (!_receiveView) { _receiveView = [[XPRoomAvatarNickView alloc] init]; } return _receiveView; } - (XPRoomAvatarNickView *)giftView { if (!_giftView) { _giftView = [[XPRoomAvatarNickView alloc] init]; } return _giftView; } - (UILabel *)sendLabel { if (!_sendLabel) { _sendLabel = [[UILabel alloc] init]; _sendLabel.text = YMLocalizedString(@"XPRoomGiftBannerView6"); _sendLabel.font = [UIFont systemFontOfSize:12]; _sendLabel.textColor = [UIColor whiteColor]; } return _sendLabel; } - (UIImageView *)lightImageView { if (!_lightImageView) { _lightImageView = [[UIImageView alloc] init]; _lightImageView.userInteractionEnabled = YES; _lightImageView.image = [UIImage imageNamed:@"room_gift_banner_light"]; } return _lightImageView; } - (UILabel *)giftCountLabel { if (!_giftCountLabel) { _giftCountLabel = [[UILabel alloc] init]; _giftCountLabel.font = [UIFont systemFontOfSize:20.f weight:UIFontWeightBold]; _giftCountLabel.textColor = [DJDKMIMOMColor animationGiftNumberColor]; } return _giftCountLabel; } @end