123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- //
- // XYChatRoomShowAnimationView.m
- // Timi
- //
- // Created by 翟玉磊 on 2021/1/4.
- //
- #import "XYChatRoomShowAnimationView.h"
- #import <SVGAPlayer/SVGA.h>
- #import <SDWebImage/SDWebImage.h>
- typedef void(^AnimationCompletionBlock)(void);
- @interface XYChatRoomShowAnimationView ()
- @property (nonatomic, strong) UIView *upBgView;
- @property (nonatomic, strong) UIView *downBgView;
- @property (nonatomic, strong) SVGAPlayer *SVGAPlayer;
- @property (nonatomic, strong) SVGAParser *SVGAParser;
- @property (nonatomic, copy) AnimationCompletionBlock animationCompletionBlock;
- @end
- @implementation XYChatRoomShowAnimationView
- #pragma mark - Public
- - (void)showChatRoomAnimationWithRoomCoverUrl:(NSString *)coverUrl completion:(void(^ __nullable)(void))completion; {
- self.animationCompletionBlock = completion;
- [[self topWindow] addSubview:self];
- [UIView animateWithDuration:.3 animations:^{
- self.upBgView.frame = CGRectMake(self.upBgView.f_x, 0, self.upBgView.f_width, self.upBgView.f_heigh);
- self.downBgView.frame = CGRectMake(self.downBgView.f_x, self.f_heigh/2, self.downBgView.f_width, self.downBgView.f_heigh);
- } completion:^(BOOL finished) {
- [self reloadAvatarWithUrl:coverUrl svgaName:@"xy_room_show_loading"];
- }];
- }
- - (void)dismissCompletion:(void(^ __nullable)(void))completion {
- [UIView animateWithDuration:.3 animations:^{
- self.upBgView.frame = CGRectMake(self.upBgView.f_x, -self.f_heigh/2, self.upBgView.f_width, self.upBgView.f_heigh);
- self.downBgView.frame = CGRectMake(self.downBgView.f_x, self.f_heigh, self.downBgView.f_width, self.downBgView.f_heigh);
- self.SVGAPlayer.transform = CGAffineTransformMakeScale(0.01, 0.01);
- } completion:^(BOOL finished) {
- [self clear];
- dispatch_async(dispatch_get_main_queue(), ^{
- if (completion) {
- completion();
- }
- });
- }];
- }
- // 获取最上层显示的window
- - (UIWindow *)topWindow {
- UIWindow *topWindow = nil;
- NSArray <UIWindow *>*windows = [UIApplication sharedApplication].windows;
- for (UIWindow *window in windows.reverseObjectEnumerator) {
- if (window.hidden == YES || window.opaque == NO) {
- // 隐藏的 或者 透明的 跳过
- continue;
- }
- if (CGRectEqualToRect(window.bounds, [UIScreen mainScreen].bounds) == NO) {
- // 不是全屏的 跳过
- continue;
- }
- topWindow = window;
- break;
- }
- return topWindow?:[UIApplication sharedApplication].delegate.window;
- }
- - (void)reloadAvatarWithUrl:(NSString *)userAvatar svgaName:(NSString *)svgaName {
- if (userAvatar.length) {
- NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:[NSURL URLWithString:userAvatar]];
- UIImage *image = [[SDImageCache sharedImageCache] imageFromCacheForKey:key];
- if (image) {
- [self reloadAvatarAnimation:image svgaName:svgaName];
- }else {
- // 为了快进进入房间,不下载图片
- [self reloadAvatarAnimation:placeholderAppIconImage() svgaName:svgaName];
- // [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:UrlForString(userAvatar) completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
- // if (image) {
- // [self reloadAvatarAnimation:image svgaName:svgaName];
- // }else {
- // [self reloadAvatarAnimation:placeholderAppIconImage() svgaName:svgaName];
- // }
- // }];
- }
- }else{
- [self reloadAvatarAnimation:placeholderAppIconImage() svgaName:svgaName];
- }
- }
- // 启动动画
- - (void)reloadAvatarAnimation:(UIImage *)image svgaName:(NSString *)svgaName {
- image = [UIImage compressImageWith:image scaleToSize:CGSizeMake(80.0f, 80.0f)];
- UIImage *cornerImage = [UIImage imageWithClipImage:image];
- [self.SVGAParser parseWithNamed:svgaName inBundle:nil completionBlock:^(SVGAVideoEntity * _Nonnull videoItem) {
- [self.SVGAPlayer setImage:cornerImage forKey:@"roomCover"];
- self.SVGAPlayer.videoItem = videoItem;
-
- [UIView animateWithDuration:.3 animations:^{
- self.SVGAPlayer.transform = CGAffineTransformMakeScale(1, 1);
- } completion:^(BOOL finished) {
- // 开始动画
- [self.SVGAPlayer startAnimation];
- if (self.animationCompletionBlock) {
- self.animationCompletionBlock();
- }
- }];
- } failureBlock:^(NSError * _Nullable error) {
- [UIView animateWithDuration:.3 animations:^{
- self.SVGAPlayer.transform = CGAffineTransformMakeScale(1, 1);
- } completion:^(BOOL finished) {
- // 开始动画
- [self.SVGAPlayer startAnimation];
- if (self.animationCompletionBlock) {
- self.animationCompletionBlock();
- }
- }];
- }];
- }
- - (void)clear {
- if (_upBgView) {
- [_upBgView removeFromSuperview];
- _upBgView = nil;
- }
- if (_downBgView) {
- [_downBgView removeFromSuperview];
- _downBgView = nil;
- }
- if (_SVGAPlayer) {
- [_SVGAPlayer stopAnimation];
- [_SVGAPlayer clear];
- [_SVGAPlayer removeFromSuperview];
- _SVGAPlayer = nil;
- }
- [self removeFromSuperview];
- }
- #pragma mark - Action
- #pragma mark - 初始化
- - (instancetype)initWithFrame:(CGRect)frame {
-
- if (self = [super initWithFrame:frame]) {
-
- [self _setup];
- [self _setupSubViews];
- [self _makeSubViewsConstraint];
- }
- return self;
- }
- #pragma mark - 配置属性
- - (void)_setup {
- self.backgroundColor = Color_Clear;
- }
- #pragma mark - 设置子控件
- - (void)_setupSubViews {
- [self addSubview:self.upBgView];
- [self addSubview:self.downBgView];
- [self addSubview:self.SVGAPlayer];
- }
- #pragma mark - 布局子控件
- - (void)_makeSubViewsConstraint {
-
- }
- #pragma mark - Getter/Setter
- - (UIView *)upBgView {
- if (!_upBgView) {
- _upBgView = [[UIView alloc] initWithFrame:CGRectMake(0, -self.f_heigh/2, self.f_width, self.f_heigh/2)];
- _upBgView.backgroundColor = ColorFromHexStringWithAlpha(@"#000000", 0.4f);
- }
- return _upBgView;
- }
- - (UIView *)downBgView {
- if (!_downBgView) {
- _downBgView = [[UIView alloc] initWithFrame:CGRectMake(0, self.f_heigh, self.f_width, self.f_heigh/2)];
- _downBgView.backgroundColor = ColorFromHexStringWithAlpha(@"#000000", 0.4f);
- }
- return _downBgView;
- }
- - (SVGAPlayer *)SVGAPlayer {
- if (!_SVGAPlayer) {
- CGFloat itemWidth = 280.0f;
- _SVGAPlayer = [[SVGAPlayer alloc] initWithFrame:CGRectMake((self.f_width-itemWidth)/2, (self.f_heigh-itemWidth)/2, itemWidth, itemWidth)];
- _SVGAPlayer.loops = -1;
- _SVGAPlayer.clearsAfterStop = YES;
- _SVGAPlayer.contentMode = UIViewContentModeScaleAspectFill;
- _SVGAPlayer.backgroundColor = Color_Clear;
- _SVGAPlayer.transform = CGAffineTransformMakeScale(0, 0);
- }
- return _SVGAPlayer;
- }
- - (SVGAParser *)SVGAParser {
- if (!_SVGAParser) {
- _SVGAParser = [[SVGAParser alloc] init];
- }
- return _SVGAParser;
- }
- @end
|