// // YOUPAILZChatRoomGiftView.m // VQU // // Created by CY on 2021/11/17. // Copyright © 2021 MS. All rights reserved. // #import "YOUPAILZChatRoomGiftView.h" @interface YOUPAILZChatRoomGiftView () @property (nonatomic, weak)UILabel *youpaipcountL; @property (nonatomic, strong) NSTimer *youpaiptimer; @property (nonatomic, assign) NSInteger youpaiptimerCount; @property (nonatomic, strong,readwrite) YOUPAILZLiveGIftModel *youpaipgiftModel; @end @implementation YOUPAILZChatRoomGiftView + (instancetype)youpaifgiftViewWithModel:(YOUPAILZLiveGIftModel *)model{ return [[self alloc] initWithModel:model]; } - (instancetype)initWithModel:(YOUPAILZLiveGIftModel *)model{ if (self = [super init]) { _youpaipgiftModel = model; [self youpaifinitUI]; } return self; } - (void)youpaifinitUI{ UIImageView *bgV = [[UIImageView alloc] init]; UIImage *bgimg = [UIImage imageNamed:@"vqu_images_chatroom_giftbox_bg"]; bgV.image = [bgimg resizableImageWithCapInsets:UIEdgeInsetsMake(0, 181, 0, 0) resizingMode:UIImageResizingModeStretch]; [self addSubview:bgV]; [bgV mas_makeConstraints:^(MASConstraintMaker *make) { make.left.top.bottom.right.offset(0.0f); make.height.offset(40.0f); // make.size.mas_offset(CGSizeMake(182.0f, 40.0f)); }]; UIImageView *avatarImgV = [[UIImageView alloc] init]; avatarImgV.contentMode = UIViewContentModeScaleAspectFill; avatarImgV.layer.cornerRadius = 17.0f; avatarImgV.clipsToBounds = YES; [avatarImgV sd_setImageWithURL:[LCTools getImageUrlWithAddress:self.youpaipgiftModel.youpaipavatar]]; [self addSubview:avatarImgV]; [avatarImgV mas_makeConstraints:^(MASConstraintMaker *make) { make.left.offset(4.0f); make.centerY.equalTo(self); make.size.mas_offset(CGSizeMake(34.0f, 34.0f)); }]; UILabel *from_nicknameL = [[UILabel alloc] init]; from_nicknameL.font = LCFont(12.0f); from_nicknameL.textColor = [UIColor whiteColor]; from_nicknameL.text = self.youpaipgiftModel.youpaipnickName; [self addSubview:from_nicknameL]; [from_nicknameL mas_makeConstraints:^(MASConstraintMaker *make) { make.top.offset(4.5f); make.left.equalTo(avatarImgV.mas_right).offset(5.0f); make.height.offset(13.0f); make.width.offset(82.0f); }]; UILabel *to_nicknameL = [[UILabel alloc] init]; to_nicknameL.font = LCBoldFont(12.0f); to_nicknameL.textColor = [UIColor whiteColor]; to_nicknameL.attributedText = [LCTools setRichTextWithTitle:@"送给 " subTitle:self.youpaipgiftModel.youpaipto_nickName?:@"" titleColor:[[UIColor whiteColor] colorWithAlphaComponent:0.6f] subTitleColor:[UIColor whiteColor] titleFontSize:9.0f subTitleFontSize:12.0f]; [self addSubview:to_nicknameL]; [to_nicknameL mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(from_nicknameL.mas_bottom).offset(6.5f); make.left.equalTo(avatarImgV.mas_right).offset(5.0f); make.height.offset(13.0f); make.width.offset(82.0f); }]; UIImageView *giftImgV = [[UIImageView alloc] init]; [giftImgV sd_setImageWithURL:[LCTools getImageUrlWithAddress:self.youpaipgiftModel.youpaipgiftImage]]; [self addSubview:giftImgV]; [giftImgV mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(from_nicknameL.mas_right).offset(11.0f); make.centerY.equalTo(self); make.size.mas_offset(CGSizeMake(34.0f, 34.0f)); }]; UILabel *youpaipcountL = [[UILabel alloc] init]; youpaipcountL.font = LCBoldFont(30.0f); youpaipcountL.textColor = [UIColor whiteColor]; youpaipcountL.text = [NSString stringWithFormat:@"x%@",@(self.youpaipgiftModel.youpaipgiftCount)]; [self addSubview:youpaipcountL]; self.youpaipcountL = youpaipcountL; [youpaipcountL mas_makeConstraints:^(MASConstraintMaker *make) { make.top.bottom.right.offset(0.0f); make.left.equalTo(giftImgV.mas_right).offset(14.0f); }]; } - (void)youpaifshowAnimation{ [self youpaifstartTimer]; if (self != nil && self.superview != nil) { [self.superview setNeedsUpdateConstraints]; [self mas_updateConstraints:^(MASConstraintMaker *make) { make.left.offset(14.0f); }]; } [UIView animateWithDuration:0.25f animations:^{ [self.superview layoutIfNeeded]; }]; } - (void)youpaifhideAnimation{ if (self != nil && self.superview != nil) { [self.superview setNeedsUpdateConstraints]; [self mas_updateConstraints:^(MASConstraintMaker *make) { make.left.offset(-200.0f); }]; } [UIView animateWithDuration:0.25f animations:^{ [self.superview layoutIfNeeded]; } completion:^(BOOL finished) { if ([self.delegate respondsToSelector:@selector(youpaifanimatiomFinishWithView:)]) { [self.delegate youpaifanimatiomFinishWithView:self]; } }]; } - (void)youpaifupdateGiftCount:(NSInteger)count{ [self youpaifstopTimer]; self.youpaipgiftModel.youpaipgiftCount += count; self.youpaipcountL.text = [NSString stringWithFormat:@"x %@",@(self.youpaipgiftModel.youpaipgiftCount)]; [self youpaifstartTimer]; } - (void)youpaifstartTimer{ self.youpaiptimerCount = 2; self.youpaiptimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(youpaifcountDown) userInfo:nil repeats:YES]; [[NSRunLoop currentRunLoop] addTimer:self.youpaiptimer forMode:NSRunLoopCommonModes]; } - (void)youpaifstopTimer{ [self.youpaiptimer invalidate]; self.youpaiptimer = nil; } - (void)youpaifcountDown{ self.youpaiptimerCount --; if (self.youpaiptimerCount <= 0) { [self youpaifstopTimer]; [self youpaifhideAnimation]; } } @end