// // QXRedPacketDriftView.m // QXLive // // Created by 启星 on 2025/8/29. // #import "QXRedPacketDriftView.h" #import @interface QXRedPacketDriftView() @property (nonatomic,assign)BOOL isPlaying; @property (nonatomic,assign)BOOL isClose; @property (nonatomic,strong)AVPlayer* player; @end @implementation QXRedPacketDriftView - (instancetype)init { self = [super init]; if (self) { self.frame = CGRectMake(SCREEN_WIDTH, ScaleWidth(290), ScaleWidth(334), ScaleWidth(102)); BOOL isClose = [[NSUserDefaults standardUserDefaults] boolForKey:kIsCloseDrifPop]; self.isClose = isClose; [self initSubviews]; } return self; } +(instancetype)shareView{ static QXRedPacketDriftView *manager = nil; static dispatch_once_t predicate; dispatch_once(&predicate, ^{ manager = [[QXRedPacketDriftView alloc] init]; }); return manager; } -(void)initSubviews{ self.bgImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"red_packet_pop_bg"]]; self.bgImageView.contentMode = UIViewContentModeScaleToFill; [self addSubview:self.bgImageView]; [self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.width.mas_equalTo(ScaleWidth(334)); make.height.mas_equalTo(ScaleWidth(102)); make.centerX.equalTo(self); make.top.equalTo(self); }]; self.titleLabel = [[UILabel alloc] init]; self.titleLabel.font = [UIFont systemFontOfSize:12]; self.titleLabel.textColor = RGB16(0xFFFFFF); [self addSubview:self.titleLabel];; [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.bgImageView).offset(ScaleWidth(58)); make.bottom.equalTo(self.bgImageView).offset(ScaleWidth(-37)); make.height.mas_equalTo(14); }]; self.userInteractionEnabled = YES; MJWeakSelf [self addTapBlock:^(id _Nonnull obj) { if ([weakSelf.model.room_id isExist]) { [[QXGlobal shareGlobal] joinRoomWithRoomId:weakSelf.model.room_id isRejoin:NO navagationController:weakSelf.viewController.navigationController]; } }]; } -(void)addRedpacketModel:(QXRedPacketModel *)model{ if (self.isClose) { return; } [self.dataArray addObject:model]; [self giftAction]; } -(void)giftAction{ if (self.isPlaying) { return; } MJWeakSelf [QXRedPacketDriftView shareView].isPlaying = YES; [QXRedPacketDriftView shareView].model = [QXRedPacketDriftView shareView].dataArray.firstObject; [self.player seekToTime:CMTimeMake(0, 1)]; [self.player play]; [KEYWINDOW addSubview:[QXRedPacketDriftView shareView]]; [UIView animateWithDuration:1.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{ [QXRedPacketDriftView shareView].x = (SCREEN_WIDTH-ScaleWidth(334))/2; } completion:^(BOOL finished) { [weakSelf performSelector:@selector(dissmissAnimate) afterDelay:5]; }]; } -(void)dissmissAnimate{ [UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{ [QXRedPacketDriftView shareView].x = -SCREEN_WIDTH; } completion:^(BOOL finished) { [QXRedPacketDriftView shareView].x = SCREEN_WIDTH; [[QXRedPacketDriftView shareView] removeFromSuperview]; [[QXRedPacketDriftView shareView].dataArray removeFirstObject]; [QXRedPacketDriftView shareView].isPlaying = NO; if ([QXRedPacketDriftView shareView].dataArray.count>0) { [[QXRedPacketDriftView shareView] giftAction]; } }]; } -(void)setModel:(QXRedPacketDriftModel *)model{ _model = model; NSMutableAttributedString*attr = [[NSMutableAttributedString alloc] initWithString:model.text]; [attr yy_setColor:RGB16(0x6C49E4) range:[model.text rangeOfString:model.nickname]]; [attr yy_setColor:RGB16(0x6C49E4) range:[model.text rangeOfString:model.room_name]]; self.titleLabel.attributedText = attr; } -(void)drifPopIsClose:(BOOL)isClose{ _isClose = isClose; self.hidden = self.isClose; [[NSUserDefaults standardUserDefaults] setBool:isClose forKey:kIsCloseDrifPop]; [[NSUserDefaults standardUserDefaults] synchronize]; if (isClose) { [QXRedPacketDriftView shareView].x = SCREEN_WIDTH; [[QXRedPacketDriftView shareView] removeFromSuperview]; [[QXRedPacketDriftView shareView].dataArray removeAllObjects]; [QXRedPacketDriftView shareView].isPlaying = NO; } } -(NSMutableArray *)dataArray{ if (!_dataArray) { _dataArray = [NSMutableArray array]; } return _dataArray; } -(AVPlayer *)player{ if (!_player) { NSString *path = [[NSBundle mainBundle] pathForResource:@"red_packet_come" ofType:@"mp3"]; _player = [[AVPlayer alloc] initWithURL:[NSURL fileURLWithPath:path]]; } return _player; } @end @implementation QXRedPacketDriftModel @end