修改hud

This commit is contained in:
2025-10-29 19:13:35 +08:00
parent e218c1bf3d
commit e8c88a6148
19 changed files with 2218 additions and 1973 deletions

View File

@@ -0,0 +1,64 @@
//
// KBHUD.h
// keyBoard
//
// 基于 MBProgressHUD 的轻量封装,提供类似 SVProgressHUD 的类方法调用方式。
// 特性:
// - 默认加到 KeyWindow 上
// - 支持遮罩模式(不拦截/透明拦截/半透明黑遮罩)
// - 默认不支持点击背景关闭;可为“本次显示/当前 HUD”开启点击关闭
// - 文案/加载/进度/自动消失
//
#import <Foundation/Foundation.h>
typedef NS_ENUM(NSUInteger, KBHUDMaskType) {
/// 不加遮罩,事件可透传到后面的视图(与 SVProgressHUDMaskTypeNone 类似)
KBHUDMaskTypeNone = 0,
/// 透明遮罩,拦截触摸但不变暗(与 SVProgressHUDMaskTypeClear 类似)
KBHUDMaskTypeClear,
/// 半透明黑色遮罩(与 SVProgressHUDMaskTypeBlack 类似)
KBHUDMaskTypeBlack,
};
NS_ASSUME_NONNULL_BEGIN
@interface KBHUD : NSObject
#pragma mark - 显示
/// 纯菊花
+ (void)show;
/// 菊花 + 文案
+ (void)showWithStatus:(nullable NSString *)status;
/// 菊花 + 文案(可配置:是否允许点击背景关闭)
+ (void)showWithStatus:(nullable NSString *)status allowTapToDismiss:(BOOL)allow;
/// 圆形进度 0~1 + 文案
+ (void)showProgress:(float)progress status:(nullable NSString *)status;
/// 圆形进度 0~1 + 文案(可配置:是否允许点击背景关闭)
+ (void)showProgress:(float)progress status:(nullable NSString *)status allowTapToDismiss:(BOOL)allow;
/// 文案提示(自动隐藏)
+ (void)showInfo:(NSString *)status;
/// 成功提示(自动隐藏)
+ (void)showSuccess:(NSString *)status;
/// 失败提示(自动隐藏)
+ (void)showError:(NSString *)status;
#pragma mark - 隐藏
/// 立即隐藏
+ (void)dismiss;
/// 延时隐藏
+ (void)dismissWithDelay:(NSTimeInterval)delay;
#pragma mark - 配置(全局)
/// 设置默认遮罩类型,默认 KBHUDMaskTypeClear
+ (void)setDefaultMaskType:(KBHUDMaskType)maskType;
/// 为“当前正在展示的 HUD”设置点击背景是否关闭若当前没有 HUD 则忽略
+ (void)setTapToDismissEnabled:(BOOL)enabled;
/// 仅本次显示:菊花(无文案)可配置是否允许点击背景关闭
+ (void)showAllowTapToDismiss:(BOOL)allow;
/// 设置自动隐藏的时长success/error/info默认 1.2s
+ (void)setAutoDismissInterval:(NSTimeInterval)interval;
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,165 @@
//
// KBHUD.m
// keyBoard
//
#import "KBHUD.h"
#import <MBProgressHUD/MBProgressHUD.h>
#ifndef KBSCREEN
#define KBSCREEN [UIScreen mainScreen].bounds.size
#endif
@implementation KBHUD
static __weak MBProgressHUD *sHUD = nil;
static KBHUDMaskType sMaskType = KBHUDMaskTypeClear; //
static BOOL sDefaultTapToDismiss = NO; //
static NSTimeInterval sAutoDismiss = 1.2;
#pragma mark - Private Helpers
+ (void)onMain:(dispatch_block_t)blk {
if (NSThread.isMainThread) { blk(); } else { dispatch_async(dispatch_get_main_queue(), blk); }
}
+ (MBProgressHUD *)ensureHUDWithMask:(KBHUDMaskType)mask tap:(BOOL)tap {
MBProgressHUD *hud = sHUD;
if (!hud) {
UIWindow *win = KB_KeyWindow();
if (!win) { win = UIApplication.sharedApplication.windows.firstObject; }
hud = [MBProgressHUD showHUDAddedTo:win animated:YES];
sHUD = hud;
// 仿 SVProgressHUD
hud.removeFromSuperViewOnHide = YES;
hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
hud.bezelView.layer.cornerRadius = 12.0;
hud.bezelView.color = [UIColor colorWithWhite:0 alpha:0.8];
hud.contentColor = UIColor.whiteColor;
hud.margin = 16;
hud.label.numberOfLines = 0;
hud.detailsLabel.numberOfLines = 0;
}
// show
[self applyMaskType:mask hud:hud];
[self applyTapToDismiss:tap hud:hud];
return hud;
}
+ (void)applyMaskType:(KBHUDMaskType)type hud:(MBProgressHUD *)hud {
switch (type) {
case KBHUDMaskTypeNone: {
hud.userInteractionEnabled = NO; //
hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
hud.backgroundView.color = UIColor.clearColor;
} break;
case KBHUDMaskTypeClear: {
hud.userInteractionEnabled = YES; //
hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
hud.backgroundView.color = [UIColor colorWithWhite:0 alpha:0.01];
} break;
case KBHUDMaskTypeBlack: {
hud.userInteractionEnabled = YES; //
hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
hud.backgroundView.color = [UIColor colorWithWhite:0 alpha:0.35];
} break;
}
}
+ (void)applyTapToDismiss:(BOOL)enabled hud:(MBProgressHUD *)hud {
UIView *bg = hud.backgroundView;
//
for (UIGestureRecognizer *g in bg.gestureRecognizers.copy) {
if ([g isKindOfClass:UITapGestureRecognizer.class] && g.view == bg) {
[bg removeGestureRecognizer:g];
}
}
if (enabled) {
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(_kb_handleTap)];
[bg addGestureRecognizer:tap];
//
hud.userInteractionEnabled = YES;
if (hud.backgroundView.color == UIColor.clearColor) {
hud.backgroundView.color = [UIColor colorWithWhite:0 alpha:0.01];
}
}
}
+ (void)_kb_handleTap {
[self dismiss];
}
+ (void)_showText:(NSString *)text icon:(nullable UIImage *)icon {
[self onMain:^{
MBProgressHUD *hud = [self ensureHUDWithMask:sMaskType tap:sDefaultTapToDismiss];
hud.mode = icon ? MBProgressHUDModeCustomView : MBProgressHUDModeText;
hud.label.text = text ?: @"";
hud.detailsLabel.text = nil;
if (icon) {
UIImageView *iv = [[UIImageView alloc] initWithImage:icon];
hud.customView = iv;
} else {
hud.customView = nil;
}
[hud hideAnimated:YES afterDelay:sAutoDismiss];
}];
}
#pragma mark - Public API
+ (void)show { [self showWithStatus:nil]; }
+ (void)showWithStatus:(NSString *)status { [self showWithStatus:status allowTapToDismiss:NO]; }
+ (void)showWithStatus:(NSString *)status allowTapToDismiss:(BOOL)allow {
[self onMain:^{
MBProgressHUD *hud = [self ensureHUDWithMask:sMaskType tap:allow];
hud.mode = MBProgressHUDModeIndeterminate;
hud.label.text = status ?: @"";
}];
}
+ (void)showProgress:(float)progress status:(NSString *)status {
[self showProgress:progress status:status allowTapToDismiss:NO];
}
+ (void)showProgress:(float)progress status:(NSString *)status allowTapToDismiss:(BOOL)allow {
[self onMain:^{
MBProgressHUD *hud = [self ensureHUDWithMask:sMaskType tap:allow];
hud.mode = MBProgressHUDModeDeterminate;
hud.progress = progress;
hud.label.text = status ?: @"";
}];
}
+ (void)showInfo:(NSString *)status {
[self _showText:status icon:nil];
}
+ (void)showSuccess:(NSString *)status {
// 使
[self _showText:status ?: @"成功" icon:nil];
}
+ (void)showError:(NSString *)status { [self _showText:status ?: @"失败" icon:nil]; }
+ (void)dismiss { [self onMain:^{ [sHUD hideAnimated:YES]; }]; }
+ (void)dismissWithDelay:(NSTimeInterval)delay { [self onMain:^{ [sHUD hideAnimated:YES afterDelay:delay]; }]; }
#pragma mark - Config
+ (void)setDefaultMaskType:(KBHUDMaskType)maskType { sMaskType = maskType; }
+ (void)setTapToDismissEnabled:(BOOL)enabled {
// HUD B A
[self onMain:^{
if (sHUD) { [self applyTapToDismiss:enabled hud:sHUD]; }
}];
}
+ (void)setAutoDismissInterval:(NSTimeInterval)interval { sAutoDismiss = MAX(0.2, interval); }
+ (void)showAllowTapToDismiss:(BOOL)allow {
[self showWithStatus:nil allowTapToDismiss:allow];
}
@end