// // YOUPAILZLiveCallWindow.m // TIANYAN // // Created by CY on 2021/5/29. // Copyright © 2021 leo. All rights reserved. // #import "YOUPAILZLiveCallWindow.h" @interface YOUPAILZLiveCallWindow () @property (nonatomic,assign)LZLiveInviteState youpaipstate; @property (nonatomic,assign)LZLiveInviteType youpaiptype; @property (nonatomic,strong)YOUPAILZLiveStartPKModel *youpaipmodel; @property (nonatomic,strong)NSTimer *timer; @property (nonatomic,assign)NSInteger youpaiptimeCount; @property (nonatomic,strong)NSMutableArray *youpaipbtns; @end @implementation YOUPAILZLiveCallWindow - (void)dealloc{ [self youpaifstopTimer]; [[NSNotificationCenter defaultCenter] removeObserver:self]; } -(instancetype)initWithState:(LZLiveInviteState)state type:(LZLiveInviteType)type model:(nonnull YOUPAILZLiveStartPKModel *)model{ if (self = [super init]) { _youpaipstate = state; _youpaiptype = type; _youpaipmodel = model; } return self; } - (void)viewDidLoad { [super viewDidLoad]; self.baseView.hidden = YES; [self youpaifinitUI]; if (self.youpaipstate == LZLiveInviteStateCall) { self.youpaiptimeCount = 15; NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(youpaifcountDown) userInfo:nil repeats:YES]; self.timer = timer; [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes]; } [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(youpaifdismissVC) name:@"RemoveLiveCallWindow" object:nil]; } - (void)youpaifdismissVC{ [self youpaifstopTimer]; [self dismissViewControllerAnimated:YES completion:nil]; } - (void)youpaifcountDown{ self.youpaiptimeCount --; if (self.youpaiptimeCount <= 0) { [self youpaifstopTimer]; [self youpaifcancelLianMaiOrPK:@"已超时"]; } if (self.youpaipbtns.count != 0) { UIButton *btn = self.youpaipbtns[0]; [btn setTitle:[NSString stringWithFormat:@"取消(%@s)",@(self.youpaiptimeCount)] forState:UIControlStateNormal]; } } - (void)youpaifstopTimer{ [self.timer invalidate]; self.timer = nil; } - (void)youpaifinitUI{ UIView *bgV = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, KScreenWidth, ScaleSize(267.0f))]; bgV.layer.borderColor = HexColorFromRGB(0x4F4B5B).CGColor; bgV.layer.borderWidth = 0.5f; bgV.backgroundColor = LCBkgColor; [LCTools clipCorner:UIRectCornerTopLeft|UIRectCornerTopRight View:bgV size:CGSizeMake(20.0f, 20.0f)]; [self.view addSubview:bgV]; [bgV mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.bottom.offset(0.0f); make.height.offset(ScaleSize(267.0f)); }]; UILabel *titleL = [[UILabel alloc] init]; titleL.font = LCFont17; titleL.textColor = [UIColor whiteColor]; titleL.textAlignment = NSTextAlignmentCenter; titleL.text = self.titleStr; [bgV addSubview:titleL]; [titleL mas_makeConstraints:^(MASConstraintMaker *make) { make.left.top.right.offset(0.0f); make.height.offset(49.0f); }]; UIImageView *callImgV = [[UIImageView alloc] init]; callImgV.image = self.callImage; [bgV addSubview:callImgV]; [callImgV mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(titleL.mas_bottom).offset(ScaleSize(45.0f)); make.centerX.equalTo(bgV.mas_centerX); make.size.mas_offset(CGSizeMake(41.0f, 47.0f)); }]; UIImageView *formAvatarImgV = [[UIImageView alloc] init]; formAvatarImgV.contentMode = UIViewContentModeScaleAspectFill; formAvatarImgV.layer.cornerRadius = 23.0f; formAvatarImgV.clipsToBounds = YES; formAvatarImgV.layer.borderColor = HexColorFromRGB(0xB6DEFA).CGColor; formAvatarImgV.layer.borderWidth = 1.0f; [formAvatarImgV sd_setImageWithURL:[LCTools getImageUrlWithAddress:self.youpaipmodel.youpaipfrom_user.youpaipavatar]]; [bgV addSubview:formAvatarImgV]; [formAvatarImgV mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(callImgV.mas_left).offset(-ScaleSize(60.0f)); make.top.equalTo(titleL.mas_bottom).offset(ScaleSize(36.0f)); make.size.mas_offset(CGSizeMake(46.0f, 46.0f)); }]; UILabel *formNicknameL = [[UILabel alloc] init]; formNicknameL.font = LCFont14; formNicknameL.textColor = [UIColor whiteColor]; formNicknameL.text = self.youpaipmodel.youpaipfrom_user.youpaipnickname; [bgV addSubview:formNicknameL]; [formNicknameL mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(formAvatarImgV.mas_bottom).offset(7.0f); make.centerX.equalTo(formAvatarImgV.mas_centerX); }]; UIImageView *toAvatarImgV = [[UIImageView alloc] init]; toAvatarImgV.contentMode = UIViewContentModeScaleAspectFill; toAvatarImgV.layer.cornerRadius = 23.0f; toAvatarImgV.clipsToBounds = YES; toAvatarImgV.layer.borderColor = HexColorFromRGB(0xB6DEFA).CGColor; toAvatarImgV.layer.borderWidth = 1.0f; [toAvatarImgV sd_setImageWithURL:[LCTools getImageUrlWithAddress:self.youpaipmodel.youpaipto_user.youpaipavatar]]; [bgV addSubview:toAvatarImgV]; [toAvatarImgV mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(callImgV.mas_right).offset(ScaleSize(60.0f)); make.top.equalTo(titleL.mas_bottom).offset(ScaleSize(36.0f)); make.size.mas_offset(CGSizeMake(46.0f, 46.0f)); }]; UILabel *toNicknameL = [[UILabel alloc] init]; toNicknameL.font = LCFont14; toNicknameL.textColor = [UIColor whiteColor]; toNicknameL.text = self.youpaipmodel.youpaipto_user.youpaipnickname; [bgV addSubview:toNicknameL]; [toNicknameL mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(toAvatarImgV.mas_bottom).offset(7.0f); make.centerX.equalTo(toAvatarImgV.mas_centerX); }]; NSArray *btnTitles = @[]; if (self.youpaipstate == LZLiveInviteStateCall) { btnTitles = @[@"取消(15s)"]; }else{ btnTitles = @[@"拒绝",@"接受"]; } for (NSInteger i = 0; i < btnTitles.count; i ++) { UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; [btn setBackgroundImage:self.btnBgImages[i] forState:UIControlStateNormal]; [btn setBackgroundImage:self.btnBgImages[i] forState:UIControlStateHighlighted]; btn.layer.cornerRadius = 17.0f; btn.clipsToBounds = YES; [btn setTitle:btnTitles[i] forState:UIControlStateNormal]; [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; btn.titleLabel.font = self.btnFont; btn.tag = i; [btn addTarget:self action:@selector(youpaifbtnClick:) forControlEvents:UIControlEventTouchUpInside]; [bgV addSubview:btn]; [self.youpaipbtns addObject:btn]; if (btnTitles.count == 1) { [btn mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.offset(ScaleSize(-42.0f)); make.centerX.equalTo(bgV.mas_centerX); make.size.mas_offset(CGSizeMake(93.0f, 34.0f)); }]; }else if (btnTitles.count == 2){ [btn mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.offset(ScaleSize(-42.0f)); if (i == 0) { make.right.equalTo(bgV.mas_centerX).offset(-ScaleSize(35.5f)); }else{ make.left.equalTo(bgV.mas_centerX).offset(ScaleSize(35.5f)); } make.size.mas_offset(CGSizeMake(93.0f, 34.0f)); }]; } } } - (void)youpaifbtnClick:(UIButton *)sender{ if (sender.tag == 0) { if (self.youpaipstate == LZLiveInviteStateCall) { [self youpaifcancelLianMaiOrPK:@"已取消"]; }else{ [self youpaifoperateToWithEvent:@"refuse" text:@"已拒绝"]; } }else if (sender.tag == 1){ [self youpaifoperateToWithEvent:@"connect" text:nil]; } } - (void)youpaifoperateToWithEvent:(NSString *)event text:(NSString *)text{ NSDictionary *params = @{ @"pk_id":self.youpaipmodel.youpaippk_id, @"event":event }; WeakSelf; [LCHttpHelper requestWithURLString:OperateTo parameters:params needToken:YES type:HttpRequestTypePost success:^(id responseObject) { NSDictionary* dict = (NSDictionary*)responseObject; NSInteger code = [[dict objectForKey:@"code"] integerValue]; if (code == 0) { if (text.length != 0) { [ZCHUDHelper showTitle:text]; } if ([event isEqual:@"connect"] && self.youpaipstartConnectBlock != nil) { weakSelf.youpaipstartConnectBlock(self.youpaipmodel); } [weakSelf youpaifdismissVC]; }else{ [ZCHUDHelper showTitle:[dict objectForKey:@"message"]]; } } failure:^(NSError *error) { [ZCHUDHelper showTitle:error.localizedDescription]; }]; } - (void)youpaifcancelLianMaiOrPK:(NSString *)text{ NSDictionary *params = @{ @"pk_id":self.youpaipmodel.youpaippk_id, @"event":@"cancel" }; WeakSelf; [LCHttpHelper requestWithURLString:OperateFrom parameters:params needToken:YES type:HttpRequestTypePost success:^(id responseObject) { NSDictionary* dict = (NSDictionary*)responseObject; NSInteger code = [[dict objectForKey:@"code"] integerValue]; if (code == 0) { [ZCHUDHelper showTitle:text]; [weakSelf youpaifdismissVC]; }else{ [ZCHUDHelper showTitle:[dict objectForKey:@"message"]]; } } failure:^(NSError *error) { [ZCHUDHelper showTitle:error.localizedDescription]; }]; } - (NSMutableArray *)youpaipbtns{ if (!_youpaipbtns) { _youpaipbtns = [NSMutableArray array]; } return _youpaipbtns; } /// 中间电话的图片 - (UIImage *)callImage{ return [UIImage imageNamed:@"vqu_images_L_live_call_phone"]; } - (UIFont *)btnFont{ if (self.youpaipstate == LZLiveInviteStateCall) { return LCFont12; }else{ return LCFont14; } } - (NSArray *)btnBgImages{ if (self.youpaipstate == LZLiveInviteStateCall) { return @[[LCTools ColorImage:CGSizeMake(93.0f, 34.0f) FromColors:@[HexColorFromRGB(0x75DFFF),HexColorFromRGB(0x0038FF)] ByGradientType:GradientLeftToRight]]; }else{ return @[[LCTools ColorImage:CGSizeMake(93.0f, 34.0f) FromColors:@[HexColorFromRGB(0x75DFFF),HexColorFromRGB(0x0038FF)] ByGradientType:GradientLeftToRight], [LCTools ColorImage:CGSizeMake(93.0f, 34.0f) FromColors:@[HexColorFromRGB(0xFF3BDC),HexColorFromRGB(0xFC2F55)] ByGradientType:GradientLeftToRight], ]; } } - (NSString *)titleStr{ if (self.youpaiptype == LZLiveInviteTypePK) { return @"约战邀请"; }else{ return @"连麦邀请"; } } @end