Files
peko-ios/YuMi/CustomUI/XNDJTDDLoadingTool.m

397 lines
12 KiB
Mathematica
Raw Normal View History

2023-07-06 16:54:13 +08:00
//
// YMHUDTool.m
// TTPlay
//
// Created by YM on 2022/5/15.
// Copyright © 2023 YUMI. All rights reserved.
//
2023-07-14 18:50:55 +08:00
#import "XNDJTDDLoadingTool.h"
2023-07-06 16:54:13 +08:00
#import "GCDHelper.h"
#import <MBProgressHUD/MBProgressHUD.h>
2023-08-10 16:25:34 +08:00
#import "MvpViewController.h"
#import <SVGA.h>
2023-07-06 16:54:13 +08:00
#define kDelayTime 2.5
2023-07-14 18:50:55 +08:00
@interface XNDJTDDLoadingTool ()
///
2023-07-06 16:54:13 +08:00
@property (class,nonatomic,copy) NSArray *animationImages;
@end
2023-07-14 18:50:55 +08:00
@implementation XNDJTDDLoadingTool
2023-07-06 16:54:13 +08:00
static NSArray * _animationImages = nil;
2023-07-14 18:50:55 +08:00
/**
HUD, viewnil, HUD
@param view view
*/
+ (void)hideHUDInView:(nullable UIView *)view {
2023-07-06 16:54:13 +08:00
dispatch_main_sync_safe(^{
if (view) {
2023-08-10 16:25:34 +08:00
MBProgressHUD *hud = [XNDJTDDLoadingTool HUDForView:view];
if (hud != nil && hud.mode != MBProgressHUDModeText) {
hud.removeFromSuperViewOnHide = YES;
[hud hideAnimated:NO];
}
MBProgressHUD *windowHud = [XNDJTDDLoadingTool HUDForView:kWindow];
if (windowHud != nil && windowHud.mode != MBProgressHUDModeText) {
windowHud.removeFromSuperViewOnHide = YES;
[windowHud hideAnimated:NO];
}
2023-07-06 16:54:13 +08:00
} else {
2023-08-10 16:25:34 +08:00
MBProgressHUD *windowHud = [XNDJTDDLoadingTool HUDForView:kWindow];
if (windowHud != nil && windowHud.mode != MBProgressHUDModeText) {
windowHud.removeFromSuperViewOnHide = YES;
[windowHud hideAnimated:NO];
}
2023-07-06 16:54:13 +08:00
}
});
}
2023-08-10 16:25:34 +08:00
+ (MBProgressHUD *)HUDForView:(UIView *)view {
NSEnumerator *subviewsEnum = [view.subviews reverseObjectEnumerator];
for (UIView *subview in subviewsEnum) {
if ([subview isKindOfClass:[MBProgressHUD class]]) {
MBProgressHUD *hud = (MBProgressHUD *)subview;
id hasFinished = [hud valueForKey:@"hasFinished"];
if (hasFinished != nil) {
if([hasFinished boolValue] == NO){
return hud;
}
}
}
}
return nil;
}
2023-07-14 18:50:55 +08:00
/**
HUD
*/
+ (void)hideHUD {
[self hideHUDInView:nil];
2023-07-06 16:54:13 +08:00
}
2023-07-14 18:50:55 +08:00
/**
message, , 2.5s,
@param message
*/
+ (void)showSuccessWithMessage:(NSString *)message {
[self showSuccessWithMessage:message inView:[UIApplication sharedApplication].keyWindow];
2023-07-06 16:54:13 +08:00
}
2023-07-14 18:50:55 +08:00
/**
message, 2.5s,
@param message
@param view view
*/
+ (void)showSuccessWithMessage:(NSString *)message inView:(nullable UIView *)view {
[self showSuccessWithMessage:message inView:view delay:kDelayTime enabled:NO];
2023-07-06 16:54:13 +08:00
}
2023-07-14 18:50:55 +08:00
/**
message
@param message
@param view view
@param afterDelay
@param enabled no: yes:
*/
+ (void)showSuccessWithMessage:(NSString *)message inView:(nullable UIView *)view delay:(NSTimeInterval)afterDelay enabled:(BOOL)enabled {
2023-07-06 16:54:13 +08:00
if (message.length == 0) { return; }
__block UIView *inView = view;
dispatch_main_sync_safe(^{
if (!inView) {
inView = [UIApplication sharedApplication].keyWindow;
}
2023-07-14 18:50:55 +08:00
[self hideHUDInView:view]; //
MBProgressHUD *hud = [self normalProgressHUD:view];
2023-07-06 16:54:13 +08:00
hud.userInteractionEnabled = enabled;
hud.mode = MBProgressHUDModeText;
hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
hud.margin = 8;
2023-07-14 18:50:55 +08:00
//
2023-07-06 16:54:13 +08:00
hud.bezelView.color = [[UIColor blackColor] colorWithAlphaComponent:0.8];
hud.label.text = message;
hud.label.numberOfLines = 0;
hud.label.textColor = [UIColor whiteColor];
hud.label.font = [UIFont systemFontOfSize:14];
[hud hideAnimated:YES afterDelay:afterDelay];
});
}
2023-07-14 18:50:55 +08:00
/**
message, , 2.5s,
@param message
*/
+ (void)showErrorWithMessage:(NSString *)message {
[self showErrorWithMessage:message inView:[UIApplication sharedApplication].keyWindow];
2023-07-06 16:54:13 +08:00
}
2023-07-14 18:50:55 +08:00
/**
message, 2.5s,
@param message
@param view view
*/
+ (void)showErrorWithMessage:(NSString *)message inView:(nullable UIView *)view {
[self showErrorWithMessage:message inView:view delay:kDelayTime enabled:NO];
2023-07-06 16:54:13 +08:00
}
2023-07-14 18:50:55 +08:00
/**
message
@param message
@param view view
@param afterDelay
@param enabled no: yes:
*/
+ (void)showErrorWithMessage:(NSString *)message inView:(nullable UIView *)view delay:(NSTimeInterval)afterDelay enabled:(BOOL)enabled {
2023-07-06 16:54:13 +08:00
if (message.length == 0) { return; }
if (!view) {
view = [UIApplication sharedApplication].keyWindow;
}
2023-07-14 18:50:55 +08:00
[self hideHUDInView:view]; //
2023-07-06 16:54:13 +08:00
dispatch_main_sync_safe(^{
2023-07-14 18:50:55 +08:00
MBProgressHUD *hud = [self normalProgressHUD:view];
2023-07-06 16:54:13 +08:00
hud.userInteractionEnabled = enabled;
hud.mode = MBProgressHUDModeText;
hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
hud.margin = 8;
2023-07-14 18:50:55 +08:00
//
2023-07-06 16:54:13 +08:00
hud.bezelView.color = [[UIColor blackColor] colorWithAlphaComponent:0.8];
hud.label.text = message;
hud.label.numberOfLines = 0;
hud.label.textColor = [UIColor whiteColor];
hud.label.font = [UIFont systemFontOfSize:14];
[hud hideAnimated:YES afterDelay:afterDelay];
});
}
2023-07-14 18:50:55 +08:00
/**
*
*/
+ (void)showLoading {
2023-08-10 16:25:34 +08:00
[self showGIFLoadingInView:[UIApplication sharedApplication].keyWindow];
return;
2023-07-14 18:50:55 +08:00
[self showLoadingInView:[UIApplication sharedApplication].keyWindow];
2023-07-06 16:54:13 +08:00
}
2023-07-14 18:50:55 +08:00
/**
* view
*/
+ (void)showLoadingInView:(nullable UIView *)view {
2023-08-10 16:25:34 +08:00
[self showGIFLoadingInView:[UIApplication sharedApplication].keyWindow];
return;
2023-07-14 18:50:55 +08:00
[self showLoadingInView:view enabled:YES];
2023-07-06 16:54:13 +08:00
}
2023-07-14 18:50:55 +08:00
/**
* view
*/
+ (void)showLoadingInView:(nullable UIView *)view enabled:(BOOL)enabled {
2023-08-10 16:25:34 +08:00
[self showGIFLoadingInView:[UIApplication sharedApplication].keyWindow];
return;
2023-07-14 18:50:55 +08:00
[self showLoadingWithMessage:@"" inView:view enabled:enabled];
2023-07-06 16:54:13 +08:00
}
2023-07-14 18:50:55 +08:00
/**
* +
*/
+ (void)showLoadingWithMessage:(NSString *)message {
2023-08-10 16:25:34 +08:00
2023-07-14 18:50:55 +08:00
[self showLoadingWithMessage:message inView:[UIApplication sharedApplication].keyWindow];
2023-07-06 16:54:13 +08:00
}
2023-07-14 18:50:55 +08:00
/**
* view+
*/
+ (void)showLoadingWithMessage:(NSString *)message inView:(nullable UIView *)view {
2023-08-10 16:25:34 +08:00
2023-07-14 18:50:55 +08:00
[self showLoadingWithMessage:message inView:view enabled:YES];
2023-07-06 16:54:13 +08:00
}
2023-07-14 18:50:55 +08:00
/**
* view+
*/
+ (void)showLoadingWithMessage:(NSString *)message inView:(nullable UIView *)view enabled:(BOOL)enabled {
2023-08-10 16:25:34 +08:00
2023-07-06 16:54:13 +08:00
if (!view) {
view = [UIApplication sharedApplication].keyWindow;
}
2023-07-14 18:50:55 +08:00
[self hideHUDInView:view];
2023-07-06 16:54:13 +08:00
dispatch_main_sync_safe(^{
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
hud.userInteractionEnabled = enabled;
hud.bezelView.color = [[UIColor whiteColor] colorWithAlphaComponent:0.8];
hud.removeFromSuperViewOnHide = YES;
if (message.length) {
hud.label.text = message;
hud.label.numberOfLines = 0;
hud.label.textColor = [UIColor blackColor];
hud.label.font = [UIFont systemFontOfSize:14];
}
});
}
2023-07-14 18:50:55 +08:00
/**
GIFLoading, 0.35,
*/
+ (void)showGIFLoading {
[self showGIFLoadingInView:[UIApplication sharedApplication].keyWindow];
2023-07-06 16:54:13 +08:00
}
2023-07-14 18:50:55 +08:00
/**
viewGIFLoading, 0.35,
@param view view
*/
+ (void)showGIFLoadingInView:(nullable UIView *)view {
[self showGIFLoadingInView:view bgColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.35] enabled:YES];
2023-07-06 16:54:13 +08:00
}
2023-07-14 18:50:55 +08:00
/**
viewGIFLoading
@param view view
@param bgColor ,
@param enabled no: yes:
*/
+ (void)showGIFLoadingInView:(nullable UIView *)view bgColor:(nullable UIColor *)bgColor enabled:(BOOL)enabled {
2023-07-06 16:54:13 +08:00
if (!view) {
view = [UIApplication sharedApplication].keyWindow;
}
2023-07-14 18:50:55 +08:00
[self hideHUDInView:view];
2023-07-06 16:54:13 +08:00
dispatch_main_sync_safe(^{
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
2023-08-10 16:25:34 +08:00
hud.minSize = CGSizeMake(100, 150);
hud.userInteractionEnabled = NO;
2023-07-06 16:54:13 +08:00
hud.mode = MBProgressHUDModeCustomView;
[hud.bezelView addSubview:[self loadingView]];
hud.backgroundColor = bgColor;
hud.bezelView.color = [UIColor clearColor];
hud.removeFromSuperViewOnHide = YES;
});
}
2023-07-14 18:50:55 +08:00
+ (void)showAnchorLoading {
2023-08-10 16:25:34 +08:00
2023-07-06 16:54:13 +08:00
UIView *view = [UIApplication sharedApplication].delegate.window;
dispatch_main_sync_safe(^{
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
hud.userInteractionEnabled = NO;
hud.mode = MBProgressHUDModeCustomView;
2023-08-10 16:25:34 +08:00
hud.minSize = CGSizeMake(100, 150);
[hud.bezelView addSubview:[self loadingView ]];
2023-07-06 16:54:13 +08:00
hud.bezelView.color = [UIColor clearColor];
hud.removeFromSuperViewOnHide = YES;
});
}
2023-07-14 18:50:55 +08:00
+ (void)showAnchorLoading:(UIView *)view {
2023-08-10 16:25:34 +08:00
dispatch_main_sync_safe(^{
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
hud.userInteractionEnabled = NO;
hud.mode = MBProgressHUDModeCustomView;
hud.minSize = CGSizeMake(100, 150);
[hud.bezelView addSubview:[self loadingView ]];
hud.bezelView.color = [UIColor clearColor];
hud.removeFromSuperViewOnHide = YES;
});
2023-07-06 16:54:13 +08:00
}
#pragma mark - private
2023-07-14 18:50:55 +08:00
+ (MBProgressHUD *)normalProgressHUD:(UIView *)view {
2023-07-06 16:54:13 +08:00
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
hud.removeFromSuperViewOnHide = YES;
return hud;
}
+ (UIView *)loadingView {
2023-08-10 16:25:34 +08:00
UIView *bgView = [UIView new];
bgView.backgroundColor = [UIColor clearColor];
bgView.frame = CGRectMake(0, 0, 100, 150);
SVGAParser *parser = [[SVGAParser alloc]init];
SVGAImageView* playView = [[SVGAImageView alloc]init];
playView.contentMode = UIViewContentModeScaleAspectFit;
playView.frame = CGRectMake(0, 0, 100,150);
playView.backgroundColor = [UIColor clearColor];
playView.userInteractionEnabled = NO;
2023-07-06 16:54:13 +08:00
2023-08-10 16:25:34 +08:00
[parser parseWithNamed:@"pi_new_loading" inBundle:[NSBundle mainBundle] completionBlock:^(SVGAVideoEntity * _Nonnull videoItem) {
2023-07-06 16:54:13 +08:00
2023-08-10 16:25:34 +08:00
playView.layer.masksToBounds = YES;
playView.contentMode = UIViewContentModeScaleAspectFill;
2023-07-06 16:54:13 +08:00
2023-08-10 16:25:34 +08:00
playView.videoItem = videoItem;
[playView startAnimation];
} failureBlock:^(NSError * _Nonnull error) {
}];
[bgView addSubview:playView];
return bgView;
2023-07-06 16:54:13 +08:00
}
2023-08-10 16:25:34 +08:00
+(void)hideOnlyView:(UIView *)view{
dispatch_main_sync_safe(^{
if (view) {
UIView *getView;
if([view isKindOfClass:[MvpViewController class]]){
getView = ((MvpViewController *)view).view;
}else{
getView = view;
}
MBProgressHUD *hud = [XNDJTDDLoadingTool HUDForView:getView];
if (hud != nil && hud.mode != MBProgressHUDModeText) {
hud.removeFromSuperViewOnHide = YES;
[hud hideAnimated:NO];
}
}
});
}
+(void)showOnlyView:(UIView *)view{
if (!view) {
view = [UIApplication sharedApplication].keyWindow;
}
[self hideOnlyView:view];
dispatch_main_sync_safe(^{
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
hud.minSize = CGSizeMake(100, 150);
hud.userInteractionEnabled = NO;
hud.mode = MBProgressHUDModeCustomView;
[hud.bezelView addSubview:[self loadingView]];
hud.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.35];
hud.bezelView.color = [UIColor clearColor];
hud.removeFromSuperViewOnHide = YES;
});
2023-07-06 16:54:13 +08:00
}
@end