XYChatRoomShowAnimationView.m 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //
  2. // XYChatRoomShowAnimationView.m
  3. // Timi
  4. //
  5. // Created by 翟玉磊 on 2021/1/4.
  6. //
  7. #import "XYChatRoomShowAnimationView.h"
  8. #import <SVGAPlayer/SVGA.h>
  9. #import <SDWebImage/SDWebImage.h>
  10. typedef void(^AnimationCompletionBlock)(void);
  11. @interface XYChatRoomShowAnimationView ()
  12. @property (nonatomic, strong) UIView *upBgView;
  13. @property (nonatomic, strong) UIView *downBgView;
  14. @property (nonatomic, strong) SVGAPlayer *SVGAPlayer;
  15. @property (nonatomic, strong) SVGAParser *SVGAParser;
  16. @property (nonatomic, copy) AnimationCompletionBlock animationCompletionBlock;
  17. @end
  18. @implementation XYChatRoomShowAnimationView
  19. #pragma mark - Public
  20. - (void)showChatRoomAnimationWithRoomCoverUrl:(NSString *)coverUrl completion:(void(^ __nullable)(void))completion; {
  21. self.animationCompletionBlock = completion;
  22. [[self topWindow] addSubview:self];
  23. [UIView animateWithDuration:.3 animations:^{
  24. self.upBgView.frame = CGRectMake(self.upBgView.f_x, 0, self.upBgView.f_width, self.upBgView.f_heigh);
  25. self.downBgView.frame = CGRectMake(self.downBgView.f_x, self.f_heigh/2, self.downBgView.f_width, self.downBgView.f_heigh);
  26. } completion:^(BOOL finished) {
  27. [self reloadAvatarWithUrl:coverUrl svgaName:@"xy_room_show_loading"];
  28. }];
  29. }
  30. - (void)dismissCompletion:(void(^ __nullable)(void))completion {
  31. [UIView animateWithDuration:.3 animations:^{
  32. self.upBgView.frame = CGRectMake(self.upBgView.f_x, -self.f_heigh/2, self.upBgView.f_width, self.upBgView.f_heigh);
  33. self.downBgView.frame = CGRectMake(self.downBgView.f_x, self.f_heigh, self.downBgView.f_width, self.downBgView.f_heigh);
  34. self.SVGAPlayer.transform = CGAffineTransformMakeScale(0.01, 0.01);
  35. } completion:^(BOOL finished) {
  36. [self clear];
  37. dispatch_async(dispatch_get_main_queue(), ^{
  38. if (completion) {
  39. completion();
  40. }
  41. });
  42. }];
  43. }
  44. // 获取最上层显示的window
  45. - (UIWindow *)topWindow {
  46. UIWindow *topWindow = nil;
  47. NSArray <UIWindow *>*windows = [UIApplication sharedApplication].windows;
  48. for (UIWindow *window in windows.reverseObjectEnumerator) {
  49. if (window.hidden == YES || window.opaque == NO) {
  50. // 隐藏的 或者 透明的 跳过
  51. continue;
  52. }
  53. if (CGRectEqualToRect(window.bounds, [UIScreen mainScreen].bounds) == NO) {
  54. // 不是全屏的 跳过
  55. continue;
  56. }
  57. topWindow = window;
  58. break;
  59. }
  60. return topWindow?:[UIApplication sharedApplication].delegate.window;
  61. }
  62. - (void)reloadAvatarWithUrl:(NSString *)userAvatar svgaName:(NSString *)svgaName {
  63. if (userAvatar.length) {
  64. NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:[NSURL URLWithString:userAvatar]];
  65. UIImage *image = [[SDImageCache sharedImageCache] imageFromCacheForKey:key];
  66. if (image) {
  67. [self reloadAvatarAnimation:image svgaName:svgaName];
  68. }else {
  69. // 为了快进进入房间,不下载图片
  70. [self reloadAvatarAnimation:placeholderAppIconImage() svgaName:svgaName];
  71. // [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:UrlForString(userAvatar) completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
  72. // if (image) {
  73. // [self reloadAvatarAnimation:image svgaName:svgaName];
  74. // }else {
  75. // [self reloadAvatarAnimation:placeholderAppIconImage() svgaName:svgaName];
  76. // }
  77. // }];
  78. }
  79. }else{
  80. [self reloadAvatarAnimation:placeholderAppIconImage() svgaName:svgaName];
  81. }
  82. }
  83. // 启动动画
  84. - (void)reloadAvatarAnimation:(UIImage *)image svgaName:(NSString *)svgaName {
  85. image = [UIImage compressImageWith:image scaleToSize:CGSizeMake(80.0f, 80.0f)];
  86. UIImage *cornerImage = [UIImage imageWithClipImage:image];
  87. [self.SVGAParser parseWithNamed:svgaName inBundle:nil completionBlock:^(SVGAVideoEntity * _Nonnull videoItem) {
  88. [self.SVGAPlayer setImage:cornerImage forKey:@"roomCover"];
  89. self.SVGAPlayer.videoItem = videoItem;
  90. [UIView animateWithDuration:.3 animations:^{
  91. self.SVGAPlayer.transform = CGAffineTransformMakeScale(1, 1);
  92. } completion:^(BOOL finished) {
  93. // 开始动画
  94. [self.SVGAPlayer startAnimation];
  95. if (self.animationCompletionBlock) {
  96. self.animationCompletionBlock();
  97. }
  98. }];
  99. } failureBlock:^(NSError * _Nullable error) {
  100. [UIView animateWithDuration:.3 animations:^{
  101. self.SVGAPlayer.transform = CGAffineTransformMakeScale(1, 1);
  102. } completion:^(BOOL finished) {
  103. // 开始动画
  104. [self.SVGAPlayer startAnimation];
  105. if (self.animationCompletionBlock) {
  106. self.animationCompletionBlock();
  107. }
  108. }];
  109. }];
  110. }
  111. - (void)clear {
  112. if (_upBgView) {
  113. [_upBgView removeFromSuperview];
  114. _upBgView = nil;
  115. }
  116. if (_downBgView) {
  117. [_downBgView removeFromSuperview];
  118. _downBgView = nil;
  119. }
  120. if (_SVGAPlayer) {
  121. [_SVGAPlayer stopAnimation];
  122. [_SVGAPlayer clear];
  123. [_SVGAPlayer removeFromSuperview];
  124. _SVGAPlayer = nil;
  125. }
  126. [self removeFromSuperview];
  127. }
  128. #pragma mark - Action
  129. #pragma mark - 初始化
  130. - (instancetype)initWithFrame:(CGRect)frame {
  131. if (self = [super initWithFrame:frame]) {
  132. [self _setup];
  133. [self _setupSubViews];
  134. [self _makeSubViewsConstraint];
  135. }
  136. return self;
  137. }
  138. #pragma mark - 配置属性
  139. - (void)_setup {
  140. self.backgroundColor = Color_Clear;
  141. }
  142. #pragma mark - 设置子控件
  143. - (void)_setupSubViews {
  144. [self addSubview:self.upBgView];
  145. [self addSubview:self.downBgView];
  146. [self addSubview:self.SVGAPlayer];
  147. }
  148. #pragma mark - 布局子控件
  149. - (void)_makeSubViewsConstraint {
  150. }
  151. #pragma mark - Getter/Setter
  152. - (UIView *)upBgView {
  153. if (!_upBgView) {
  154. _upBgView = [[UIView alloc] initWithFrame:CGRectMake(0, -self.f_heigh/2, self.f_width, self.f_heigh/2)];
  155. _upBgView.backgroundColor = ColorFromHexStringWithAlpha(@"#000000", 0.4f);
  156. }
  157. return _upBgView;
  158. }
  159. - (UIView *)downBgView {
  160. if (!_downBgView) {
  161. _downBgView = [[UIView alloc] initWithFrame:CGRectMake(0, self.f_heigh, self.f_width, self.f_heigh/2)];
  162. _downBgView.backgroundColor = ColorFromHexStringWithAlpha(@"#000000", 0.4f);
  163. }
  164. return _downBgView;
  165. }
  166. - (SVGAPlayer *)SVGAPlayer {
  167. if (!_SVGAPlayer) {
  168. CGFloat itemWidth = 280.0f;
  169. _SVGAPlayer = [[SVGAPlayer alloc] initWithFrame:CGRectMake((self.f_width-itemWidth)/2, (self.f_heigh-itemWidth)/2, itemWidth, itemWidth)];
  170. _SVGAPlayer.loops = -1;
  171. _SVGAPlayer.clearsAfterStop = YES;
  172. _SVGAPlayer.contentMode = UIViewContentModeScaleAspectFill;
  173. _SVGAPlayer.backgroundColor = Color_Clear;
  174. _SVGAPlayer.transform = CGAffineTransformMakeScale(0, 0);
  175. }
  176. return _SVGAPlayer;
  177. }
  178. - (SVGAParser *)SVGAParser {
  179. if (!_SVGAParser) {
  180. _SVGAParser = [[SVGAParser alloc] init];
  181. }
  182. return _SVGAParser;
  183. }
  184. @end