// // XPReceiveRedPacketView.m // xplan-ios // // Created by GreenLand on 2022/9/2. // #import "XPReceiveRedPacketView.h" #import "XPReceiveRedPacketModel.h" #import "NSObject+MJExtension.h" #import "ThemeColor.h" #import #import "NetImageView.h" #import "TTPopUp.h" #import "Api+RedPacket.h" #import "XCHUDTool.h" #import "XPRedPacketResultModel.h" #import "XPOpenRedPacketCell.h" @interface XPReceiveRedPacketView() @property (nonatomic, strong) XPReceiveRedPacketModel *redPacketModel; @property (nonatomic, strong) XPRedPacketResultModel *resultData; @property (nonatomic, strong) UIView *alphaView; ///背景 @property (nonatomic, strong) UIImageView *bgImageView; ///发送者头像 @property (nonatomic, strong) NetImageView *sendAvatar; ///发送者昵称 @property (nonatomic, strong) UILabel *sendNickLabel; ///收到的祝福语 @property (nonatomic, strong) UILabel *sendWishLabel; ///开红包按钮 @property (nonatomic, strong) UIButton *openButton; ///关闭 @property (nonatomic, strong) UIButton *closeBtn; ///领取到容器 @property (nonatomic, strong) UIView *diamondContentView; @property (nonatomic, strong) UIImageView *diamondImage; @property (nonatomic, strong) UILabel *diamondLabel; ///是否领到红包文案 @property (nonatomic, strong) UILabel *getLabel; ///红包领取情况 @property (nonatomic, strong) UILabel *openSituationLabel; /// @property (nonatomic, strong) UITableView *tableView; @end @implementation XPReceiveRedPacketView - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { [self initSubViews]; [self initSubViewConstraints]; } return self; } #pragma mark - Private Method - (void)initSubViews { [self addSubview:self.alphaView]; [self addSubview:self.bgImageView]; [self addSubview:self.sendAvatar]; [self addSubview:self.sendNickLabel]; [self addSubview:self.sendWishLabel]; [self addSubview:self.openButton]; [self addSubview:self.closeBtn]; } - (void)initSubViewConstraints { [self.alphaView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.mas_equalTo(0); }]; [self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.width.mas_equalTo(310); make.height.mas_equalTo(450); make.centerX.centerY.mas_equalTo(self); }]; [self.closeBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self.bgImageView.mas_bottom).mas_offset(25); make.height.width.mas_equalTo(35); make.centerX.mas_equalTo(self.bgImageView); }]; [self.sendAvatar mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.mas_equalTo(self.bgImageView); make.top.mas_equalTo(self.bgImageView).mas_offset(71); make.width.height.mas_equalTo(88); }]; [self.sendNickLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.mas_equalTo(self.bgImageView); make.top.mas_equalTo(self.sendAvatar.mas_bottom).mas_offset(9); make.height.mas_equalTo(18); make.left.mas_equalTo(30); }]; [self.sendWishLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.mas_equalTo(self.bgImageView); make.left.mas_equalTo(30); make.top.mas_equalTo(self.sendNickLabel.mas_bottom).mas_offset(30); }]; [self.openButton mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.mas_equalTo(self.bgImageView); make.width.height.mas_equalTo(107); make.bottom.mas_equalTo(self.bgImageView).mas_offset(-75); }]; } - (void)setDict:(NSDictionary *)dict { XPReceiveRedPacketModel *data = [XPReceiveRedPacketModel modelWithDictionary:dict]; self.redPacketModel = data; self.sendAvatar.imageUrl = data.sendUserAvatar; self.sendWishLabel.text = data.redEnvelopeMessage; if (data.redEnvelopeType == 1) { self.sendNickLabel.text = data.sendUserNick; } else { NSString *nick = data.sendUserNick; if (data.sendUserNick.length > 6) { nick = [NSString stringWithFormat:@"%@...", [data.sendUserNick substringToIndex:6]]; } self.sendNickLabel.text = [NSString stringWithFormat:@"%@厅内的%@", data.roomTitle, nick]; [self.openButton setImage:[UIImage imageNamed:@"redPacket_openRoom"] forState:UIControlStateNormal]; } } #pragma mark - action - (void)onOpenButtonClick:(UIButton *)sender { //判断是否为房内红包 //1、房内红包 //1.1 开红包动画 CABasicAnimation *transformAnima = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"]; transformAnima.toValue = [NSNumber numberWithFloat:M_PI]; transformAnima.duration = 0.3; transformAnima.cumulative = NO; transformAnima.autoreverses = YES; transformAnima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]; sender.layer.zPosition = 5; sender.layer.zPosition = sender.layer.frame.size.width / 2.f; transformAnima.repeatCount = 1; [sender.layer addAnimation:transformAnima forKey:@"rotationAnimationY"]; //1.2 网络请求 [Api openRedPacket:^(BaseModel * _Nullable data, NSInteger code, NSString * _Nullable msg) { if (code != 200) { [XCHUDTool showErrorWithMessage:msg]; return;; } XPRedPacketResultModel *model = [XPRedPacketResultModel modelWithJSON:data.data]; self.resultData = model; [self showRedPacketResultViewWithResultData:model]; } redEnvelopeId:self.redPacketModel.redEnvelopeId]; } ///显示红包结果 - (void)showRedPacketResultViewWithResultData:(XPRedPacketResultModel *)resultData { self.openButton.hidden = YES; [self addSubview:self.getLabel]; [self addSubview:self.openSituationLabel]; [self addSubview:self.tableView]; if (resultData.redEnvelopeState == RedPacketState_Success) {//抢到红包 [self addSubview:self.diamondContentView]; [self.diamondContentView addSubview:self.diamondImage]; [self.diamondContentView addSubview:self.diamondLabel]; [self.diamondContentView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self.bgImageView).mas_offset(30); make.centerX.mas_equalTo(self); make.height.mas_equalTo(27); }]; [self.diamondImage mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.diamondContentView); make.centerY.mas_equalTo(self.diamondContentView); make.width.height.mas_equalTo(24); }]; [self.diamondLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.diamondImage.mas_right).mas_offset(6); make.centerY.mas_equalTo(self.diamondContentView); make.right.top.mas_equalTo(self.diamondContentView); }]; [self.getLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.mas_equalTo(self); make.top.mas_equalTo(self.diamondContentView.mas_bottom).mas_offset(10); make.height.mas_equalTo(12); }]; self.diamondLabel.text = resultData.currentUserAmount; self.getLabel.text = @"已存入钱包,请到我的收益确认"; } else if (resultData.redEnvelopeState == RedPacketState_Remain_Zero) {//抢光了 self.getLabel.text = @"抢完了,下次早点来哦~"; [self.getLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.mas_equalTo(self); make.top.mas_equalTo(self.bgImageView).mas_offset(59); make.height.mas_equalTo(12); }]; } [self.sendAvatar mas_remakeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.bgImageView).mas_offset(65); make.top.mas_equalTo(self.bgImageView).mas_offset(128); make.width.height.mas_equalTo(30); }]; self.sendAvatar.layer.borderWidth = 1; self.sendAvatar.layer.cornerRadius = 15; [self.sendNickLabel mas_remakeConstraints:^(MASConstraintMaker *make) { make.centerY.mas_equalTo(self.sendAvatar); make.height.mas_equalTo(18); make.left.mas_equalTo(self.sendAvatar.mas_right).mas_offset(7); }]; [self.sendWishLabel mas_remakeConstraints:^(MASConstraintMaker *make) { make.centerX.mas_equalTo(self.bgImageView); make.left.mas_equalTo(30); make.top.mas_equalTo(self.sendNickLabel.mas_bottom).mas_offset(15); }]; [self.openSituationLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self.sendWishLabel.mas_bottom).mas_offset(19); make.height.mas_equalTo(10); make.right.mas_equalTo(self.bgImageView).mas_offset(-38); }]; [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self.openSituationLabel.mas_bottom).mas_offset(5); make.left.right.mas_equalTo(self.bgImageView).inset(20); make.height.mas_equalTo(205); }]; self.bgImageView.image = [UIImage imageNamed:@"redPacket_result_bg"]; self.sendAvatar.imageUrl = resultData.redEnvelopeVO.userVO.avatar; NSString *nick = resultData.redEnvelopeVO.userVO.nick; if (resultData.redEnvelopeVO.userVO.nick.length > 6) { nick = [NSString stringWithFormat:@"%@...", [resultData.redEnvelopeVO.userVO.nick substringToIndex:6]]; } NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] init]; NSMutableAttributedString *nickAttr = [[NSMutableAttributedString alloc] initWithString:nick attributes:@{NSForegroundColorAttributeName : UIColorFromRGB(0xFDCD00)}]; NSMutableAttributedString *redPacketStr = [[NSMutableAttributedString alloc] initWithString:@"的红包" attributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}]; [attribute appendAttributedString:nickAttr]; [attribute appendAttributedString:redPacketStr]; self.sendNickLabel.attributedText = attribute; self.sendWishLabel.text = resultData.redEnvelopeVO.message; self.openSituationLabel.text = [NSString stringWithFormat:@"领取%@/%@个", resultData.redEnvelopeVO.pickNum, resultData.redEnvelopeVO.totalNum]; [self.tableView reloadData]; } #pragma mark - tableviewDatasource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.resultData.redEnvelopeItemVOs.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ XPOpenRedPacketCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XPOpenRedPacketCell class]) forIndexPath:indexPath]; cell.selectionStyle = UITableViewCellSelectionStyleNone; XPRedPacketPersonReceiveModel *model = [self.resultData.redEnvelopeItemVOs objectAtIndex:indexPath.row]; cell.model = model; cell.backgroundColor = [UIColor clearColor]; return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 50; } #pragma mark - action - (void)onCloseButtonClick:(UIButton *)sender { [self removeFromSuperview]; } #pragma mark - getter - (UIView *)alphaView { if (!_alphaView) { _alphaView = [[UIView alloc] init]; _alphaView.backgroundColor = UIColorRGBAlpha(0x171213, 0.5); } return _alphaView; } - (UIImageView *)bgImageView { if (!_bgImageView) { _bgImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"redPacket_receive_bg"]]; } return _bgImageView; } - (NetImageView *)sendAvatar { if (!_sendAvatar) { _sendAvatar = [[NetImageView alloc] init]; _sendAvatar.layer.masksToBounds = YES; _sendAvatar.layer.cornerRadius = 44; _sendAvatar.layer.borderWidth = 2; _sendAvatar.layer.borderColor = [UIColor whiteColor].CGColor; } return _sendAvatar; } - (UIView *)sendNickLabel { if (!_sendNickLabel) { UILabel *label = [[UILabel alloc] init]; label.font = [UIFont systemFontOfSize:18]; label.textColor = UIColorFromRGB(0xF6E36F); label.textAlignment = NSTextAlignmentCenter; _sendNickLabel = label; } return _sendNickLabel; } - (UILabel *)sendWishLabel { if (!_sendWishLabel) { UILabel *label = [[UILabel alloc] init]; label.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium]; label.textColor = [UIColor whiteColor]; label.numberOfLines = 0; label.textAlignment = NSTextAlignmentCenter; _sendWishLabel = label; } return _sendWishLabel; } - (UIButton *)openButton { if (!_openButton) { _openButton = [[UIButton alloc] init]; [_openButton setImage:[UIImage imageNamed:@"redPacket_open"] forState:UIControlStateNormal]; [_openButton addTarget:self action:@selector(onOpenButtonClick:) forControlEvents:UIControlEventTouchUpInside]; } return _openButton; } - (UIButton *)closeBtn { if (!_closeBtn) { _closeBtn = [[UIButton alloc] init]; [_closeBtn setImage:[UIImage imageNamed:@"common_close_white"] forState:UIControlStateNormal]; [_closeBtn addTarget:self action:@selector(onCloseButtonClick:) forControlEvents:UIControlEventTouchUpInside]; } return _closeBtn; } - (UIView *)diamondContentView { if (!_diamondContentView) { _diamondContentView = [[UIView alloc] init]; } return _diamondContentView; } - (UIImageView *)diamondImage { if (!_diamondImage) { _diamondImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"common_diamond"]]; } return _diamondImage; } - (UILabel *)diamondLabel { if (!_diamondLabel) { UILabel *label = [[UILabel alloc] init]; label.font = [UIFont systemFontOfSize:36 weight:UIFontWeightMedium]; label.textColor = UIColorFromRGB(0xE12D31); _diamondLabel = label; } return _diamondLabel; } - (UILabel *)getLabel { if (!_getLabel) { UILabel *label = [[UILabel alloc] init]; label.font = [UIFont systemFontOfSize:12]; label.textColor = UIColorFromRGB(0xCF9380); label.textAlignment = NSTextAlignmentCenter; label.text = @"抢完了,下次早点来哦~"; _getLabel = label; } return _getLabel; } - (UILabel *)openSituationLabel { if (!_openSituationLabel) { UILabel *label = [[UILabel alloc] init]; label.font = [UIFont systemFontOfSize:10 weight:UIFontWeightMedium]; label.textColor = UIColorRGBAlpha(0xffffff, 0.5); _openSituationLabel = label; } return _openSituationLabel; } - (UITableView *)tableView { if (!_tableView) { _tableView = [[UITableView alloc] init]; [_tableView registerClass:[XPOpenRedPacketCell class] forCellReuseIdentifier:NSStringFromClass([XPOpenRedPacketCell class])]; _tableView.delegate = self; _tableView.dataSource = self; _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; _tableView.layer.cornerRadius = 20; _tableView.layer.masksToBounds = YES; _tableView.backgroundColor = UIColorFromRGB(0xCC2247); } return _tableView; } @end