YOUPAILZChatRoomBlindDataAnimationView.m 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. //
  2. // YOUPAILZChatRoomBlindDataAnimationView.m
  3. // YOUQU
  4. //
  5. // Created by CY on 2021/12/20.
  6. // Copyright © 2021 MS. All rights reserved.
  7. //
  8. #import "YOUPAILZChatRoomBlindDataAnimationView.h"
  9. #import <SVGAPlayer.h>
  10. #import <SVGAParser.h>
  11. @interface YOUPAILZChatRoomBlindDataAnimationView ()<SVGAPlayerDelegate>
  12. @property (nonatomic, strong) NSMutableArray *youpaipsvgas;
  13. @property (nonatomic, strong) SVGAPlayer *youpaipsvgaPlayer;
  14. @property (nonatomic, strong) SVGAParser *youpaipparser;
  15. @property (nonatomic, assign) BOOL youpaipisPlaySvga;
  16. @property (nonatomic, weak) UIView *youpaipavatarBgV;
  17. @property (nonatomic, weak) UIImageView *youpaipleftAvatarImgV;
  18. @property (nonatomic, weak) UILabel *youpaipleftNameL;
  19. @property (nonatomic, weak) UIImageView *youpaiprightAvatarImgV;
  20. @property (nonatomic, weak) UILabel *youpaiprightNameL;
  21. @end
  22. @implementation YOUPAILZChatRoomBlindDataAnimationView
  23. - (instancetype)initWithFrame:(CGRect)frame{
  24. if (self = [super initWithFrame:frame]) {
  25. [self youpaifinitUI];
  26. }
  27. return self;
  28. }
  29. - (void)youpaifinitUI{
  30. self.youpaipsvgaPlayer = [[SVGAPlayer alloc] initWithFrame:[UIScreen mainScreen].bounds];
  31. self.youpaipsvgaPlayer.contentMode = UIViewContentModeScaleAspectFit;
  32. self.youpaipsvgaPlayer.delegate = self;
  33. [self addSubview:self.youpaipsvgaPlayer];
  34. self.youpaipsvgaPlayer.hidden = YES;
  35. self.youpaipsvgaPlayer.loops = 1;
  36. self.youpaipsvgaPlayer.clearsAfterStop = true;
  37. self.youpaipparser = [[SVGAParser alloc] init];
  38. self.youpaipsvgaPlayer.userInteractionEnabled = NO;
  39. self.youpaipisPlaySvga = NO;
  40. UIView *youpaipavatarBgV = [[UIView alloc] init];
  41. [self addSubview:youpaipavatarBgV];
  42. [self addSubview:youpaipavatarBgV];
  43. self.youpaipavatarBgV = youpaipavatarBgV;
  44. [youpaipavatarBgV mas_makeConstraints:^(MASConstraintMaker *make) {
  45. make.center.equalTo(self);
  46. make.size.mas_offset(CGSizeMake(ScaleSize(190.0f), ScaleSize(84.0f)));
  47. }];
  48. UIImageView *youpaipleftAvatarImgV = [[UIImageView alloc] init];
  49. youpaipleftAvatarImgV.layer.cornerRadius = ScaleSize(30.0f);
  50. youpaipleftAvatarImgV.clipsToBounds = YES;
  51. youpaipleftAvatarImgV.layer.borderColor = HexColorFromRGB(0x368CFF).CGColor;
  52. youpaipleftAvatarImgV.layer.borderWidth = ScaleSize(2.0f);
  53. youpaipleftAvatarImgV.contentMode = UIViewContentModeScaleAspectFill;
  54. [youpaipavatarBgV addSubview:youpaipleftAvatarImgV];
  55. self.youpaipleftAvatarImgV = youpaipleftAvatarImgV;
  56. [youpaipleftAvatarImgV mas_makeConstraints:^(MASConstraintMaker *make) {
  57. make.top.offset(0.0f);
  58. make.left.offset(ScaleSize(12.0f));
  59. make.size.mas_offset(CGSizeMake(ScaleSize(60.0f), ScaleSize(60.0f)));
  60. }];
  61. UIImage *bgImg = [UIImage imageNamed:@"vqu_image_chatroom_blinddate_animation_name_bg"];
  62. UIImageView *leftNameBgV = [[UIImageView alloc] initWithImage:bgImg];
  63. [youpaipavatarBgV addSubview:leftNameBgV];
  64. [leftNameBgV mas_makeConstraints:^(MASConstraintMaker *make) {
  65. make.left.bottom.offset(0.0f);
  66. make.size.mas_offset(CGSizeMake(ScaleSize(bgImg.size.width),ScaleSize(bgImg.size.height)));
  67. }];
  68. UILabel *youpaipleftNameL = [[UILabel alloc] init];
  69. youpaipleftNameL.font = LCFont(ScaleSize(10.0f));
  70. youpaipleftNameL.textColor = HexColorFromRGB(0x513F27);
  71. youpaipleftNameL.textAlignment = NSTextAlignmentCenter;
  72. [leftNameBgV addSubview:youpaipleftNameL];
  73. self.youpaipleftNameL = youpaipleftNameL;
  74. [youpaipleftNameL mas_makeConstraints:^(MASConstraintMaker *make) {
  75. make.top.bottom.offset(0.0f);
  76. make.left.offset(ScaleSize(ScaleSize(10.0f)));
  77. make.right.offset(ScaleSize(ScaleSize(-10.0f)));
  78. }];
  79. UIImageView *youpaiprightAvatarImgV = [[UIImageView alloc] init];
  80. youpaiprightAvatarImgV.layer.cornerRadius = ScaleSize(30.0f);
  81. youpaiprightAvatarImgV.clipsToBounds = YES;
  82. youpaiprightAvatarImgV.layer.borderColor = HexColorFromRGB(0xFF48B3).CGColor;
  83. youpaiprightAvatarImgV.layer.borderWidth = ScaleSize(2.0f);
  84. youpaiprightAvatarImgV.contentMode = UIViewContentModeScaleAspectFill;
  85. [youpaipavatarBgV addSubview:youpaiprightAvatarImgV];
  86. self.youpaiprightAvatarImgV = youpaiprightAvatarImgV;
  87. [youpaiprightAvatarImgV mas_makeConstraints:^(MASConstraintMaker *make) {
  88. make.top.offset(0.0f);
  89. make.right.offset(ScaleSize(-12.0f));
  90. make.size.mas_offset(CGSizeMake(ScaleSize(60.0f), ScaleSize(60.0f)));
  91. }];
  92. UIImageView *rightNameBgV = [[UIImageView alloc] initWithImage:bgImg];
  93. [youpaipavatarBgV addSubview:rightNameBgV];
  94. [rightNameBgV mas_makeConstraints:^(MASConstraintMaker *make) {
  95. make.right.bottom.offset(0.0f);
  96. make.size.mas_offset(CGSizeMake(ScaleSize(bgImg.size.width),ScaleSize(bgImg.size.height)));
  97. }];
  98. UILabel *youpaiprightNameL = [[UILabel alloc] init];
  99. youpaiprightNameL.font = LCFont(ScaleSize(10.0f));
  100. youpaiprightNameL.textColor = HexColorFromRGB(0x513F27);
  101. youpaiprightNameL.textAlignment = NSTextAlignmentCenter;
  102. [rightNameBgV addSubview:youpaiprightNameL];
  103. self.youpaiprightNameL = youpaiprightNameL;
  104. [youpaiprightNameL mas_makeConstraints:^(MASConstraintMaker *make) {
  105. make.top.bottom.offset(0.0f);
  106. make.left.offset(ScaleSize(ScaleSize(10.0f)));
  107. make.right.offset(ScaleSize(ScaleSize(-10.0f)));
  108. }];
  109. }
  110. - (void)youpaifreloadWithModels:(NSArray<YOUPAILZChatRoomBlindDataAnimationModel *> *)models{
  111. self.youpaipsvgas = models.mutableCopy;
  112. [self youpaifstartPlaySvga];
  113. }
  114. - (void)youpaifstartPlaySvga{
  115. if (self.youpaipisPlaySvga == NO && self.youpaipsvgas.count != 0) {
  116. self.youpaipisPlaySvga = YES;
  117. YOUPAILZChatRoomBlindDataAnimationModel* animationModel = self.youpaipsvgas[0];
  118. [self.youpaipleftAvatarImgV sd_setImageWithURL:[LCTools getImageUrlWithAddress:animationModel.youpaipavatar_left]];
  119. self.youpaipleftNameL.text = animationModel.youpaipname_left;
  120. [self.youpaiprightAvatarImgV sd_setImageWithURL:[LCTools getImageUrlWithAddress:animationModel.youpaipavatar_right]];
  121. self.youpaiprightNameL.text = animationModel.youpaipname_right;
  122. [self.youpaipsvgaPlayer stopAnimation];
  123. self.youpaipsvgaPlayer.hidden = NO;
  124. NSString* svgaName = [[animationModel.youpaipsvga componentsSeparatedByString:@"/"] lastObject];
  125. NSString* svgaCanchesPath= [[NSString alloc]initWithFormat:@"%@/%@/%@",CachesPath,@"SVGA",svgaName];
  126. if (![LCTools giftSVGAWithSvgaUrlStr:animationModel.youpaipsvga]){
  127. NSString* urlStr = [NSString stringWithFormat:@"%@/%@",[LCSaveData getImageUrl]?[LCSaveData getImageUrl]:BaseImgUrl,animationModel.youpaipsvga];
  128. @weakify(self);
  129. [self.youpaipparser parseWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]] completionBlock:^(SVGAVideoEntity * _Nullable videoItem) {
  130. @strongify(self);
  131. if (videoItem != nil) {
  132. self.youpaipsvgaPlayer.videoItem = videoItem;
  133. [self.youpaipsvgaPlayer startAnimation];
  134. [self performSelector:@selector(youpaifAvatarAnimation) withObject:nil afterDelay:0.1];
  135. }
  136. } failureBlock:^(NSError * _Nullable error) {
  137. @strongify(self);
  138. self.youpaipsvgaPlayer.hidden = YES;
  139. self.youpaipisPlaySvga = NO;
  140. [self youpaifstartPlaySvga];
  141. }];
  142. [LCTools giftSVGAWithSvgaUrlStr:animationModel.youpaipsvga];
  143. }else{
  144. @weakify(self);
  145. [self.youpaipparser parseWithData:[LCTools giftSVGAWithSvgaUrlStr:animationModel.youpaipsvga] cacheKey:svgaCanchesPath completionBlock:^(SVGAVideoEntity * _Nonnull videoItem) {
  146. @strongify(self);
  147. if (videoItem != nil) {
  148. self.youpaipsvgaPlayer.videoItem = videoItem;
  149. [self.youpaipsvgaPlayer startAnimation];
  150. [self performSelector:@selector(youpaifAvatarAnimation) withObject:nil afterDelay:0.1];
  151. }
  152. } failureBlock:^(NSError * _Nonnull error) {
  153. @strongify(self);
  154. self.youpaipsvgaPlayer.hidden = YES;
  155. self.youpaipisPlaySvga = NO;
  156. [self youpaifstartPlaySvga];
  157. }];
  158. }
  159. [self.youpaipsvgas removeObject:animationModel];
  160. }
  161. }
  162. - (void)youpaifAvatarAnimation{
  163. self.youpaipavatarBgV.hidden = NO;
  164. self.youpaipavatarBgV.alpha = 1.0f;
  165. [self layoutIfNeeded];
  166. self.youpaipavatarBgV.transform = CGAffineTransformMakeScale(0.5f, 0.5f);
  167. [UIView animateWithDuration:0.25f animations:^{
  168. self.youpaipavatarBgV.transform = CGAffineTransformMakeScale(1.0f, 1.0f);
  169. [self layoutIfNeeded];
  170. }];
  171. }
  172. #pragma mark - SVGAPlayerDelegate
  173. - (void)svgaPlayerDidFinishedAnimation:(SVGAPlayer *)player{
  174. self.youpaipavatarBgV.alpha = 0.5f;
  175. [UIView animateWithDuration:0.25f animations:^{
  176. self.youpaipavatarBgV.alpha = 0.0f;
  177. } completion:^(BOOL finished) {
  178. self.youpaipsvgaPlayer.hidden = YES;
  179. self.youpaipisPlaySvga = NO;
  180. self.youpaipavatarBgV.hidden = YES;
  181. if (self.youpaipsvgas.count != 0) {
  182. [self youpaifstartPlaySvga];
  183. }else{
  184. if (self.animationFinishBlock != nil) {
  185. self.animationFinishBlock();
  186. }
  187. }
  188. }];
  189. }
  190. @end