375 lines
12 KiB
Objective-C
375 lines
12 KiB
Objective-C
//
|
|
// XCHUDTool.m
|
|
// TTPlay
|
|
//
|
|
// Created by Macx on 2019/5/15.
|
|
// Copyright © 2019 YiZhuan. All rights reserved.
|
|
//
|
|
|
|
#import "XCHUDTool.h"
|
|
#import "GCDHelper.h"
|
|
#import <MBProgressHUD/MBProgressHUD.h>
|
|
#import "ThemeColor.h"
|
|
|
|
#define kDelayTime 2.5
|
|
@interface XCHUDTool ()
|
|
///
|
|
@property (class,nonatomic,copy) NSArray *animationImages;
|
|
@end
|
|
|
|
@implementation XCHUDTool
|
|
|
|
static NSArray * _animationImages = nil;
|
|
|
|
/**
|
|
隐藏HUD, 如果view为nil, 则默认隐藏主窗口的HUD
|
|
|
|
@param view view
|
|
*/
|
|
+ (void)hideHUDInView:(nullable UIView *)view {
|
|
dispatch_main_sync_safe(^{
|
|
if (view) {
|
|
[MBProgressHUD hideHUDForView:view animated:NO];
|
|
[MBProgressHUD hideHUDForView:[UIApplication sharedApplication].keyWindow animated:NO];
|
|
} else {
|
|
[MBProgressHUD hideHUDForView:[UIApplication sharedApplication].keyWindow animated:NO];
|
|
}
|
|
});
|
|
}
|
|
/**
|
|
隐藏HUD
|
|
*/
|
|
+ (void)hideHUD {
|
|
[self hideHUDInView:nil];
|
|
}
|
|
|
|
/**
|
|
显示成功message, 默认显示在窗口上, 2.5s后消失, 默认不拦截点击事件
|
|
|
|
@param message 文字
|
|
*/
|
|
+ (void)showSuccessWithMessage:(NSString *)message {
|
|
[self showSuccessWithMessage:message inView:[UIApplication sharedApplication].keyWindow];
|
|
}
|
|
|
|
/**
|
|
显示成功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];
|
|
}
|
|
|
|
/**
|
|
显示成功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 {
|
|
|
|
if (message.length == 0) { return; }
|
|
__block UIView *inView = view;
|
|
|
|
dispatch_main_sync_safe(^{
|
|
if (!inView) {
|
|
inView = [UIApplication sharedApplication].keyWindow;
|
|
}
|
|
[self hideHUDInView:view]; // 先隐藏
|
|
MBProgressHUD *hud = [self normalProgressHUD:view];
|
|
hud.userInteractionEnabled = enabled;
|
|
hud.mode = MBProgressHUDModeText;
|
|
hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
|
|
hud.margin = 8;
|
|
// 方框背景颜色
|
|
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];
|
|
});
|
|
}
|
|
|
|
/**
|
|
显示错误message, 默认显示在窗口上, 2.5s后消失, 默认不拦截点击事件
|
|
|
|
@param message 文字
|
|
*/
|
|
+ (void)showErrorWithMessage:(NSString *)message {
|
|
[self showErrorWithMessage:message inView:[UIApplication sharedApplication].keyWindow];
|
|
}
|
|
|
|
/**
|
|
显示错误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];
|
|
}
|
|
|
|
/**
|
|
显示错误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 {
|
|
if (message.length == 0) { return; }
|
|
if (!view) {
|
|
view = [UIApplication sharedApplication].keyWindow;
|
|
}
|
|
|
|
[self hideHUDInView:view]; // 先隐藏
|
|
|
|
dispatch_main_sync_safe(^{
|
|
MBProgressHUD *hud = [self normalProgressHUD:view];
|
|
hud.userInteractionEnabled = enabled;
|
|
hud.mode = MBProgressHUDModeText;
|
|
hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
|
|
hud.margin = 8;
|
|
// 方框背景颜色
|
|
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];
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 在窗口上显示菊花
|
|
*/
|
|
+ (void)showLoading {
|
|
[self showLoadingInView:[UIApplication sharedApplication].keyWindow];
|
|
}
|
|
|
|
/**
|
|
* 在view上显示菊花
|
|
*/
|
|
+ (void)showLoadingInView:(nullable UIView *)view {
|
|
[self showLoadingInView:view enabled:YES];
|
|
}
|
|
|
|
/**
|
|
* 在view上显示菊花
|
|
*/
|
|
+ (void)showLoadingInView:(nullable UIView *)view enabled:(BOOL)enabled {
|
|
[self showLoadingWithMessage:@"" inView:view enabled:enabled];
|
|
}
|
|
|
|
/**
|
|
* 在窗口上显示菊花+文字
|
|
*/
|
|
+ (void)showLoadingWithMessage:(NSString *)message {
|
|
[self showLoadingWithMessage:message inView:[UIApplication sharedApplication].keyWindow];
|
|
}
|
|
|
|
/**
|
|
* 在view上显示菊花+文字
|
|
*/
|
|
+ (void)showLoadingWithMessage:(NSString *)message inView:(nullable UIView *)view {
|
|
[self showLoadingWithMessage:message inView:view enabled:YES];
|
|
}
|
|
|
|
/**
|
|
* 在view上显示菊花+文字
|
|
*/
|
|
+ (void)showLoadingWithMessage:(NSString *)message inView:(nullable UIView *)view enabled:(BOOL)enabled {
|
|
if (!view) {
|
|
view = [UIApplication sharedApplication].keyWindow;
|
|
}
|
|
[self hideHUDInView:view];
|
|
|
|
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];
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
/**
|
|
在窗口上显示自定义GIFLoading, 背景默认黑色0.35透明度, 默认拦截点击事件
|
|
*/
|
|
+ (void)showGIFLoading {
|
|
[self showGIFLoadingInView:[UIApplication sharedApplication].keyWindow];
|
|
}
|
|
|
|
/**
|
|
在指定的view上显示自定义GIFLoading, 背景默认黑色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];
|
|
}
|
|
|
|
/**
|
|
在指定的view上显示自定义GIFLoading
|
|
@param view 显示在哪个view上
|
|
@param bgColor 背景颜色, 遮盖
|
|
@param enabled 是否可以拦截事件 no:不拦截 yes:拦截
|
|
*/
|
|
+ (void)showGIFLoadingInView:(nullable UIView *)view bgColor:(nullable UIColor *)bgColor enabled:(BOOL)enabled {
|
|
if (!view) {
|
|
view = [UIApplication sharedApplication].keyWindow;
|
|
}
|
|
[self hideHUDInView:view];
|
|
dispatch_main_sync_safe(^{
|
|
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
|
|
hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
|
|
hud.minSize = CGSizeMake(168, 133);
|
|
hud.userInteractionEnabled = enabled;
|
|
hud.mode = MBProgressHUDModeCustomView;
|
|
hud.minSize = CGSizeMake(150, 150);
|
|
[hud.bezelView addSubview:[self loadingView]];
|
|
hud.backgroundColor = bgColor;
|
|
hud.bezelView.color = [UIColor clearColor];
|
|
hud.removeFromSuperViewOnHide = YES;
|
|
});
|
|
}
|
|
|
|
|
|
+ (void)showAnchorLoading {
|
|
UIView *view = [UIApplication sharedApplication].keyWindow;
|
|
dispatch_main_sync_safe(^{
|
|
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
|
|
hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
|
|
hud.userInteractionEnabled = NO;
|
|
hud.mode = MBProgressHUDModeCustomView;
|
|
hud.minSize = CGSizeMake(240, 180 + 16 + 30);
|
|
[hud.bezelView addSubview:[self anchorSwitchLoadingView]];
|
|
hud.bezelView.color = [UIColor clearColor];
|
|
hud.removeFromSuperViewOnHide = YES;
|
|
});
|
|
}
|
|
|
|
#pragma mark - private
|
|
+ (MBProgressHUD *)normalProgressHUD:(UIView *)view {
|
|
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
|
|
hud.removeFromSuperViewOnHide = YES;
|
|
return hud;
|
|
}
|
|
|
|
|
|
|
|
+ (UIView *)loadingView {
|
|
UIView *loadingBGView = [[UIView alloc] init];
|
|
loadingBGView.layer.cornerRadius = 20;
|
|
loadingBGView.backgroundColor = [UIColor whiteColor];
|
|
UIImageView *loadingImageView = [[UIImageView alloc] init];
|
|
|
|
UILabel *loadingTitleLabel = [[UILabel alloc] init];
|
|
loadingTitleLabel = [[UILabel alloc] init];
|
|
loadingTitleLabel.textColor = [UIColor colorWithRed:(153.0)/255.0f green:(153.0)/255.0f blue:(153.0)/255.0f alpha:1];
|
|
loadingTitleLabel.font = [UIFont systemFontOfSize:14];
|
|
loadingTitleLabel.text = @"加载中…";
|
|
loadingTitleLabel.textAlignment = NSTextAlignmentCenter;
|
|
|
|
[loadingBGView addSubview:loadingImageView];
|
|
[loadingBGView addSubview:loadingTitleLabel];
|
|
|
|
loadingBGView.frame = CGRectMake(0,0,120,120);
|
|
loadingImageView.frame = CGRectMake((CGRectGetWidth(loadingBGView.frame)-80)/ 2, 10, 80, 80);
|
|
loadingTitleLabel.frame = CGRectMake(0, CGRectGetMaxY(loadingImageView.frame) + 8, loadingBGView.frame.size.width, 15);
|
|
|
|
loadingImageView.animationImages = [self loadingAnimationImages];
|
|
[loadingImageView startAnimating];
|
|
return loadingBGView;
|
|
}
|
|
|
|
+ (NSArray<UIImage *> *)loadingAnimationImages {
|
|
|
|
if (XCHUDTool.animationImages.count > 0) {
|
|
return XCHUDTool.animationImages;
|
|
}
|
|
NSString * countString = @"00";
|
|
NSMutableArray * imageArray= [NSMutableArray array];
|
|
for (int i = 0; i <= 27; i++) {
|
|
if (i < 10) {
|
|
countString = [NSString stringWithFormat:@"0%d", i];
|
|
} else {
|
|
countString = [NSString stringWithFormat:@"%d", i];
|
|
}
|
|
NSString * loadingImageName = [NSString stringWithFormat:@"loading_0%@", countString];
|
|
UIImage * image = [UIImage imageNamed:loadingImageName];
|
|
[imageArray addObject:image];
|
|
}
|
|
NSArray * array = [imageArray copy];
|
|
[XCHUDTool setAnimationImages:array];
|
|
return array;
|
|
}
|
|
|
|
+ (UIView *)anchorSwitchLoadingView {
|
|
UIView *loadingBGView = [[UIView alloc] init];
|
|
loadingBGView.layer.cornerRadius = 20;
|
|
loadingBGView.backgroundColor = [UIColor clearColor];
|
|
UIImageView *loadingImageView = [[UIImageView alloc] init];
|
|
|
|
UILabel *loadingTitleLabel = [[UILabel alloc] init];
|
|
loadingTitleLabel = [[UILabel alloc] init];
|
|
loadingTitleLabel.textColor = UIColorFromRGB(0xC9BCF2);
|
|
loadingTitleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
|
|
loadingTitleLabel.text = @"加载中…";
|
|
loadingTitleLabel.textAlignment = NSTextAlignmentCenter;
|
|
|
|
[loadingBGView addSubview:loadingImageView];
|
|
[loadingBGView addSubview:loadingTitleLabel];
|
|
|
|
loadingBGView.frame = CGRectMake(0,0,240,180 + 16 + 30);
|
|
loadingImageView.frame = CGRectMake(0, 0, 240, 180);
|
|
loadingTitleLabel.frame = CGRectMake(0, CGRectGetMaxY(loadingImageView.frame) + 8, loadingBGView.frame.size.width, 15);
|
|
loadingImageView.animationImages = [self loadingAnchorAnimationImages];
|
|
[loadingImageView startAnimating];
|
|
return loadingBGView;
|
|
}
|
|
|
|
+ (NSArray<UIImage *> *)loadingAnchorAnimationImages {
|
|
NSString * countString = @"00";
|
|
NSMutableArray * imageArray= [NSMutableArray array];
|
|
for (int i = 0; i <= 23; i++) {
|
|
if (i < 10) {
|
|
countString = [NSString stringWithFormat:@"0%d", i];
|
|
} else {
|
|
countString = [NSString stringWithFormat:@"%d", i];
|
|
}
|
|
NSString * loadingImageName = [NSString stringWithFormat:@"anchorLoading_0%@", countString];
|
|
UIImage * image = [UIImage imageNamed:loadingImageName];
|
|
[imageArray addObject:image];
|
|
}
|
|
NSArray * array = [imageArray copy];
|
|
return array;
|
|
}
|
|
|
|
+ (NSArray *)animationImages {
|
|
if (_animationImages == nil) {
|
|
_animationImages = [NSArray array];
|
|
}
|
|
return _animationImages;
|
|
}
|
|
|
|
+ (void)setAnimationImages:(NSArray *)animationImages {
|
|
if (animationImages != _animationImages) {
|
|
_animationImages = animationImages;
|
|
}
|
|
}
|
|
|
|
|
|
@end
|