Files
yinmeng-ios/xplan-ios/Base/UI/XCHUDTool.m

285 lines
9.3 KiB
Mathematica
Raw Normal View History

2021-09-06 18:47:38 +08:00
//
// 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>
#define kDelayTime 2.5
@implementation XCHUDTool
/**
HUD, viewnil, 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];
}
/**
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];
}
/**
viewGIFLoading
@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;
});
}
#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,130,130);
loadingImageView.frame = CGRectMake((CGRectGetWidth(loadingBGView.frame)-40)/ 2, (CGRectGetHeight(loadingBGView.frame) - 40 -15-15) / 2, 40, 40);
loadingTitleLabel.frame = CGRectMake(0, CGRectGetMaxY(loadingImageView.frame) + 15, loadingBGView.frame.size.width, 15);
loadingImageView.image = [UIImage imageNamed:@"common_loading"];
CABasicAnimation * animage = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
animage.fromValue = 0;
animage.toValue = @(2 * M_PI);
animage.duration = 1;
animage.repeatCount = CGFLOAT_MAX;
animage.autoreverses = NO;
animage.removedOnCompletion = NO;
[loadingImageView.layer addAnimation:animage forKey:@"animation"];
return loadingBGView;
}
@end