Files
yinmeng-ios/xplan-ios/Main/Room/View/BaseUIContainerView/XPRoomGiftBannerView.m
2021-11-19 14:13:08 +08:00

357 lines
11 KiB
Objective-C

//
// XPRoomGiftBannerView.m
// xplan-ios
//
// Created by 冯硕 on 2021/11/18.
//
#import "XPRoomGiftBannerView.h"
///Third
#import <Masonry/Masonry.h>
#import <pop/pop.h>
///Tool
#import "ThemeColor+Room.h"
#import "XPGiftStorage.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) {
_avatImageView = [[NetImageView alloc] init];
_avatImageView.userInteractionEnabled = YES;
_avatImageView.imageType = ImageTypeUserIcon;
_avatImageView.layer.masksToBounds = YES;
}
return _avatImageView;
}
- (UILabel *)nickLabel {
if (!_nickLabel) {
_nickLabel = [[UILabel alloc] init];
_nickLabel.font = [UIFont systemFontOfSize:12];
_nickLabel.textColor = [ThemeColor mainTextColor];
_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];
[self startLightCircleAnimation];
}
- (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;
}
- (void)configGiftBanner:(GiftReceiveInfoModel *)receiveInfo users:(NSArray<XPGiftBannerUserInfoModel *> *)users {
if (receiveInfo) {
NSInteger giftTotal = 0;
GiftInfoModel *giftInfo = receiveInfo.gift == nil ? receiveInfo.giftInfo : receiveInfo.gift;
if (!giftInfo) {
giftInfo = [[XPGiftStorage shareStorage] findGiftInfo:receiveInfo.giftId];
}
self.sendView.avatImageView.imageUrl = receiveInfo.avatar;
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:@",%ld麦", position];
} else {
subTitle = [subTitle stringByAppendingFormat:@"%ld麦", 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:@"room_gift_all_mic"];
self.receiveView.nickLabel.text = subTitle;
}
giftTotal = receiveInfo.giftNum * giftInfo.goldPrice * receiveInfo.targetUsers.count;
} else { // 全麦
self.receiveView.avatImageView.image = [UIImage imageNamed:@"room_gift_all_mic"];
self.receiveView.nickLabel.text = @"全麦";
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:@",%ld麦", position];
} else {
subTitle = [subTitle stringByAppendingFormat:@"%ld麦", position];
}
}
self.receiveView.avatImageView.image = [UIImage imageNamed:@"room_gift_all_mic"];
self.receiveView.nickLabel.text = subTitle;
giftTotal = receiveInfo.giftNum * giftInfo.goldPrice * receiveInfo.targetUids.count;
} else { // 全麦
self.receiveView.avatImageView.image = [UIImage imageNamed:@"room_gift_all_mic"];
self.receiveView.nickLabel.text = @"全麦";
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<XPGiftBannerUserInfoModel *> *)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 = @"赠送";
_sendLabel.font = [UIFont systemFontOfSize:12];
_sendLabel.textColor = [ThemeColor mainTextColor];
}
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 = [ThemeColor mainTextColor];
}
return _giftCountLabel;
}
@end