// // CPBindingAnimation.m // YuMi // // Created by P on 2024/9/23. // #import "CPBindingAnimation.h" #import "UIView+VAP.h" #import "AttachmentModel.h" #import "QGVAPConfigModel.h" @interface CPBindingAnimation() @property (nonatomic, strong) VAPView *cpVAPView; @property (nonatomic, strong) NetImageView *avatarLoader; @property (nonatomic, strong) NetImageView *cpAvatarLoader; @property (nonatomic, copy) void(^completeDisplay)(void); @end @implementation CPBindingAnimation - (instancetype)init { if (self = [super init]) { [self addSubview:self.cpVAPView]; [self.cpVAPView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.mas_equalTo(self); }]; } return self; } // {"data":{"giftUrl":"https://image.pekolive.com/I Do.png","partitionId":2,"receiverAvatar":"https://img.molistar.xyz/default_avatar_molistar.png","roomUid":3238,"senderAvatar":"https://img.molistar.xyz/default_avatar_molistar.png"},"first":64,"second":643} - (void)setCpAttachment:(AttachmentModel *)cpAttachment { _cpAttachment = cpAttachment; #if DEBUG if (cpAttachment == nil) { AttachmentModel *m = [[AttachmentModel alloc] init]; m.data = @{@"senderAvatar":@"https://img.molistar.xyz/default_avatar_molistar.png", @"receiverAvatar":@"https://img.molistar.xyz/default_avatar_molistar.png"}; _cpAttachment = m; } #endif [self startPlay]; } - (void)startPlay { NSString *filePath = [[NSBundle mainBundle] pathForResource:@"CP绑定" ofType:@"mp4"]; [self.cpVAPView playHWDMP4:filePath delegate:self]; } - (void)endPlay { if (_cpVAPView) { @kWeakify(self); dispatch_async(dispatch_get_main_queue(), ^{ if (self.completeDisplay) { self.completeDisplay(); } @kStrongify(self); [self.cpVAPView removeFromSuperview]; self.cpVAPView = nil; }); } } + (void)display:(UIView *)superView with:(AttachmentModel *)attachment complete:(void(^)(void))complete { CPBindingAnimation *cpBindingAnimation = [[CPBindingAnimation alloc] init]; cpBindingAnimation.cpAttachment = attachment; cpBindingAnimation.completeDisplay = complete; [superView addSubview:cpBindingAnimation]; [cpBindingAnimation mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.mas_equalTo(superView); make.top.mas_equalTo(146); make.width.mas_equalTo(KScreenWidth); make.height.mas_equalTo(kGetScaleWidth(272)); }]; } #pragma mark - HWDMP4PlayDelegate - (NSString *)contentForVapTag:(NSString *)tag resource:(QGVAPSourceInfo *)info { NSDictionary *extraInfo = @{@"name1" : self.cpAttachment.data[@"senderNick"], @"name2" : self.cpAttachment.data[@"receiverNick"]}; return extraInfo[tag]; } - (void)loadVapImageWithURL:(NSString *)urlStr context:(NSDictionary *)context completion:(VAPImageCompletionBlock)completionBlock { @kWeakify(self); dispatch_async(dispatch_get_main_queue(), ^{ @kStrongify(self); QGVAPSourceInfo *info = (QGVAPSourceInfo *)context[@"resource"]; if ([info.contentTag isEqualToString:@"avatar1"] ) { self.avatarLoader = [[NetImageView alloc] init]; [self.avatarLoader loadImageWithUrl:self.cpAttachment.data[@"senderAvatar"] completion:^(UIImage * _Nullable image, NSURL * _Nonnull url) { completionBlock(image, nil, urlStr); }]; } else { self.cpAvatarLoader = [[NetImageView alloc] init]; [self.cpAvatarLoader loadImageWithUrl:self.cpAttachment.data[@"receiverAvatar"] completion:^(UIImage * _Nullable image, NSURL * _Nonnull url) { completionBlock(image, nil, urlStr); }]; } }); } - (BOOL)shouldStartPlayMP4:(VAPView *)container config:(QGVAPConfigModel *)config { return YES; } - (void)viewDidStopPlayMP4:(NSInteger)lastFrameIndex view:(VAPView *)container { [self endPlay]; } - (void)viewDidFinishPlayMP4:(NSInteger)totalFrameCount view:(VAPView *)container { [self endPlay]; } - (void)viewDidFailPlayMP4:(NSError *)error { [self endPlay]; } #pragma mark - - (VAPView *)cpVAPView { if (!_cpVAPView) { _cpVAPView = [[VAPView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 0)]; _cpVAPView.contentMode = UIViewContentModeScaleAspectFill; } return _cpVAPView; } @end