// // YOUPAILZChatRoomBlindDataAnimationView.m // YOUQU // // Created by CY on 2021/12/20. // Copyright © 2021 MS. All rights reserved. // #import "YOUPAILZChatRoomBlindDataAnimationView.h" #import #import @interface YOUPAILZChatRoomBlindDataAnimationView () @property (nonatomic, strong) NSMutableArray *youpaipsvgas; @property (nonatomic, strong) SVGAPlayer *youpaipsvgaPlayer; @property (nonatomic, strong) SVGAParser *youpaipparser; @property (nonatomic, assign) BOOL youpaipisPlaySvga; @property (nonatomic, weak) UIView *youpaipavatarBgV; @property (nonatomic, weak) UIImageView *youpaipleftAvatarImgV; @property (nonatomic, weak) UILabel *youpaipleftNameL; @property (nonatomic, weak) UIImageView *youpaiprightAvatarImgV; @property (nonatomic, weak) UILabel *youpaiprightNameL; @end @implementation YOUPAILZChatRoomBlindDataAnimationView - (instancetype)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { [self youpaifinitUI]; } return self; } - (void)youpaifinitUI{ self.youpaipsvgaPlayer = [[SVGAPlayer alloc] initWithFrame:[UIScreen mainScreen].bounds]; self.youpaipsvgaPlayer.contentMode = UIViewContentModeScaleAspectFit; self.youpaipsvgaPlayer.delegate = self; [self addSubview:self.youpaipsvgaPlayer]; self.youpaipsvgaPlayer.hidden = YES; self.youpaipsvgaPlayer.loops = 1; self.youpaipsvgaPlayer.clearsAfterStop = true; self.youpaipparser = [[SVGAParser alloc] init]; self.youpaipsvgaPlayer.userInteractionEnabled = NO; self.youpaipisPlaySvga = NO; UIView *youpaipavatarBgV = [[UIView alloc] init]; [self addSubview:youpaipavatarBgV]; [self addSubview:youpaipavatarBgV]; self.youpaipavatarBgV = youpaipavatarBgV; [youpaipavatarBgV mas_makeConstraints:^(MASConstraintMaker *make) { make.center.equalTo(self); make.size.mas_offset(CGSizeMake(ScaleSize(190.0f), ScaleSize(84.0f))); }]; UIImageView *youpaipleftAvatarImgV = [[UIImageView alloc] init]; youpaipleftAvatarImgV.layer.cornerRadius = ScaleSize(30.0f); youpaipleftAvatarImgV.clipsToBounds = YES; youpaipleftAvatarImgV.layer.borderColor = HexColorFromRGB(0x368CFF).CGColor; youpaipleftAvatarImgV.layer.borderWidth = ScaleSize(2.0f); youpaipleftAvatarImgV.contentMode = UIViewContentModeScaleAspectFill; [youpaipavatarBgV addSubview:youpaipleftAvatarImgV]; self.youpaipleftAvatarImgV = youpaipleftAvatarImgV; [youpaipleftAvatarImgV mas_makeConstraints:^(MASConstraintMaker *make) { make.top.offset(0.0f); make.left.offset(ScaleSize(12.0f)); make.size.mas_offset(CGSizeMake(ScaleSize(60.0f), ScaleSize(60.0f))); }]; UIImage *bgImg = [UIImage imageNamed:@"vqu_image_chatroom_blinddate_animation_name_bg"]; UIImageView *leftNameBgV = [[UIImageView alloc] initWithImage:bgImg]; [youpaipavatarBgV addSubview:leftNameBgV]; [leftNameBgV mas_makeConstraints:^(MASConstraintMaker *make) { make.left.bottom.offset(0.0f); make.size.mas_offset(CGSizeMake(ScaleSize(bgImg.size.width),ScaleSize(bgImg.size.height))); }]; UILabel *youpaipleftNameL = [[UILabel alloc] init]; youpaipleftNameL.font = LCFont(ScaleSize(10.0f)); youpaipleftNameL.textColor = HexColorFromRGB(0x513F27); youpaipleftNameL.textAlignment = NSTextAlignmentCenter; [leftNameBgV addSubview:youpaipleftNameL]; self.youpaipleftNameL = youpaipleftNameL; [youpaipleftNameL mas_makeConstraints:^(MASConstraintMaker *make) { make.top.bottom.offset(0.0f); make.left.offset(ScaleSize(ScaleSize(10.0f))); make.right.offset(ScaleSize(ScaleSize(-10.0f))); }]; UIImageView *youpaiprightAvatarImgV = [[UIImageView alloc] init]; youpaiprightAvatarImgV.layer.cornerRadius = ScaleSize(30.0f); youpaiprightAvatarImgV.clipsToBounds = YES; youpaiprightAvatarImgV.layer.borderColor = HexColorFromRGB(0xFF48B3).CGColor; youpaiprightAvatarImgV.layer.borderWidth = ScaleSize(2.0f); youpaiprightAvatarImgV.contentMode = UIViewContentModeScaleAspectFill; [youpaipavatarBgV addSubview:youpaiprightAvatarImgV]; self.youpaiprightAvatarImgV = youpaiprightAvatarImgV; [youpaiprightAvatarImgV mas_makeConstraints:^(MASConstraintMaker *make) { make.top.offset(0.0f); make.right.offset(ScaleSize(-12.0f)); make.size.mas_offset(CGSizeMake(ScaleSize(60.0f), ScaleSize(60.0f))); }]; UIImageView *rightNameBgV = [[UIImageView alloc] initWithImage:bgImg]; [youpaipavatarBgV addSubview:rightNameBgV]; [rightNameBgV mas_makeConstraints:^(MASConstraintMaker *make) { make.right.bottom.offset(0.0f); make.size.mas_offset(CGSizeMake(ScaleSize(bgImg.size.width),ScaleSize(bgImg.size.height))); }]; UILabel *youpaiprightNameL = [[UILabel alloc] init]; youpaiprightNameL.font = LCFont(ScaleSize(10.0f)); youpaiprightNameL.textColor = HexColorFromRGB(0x513F27); youpaiprightNameL.textAlignment = NSTextAlignmentCenter; [rightNameBgV addSubview:youpaiprightNameL]; self.youpaiprightNameL = youpaiprightNameL; [youpaiprightNameL mas_makeConstraints:^(MASConstraintMaker *make) { make.top.bottom.offset(0.0f); make.left.offset(ScaleSize(ScaleSize(10.0f))); make.right.offset(ScaleSize(ScaleSize(-10.0f))); }]; } - (void)youpaifreloadWithModels:(NSArray *)models{ self.youpaipsvgas = models.mutableCopy; [self youpaifstartPlaySvga]; } - (void)youpaifstartPlaySvga{ if (self.youpaipisPlaySvga == NO && self.youpaipsvgas.count != 0) { self.youpaipisPlaySvga = YES; YOUPAILZChatRoomBlindDataAnimationModel* animationModel = self.youpaipsvgas[0]; [self.youpaipleftAvatarImgV sd_setImageWithURL:[LCTools getImageUrlWithAddress:animationModel.youpaipavatar_left]]; self.youpaipleftNameL.text = animationModel.youpaipname_left; [self.youpaiprightAvatarImgV sd_setImageWithURL:[LCTools getImageUrlWithAddress:animationModel.youpaipavatar_right]]; self.youpaiprightNameL.text = animationModel.youpaipname_right; [self.youpaipsvgaPlayer stopAnimation]; self.youpaipsvgaPlayer.hidden = NO; NSString* svgaName = [[animationModel.youpaipsvga componentsSeparatedByString:@"/"] lastObject]; NSString* svgaCanchesPath= [[NSString alloc]initWithFormat:@"%@/%@/%@",CachesPath,@"SVGA",svgaName]; if (![LCTools giftSVGAWithSvgaUrlStr:animationModel.youpaipsvga]){ NSString* urlStr = [NSString stringWithFormat:@"%@/%@",[LCSaveData getImageUrl]?[LCSaveData getImageUrl]:BaseImgUrl,animationModel.youpaipsvga]; @weakify(self); [self.youpaipparser parseWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]] completionBlock:^(SVGAVideoEntity * _Nullable videoItem) { @strongify(self); if (videoItem != nil) { self.youpaipsvgaPlayer.videoItem = videoItem; [self.youpaipsvgaPlayer startAnimation]; [self performSelector:@selector(youpaifAvatarAnimation) withObject:nil afterDelay:0.1]; } } failureBlock:^(NSError * _Nullable error) { @strongify(self); self.youpaipsvgaPlayer.hidden = YES; self.youpaipisPlaySvga = NO; [self youpaifstartPlaySvga]; }]; [LCTools giftSVGAWithSvgaUrlStr:animationModel.youpaipsvga]; }else{ @weakify(self); [self.youpaipparser parseWithData:[LCTools giftSVGAWithSvgaUrlStr:animationModel.youpaipsvga] cacheKey:svgaCanchesPath completionBlock:^(SVGAVideoEntity * _Nonnull videoItem) { @strongify(self); if (videoItem != nil) { self.youpaipsvgaPlayer.videoItem = videoItem; [self.youpaipsvgaPlayer startAnimation]; [self performSelector:@selector(youpaifAvatarAnimation) withObject:nil afterDelay:0.1]; } } failureBlock:^(NSError * _Nonnull error) { @strongify(self); self.youpaipsvgaPlayer.hidden = YES; self.youpaipisPlaySvga = NO; [self youpaifstartPlaySvga]; }]; } [self.youpaipsvgas removeObject:animationModel]; } } - (void)youpaifAvatarAnimation{ self.youpaipavatarBgV.hidden = NO; self.youpaipavatarBgV.alpha = 1.0f; [self layoutIfNeeded]; self.youpaipavatarBgV.transform = CGAffineTransformMakeScale(0.5f, 0.5f); [UIView animateWithDuration:0.25f animations:^{ self.youpaipavatarBgV.transform = CGAffineTransformMakeScale(1.0f, 1.0f); [self layoutIfNeeded]; }]; } #pragma mark - SVGAPlayerDelegate - (void)svgaPlayerDidFinishedAnimation:(SVGAPlayer *)player{ self.youpaipavatarBgV.alpha = 0.5f; [UIView animateWithDuration:0.25f animations:^{ self.youpaipavatarBgV.alpha = 0.0f; } completion:^(BOOL finished) { self.youpaipsvgaPlayer.hidden = YES; self.youpaipisPlaySvga = NO; self.youpaipavatarBgV.hidden = YES; if (self.youpaipsvgas.count != 0) { [self youpaifstartPlaySvga]; }else{ if (self.animationFinishBlock != nil) { self.animationFinishBlock(); } } }]; } @end