iOS 之MBProgressHUD的简单封装
来源:互联网 发布:听考研英语单词软件 编辑:程序博客网 时间:2024/06/05 07:06
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
typedef void (^ AfterHudDisappearBlock) (void);
@interface AXProgressHUDHelper : NSObject
@property (nonatomic,assign) CGFloat autoHideTime;
+ (AXProgressHUDHelper*)getInstance;
// 在window上显示hud
// 参数:
// caption:标题
// bActive:是否显示转圈动画
// time:自动消失时间,如果为0,则不自动消失
- (void)showHudOnWindow:(NSString *)caption
image:(UIImage *)image
acitivity:(BOOL)bAcitve
autoHideTime:(NSTimeInterval)time;
// 在当前的view上显示hud
// 参数:
// view:要添加hud的view
// caption:标题
// image:图片
// bActive:是否显示转圈动画
// time:自动消失时间,如果为0,则不自动消失
- (void)showHudOnView:(UIView *)view
caption:(NSString *)caption
image:(UIImage *)image
acitivity:(BOOL)bAcitve
autoHideTime:(NSTimeInterval)time;
//有loading 状态
- (void)showWithStatus:(NSString *) status onView:(UIView *)view;
- (void)dismissOnView:(UIView *)view;
//有感叹号提示
- (void)showInfoWithStatus:(NSString*)status onView:(UIView *)view;
- (void)showWithStatusOnWindow:(NSString *)caption;
- (void)showInfoWithStatus:(NSString*)status onView:(UIView *)view autoHideTime:(NSTimeInterval)time;
- (void)showInfoOnWindowWithStatus:(NSString*)status;
- (void)showHudOnView:(UIView *)view
caption:(NSString *)caption
image:(UIImage *)image
acitivity:(BOOL)bAcitve
autoHideTime:(NSTimeInterval)time
animated:(BOOL)animated;
- (void)showAutoTimeHudOnWindow:(NSString *)caption;
- (void)showAutoTimeHudOnView:(UIView *)view
caption:(NSString *)caption;
- (void)showHudOnView:(UIView *)view
caption:(NSString *)caption
autoHideTime:(NSTimeInterval)time;
- (void)showHudOnWindow:(NSString *)caption autoHideTime:(NSTimeInterval)autoHideTimeInterval;
- (void)showHudOnView:(UIView *)view
caption:(NSString *)caption
image:(UIImage *)image
acitivity:(BOOL)bAcitve
autoHideTime:(NSTimeInterval)time
doBlock:(AfterHudDisappearBlock)block;
// 隐藏hud
- (void)hideHudInView:(UIView *)parentView;
- (void)hideHudInView:(UIView *)parentView after:(NSTimeInterval)time;
- (void)hideHudInWindow;
/** oading状态*/
- (void)showThemeLoadingOnView:(UIView *)view;
/** 屏蔽用户交互行为,带对勾的文案*/
- (void)showSuccessWithStatus:(NSString *) statue onView:(UIView *)view;
/** 不屏蔽用户交互行为,带对勾的文案*/
- (void)showInteractiveSuccessWithStatus:(NSString *) status onView:(UIView *)view;
/** 屏蔽用户交互行为,带叉子的文案*/
- (void)showErrorWithStatus:(NSString*)status onView:(UIView *)view;
/** 不屏蔽用户交互行为,带叉子的文案*/
- (void)showInteractiveErrorWithStatus:(NSString*)status onView:(UIView *)view;
/** 展示文字和自定义图片,如果无法获取当前view,传nil也可获得当前最上层的view*/
- (void)showImageTextWithStatus:(NSString *)statusStr image:(UIImage *)image onView:(UIView *)view;
/** 展示文字,如果无法获取当前view,传nil也可获得当前最上层的view*/
- (void)showTextWithStatus:(NSString *)statusStr onView:(UIView *)view;
@end
#import "AXProgressHUDHelper.h"
#import "MBProgressHUD.h"
@interface AXProgressHUDHelper ()
@property (nonatomic,strong) NSString *showingCaption;
@property (nonatomic,strong) MBProgressHUD *hud;
@property (nonatomic,strong) UIView *parentView;
@end
@implementation AXProgressHUDHelper
static AXProgressHUDHelper* hudInstance = nil;
- (id)init
{
self = [super init];
if (self) {
self.autoHideTime = 1.5;
}
return self;
}
#pragma mark - public method
+ (AXProgressHUDHelper*) getInstance{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
hudInstance = [[AXProgressHUDHelper alloc] init];
});
return hudInstance;
}
// 在window上显示hud
- (void)showHudOnWindow:(NSString *)caption
image:(UIImage *)image
acitivity:(BOOL)bAcitve
autoHideTime:(NSTimeInterval)time
{
UIView *v = [[UIApplication sharedApplication].delegate window];
[self showHudOnView:v
caption:caption
image:image
acitivity:bAcitve
autoHideTime:time];
}
// 在当前的view上显示hud
- (void)showHudOnView:(UIView *)view
caption:(NSString *)caption
image:(UIImage *)image
acitivity:(BOOL)bAcitve
autoHideTime:(NSTimeInterval)time
{
if (!view) {
view = [self getTopController].view;
}
[self showHudOnView:view
caption:caption
image:image
acitivity:bAcitve
autoHideTime:time
animated:YES];
}
// 在当前的view上显示hud,带动画选项
- (void)showHudOnView:(UIView *)view
caption:(NSString *)caption
image:(UIImage *)image
acitivity:(BOOL)bAcitve
autoHideTime:(NSTimeInterval)time
animated:(BOOL)animated
{
// 删除此view上原有的hud
NSArray *array;
if (view) {
array = [MBProgressHUD allHUDsForView:view];
} else {
view = [self getTopController].view;
array = [MBProgressHUD allHUDsForView:[self getTopController].view];
}
for (MBProgressHUD *obj in array) {
[obj hide:NO];
}
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:animated];
hud.detailsLabelText = caption;
if (!bAcitve) {
hud.mode = MBProgressHUDModeText;
} else {
hud.mode = MBProgressHUDModeIndeterminate;
}
if (image != nil) {
hud.mode = MBProgressHUDModeCustomView;
hud.customView = [[UIImageView alloc] initWithImage:image];
}
if (time > 0.0f) {
[hud hide:YES afterDelay:time];
}
[hud layoutSubviews];
}
- (void)showHudOnView:(UIView *)view
caption:(NSString *)caption
image:(UIImage *)image
acitivity:(BOOL)bAcitve
autoHideTime:(NSTimeInterval)time
doBlock:(AfterHudDisappearBlock)block
{
// 删除此view上原有的hud
NSArray *array;
if (view) {
array = [MBProgressHUD allHUDsForView:view];
} else {
view = [self getTopController].view;
array = [MBProgressHUD allHUDsForView:[self getTopController].view];
}
for (MBProgressHUD *obj in array) {
[obj hide:NO];
}
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
hud.detailsLabelText = caption;
hud.completionBlock = block;
if (!bAcitve) {
hud.mode = MBProgressHUDModeText;
} else {
hud.mode = MBProgressHUDModeIndeterminate;
}
if (image != nil) {
hud.mode = MBProgressHUDModeCustomView;
hud.customView = [[UIImageView alloc] initWithImage:image];
}
if (time > 0.0f) {
[hud hide:YES afterDelay:time];
}
[hud layoutSubviews];
}
- (void)showAutoTimeHudOnWindow:(NSString *)caption {
[self showHudOnWindow:caption autoHideTime:self.autoHideTime];
}
- (void)showAutoTimeHudOnView:(UIView *)view
caption:(NSString *)caption
{
if (!view) {
view = [self getTopController].view;
}
[self showHudOnView:view caption:caption image:nil acitivity:NO autoHideTime:self.autoHideTime];
}
- (void)showHudOnView:(UIView *)view
caption:(NSString *)caption
autoHideTime:(NSTimeInterval)autoHideTimeInterval
{
if (!view) {
view = [self getTopController].view;
}
[self showHudOnView:view caption:caption image:nil acitivity:NO autoHideTime:autoHideTimeInterval];
}
- (void)showHudOnWindow:(NSString *)caption autoHideTime:(NSTimeInterval)autoHideTimeInterval
{
UIView *v = [[UIApplication sharedApplication].delegate window];
[self showHudOnView:v caption:caption image:nil acitivity:NO autoHideTime:autoHideTimeInterval];
}
// 隐藏hud
- (void)hideHudInView:(UIView *)parentView
{
if (!parentView) {
parentView = [self getTopController].view;
}
[MBProgressHUD hideAllHUDsForView:parentView animated:YES];
}
- (void)hideHudInView:(UIView *)parentView after:(NSTimeInterval)time
{
NSArray *array;
if (parentView) {
} else {
parentView = [self getTopController].view;
}
array = [MBProgressHUD allHUDsForView:parentView];
for (MBProgressHUD *hud in array) {
hud.removeFromSuperViewOnHide = YES;
[hud hide:YES afterDelay:time];
}
}
- (void)hideHudInWindow
{
UIView *v = [UIApplication sharedApplication].keyWindow;
[self hideHudInView:v];
}
#pragma mark 封装
- (void)showWithStatus:(NSString *) status onView:(UIView *)view
{
if (view) {
[self showHudOnView:view caption: status image:nil acitivity: true autoHideTime: 0];
} else {
[self showHudOnView:[self getTopController].view caption: status image:nil acitivity: true autoHideTime: 0];
}
}
- (void)showThemeLoadingOnView:(UIView *)view {
if (view) {
[self showHudOnView:view caption:@"正在加载" image:nil acitivity: true autoHideTime: 0];
} else {
[self showHudOnView:[self getTopController].view caption:@"正在加载" image:nil acitivity:true autoHideTime: 0];
}
}
- (void)showWithStatusOnWindow:(NSString *)status
{
UIWindow *window = [UIApplication sharedApplication].keyWindow;
[self showHudOnView: window caption: status image:nil acitivity: true autoHideTime: 0];
}
- (void)dismissOnView:(UIView *)view
{
if (!view) {
view = [self getTopController].view;
}
[self hideHudInView:view];
}
- (void)showWithStatus:(NSString *)status image:(UIImage *)image interactive:(BOOL)interactive onView:(UIView *)view{
// 删除此view上原有的hud
NSArray *array;
if (view) {
array = [MBProgressHUD allHUDsForView:view];
} else {
view = [self getTopController].view;
array = [MBProgressHUD allHUDsForView:[self getTopController].view];
}
for (MBProgressHUD *obj in array) {
[obj hide:NO];
}
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
hud.userInteractionEnabled = !interactive;
hud.detailsLabelText = status;
hud.mode = MBProgressHUDModeText;
hud.mode = MBProgressHUDModeCustomView;
[image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
UIImageView *imageView1 = [[UIImageView alloc] initWithImage:image];
imageView1.tintColor = [UIColor whiteColor];
hud.customView = imageView1;
NSTimeInterval time = [self displayDurationForString:status];
if ([self displayDurationForString:status] < 2.0) {
time = 2.0f;
}
[hud hide:YES afterDelay:time];
[hud layoutSubviews];
}
//不可交互成功框
- (void)showSuccessWithStatus:(NSString *) status onView:(UIView *)view {
UIImage *image = [UIImage imageNamed:@"success.png"];
[self showWithStatus:status image:image interactive:NO onView:view];
}
//不可交互失败框
- (void)showErrorWithStatus:(NSString*)status onView:(UIView *)view {
UIImage *image = [UIImage imageNamed:@"error.png"];
[self showWithStatus:status image:image interactive:NO onView:view];
}
//可交互成功框
- (void)showInteractiveSuccessWithStatus:(NSString *)status onView:(UIView *)view {
UIImage *image = [UIImage imageNamed:@"success.png"];
[self showWithStatus:status image:image interactive:YES onView:view];
}
//可交互失败框
- (void)showInteractiveErrorWithStatus:(NSString*)status onView:(UIView *)view {
UIImage *image = [UIImage imageNamed:@"error.png"];
[self showWithStatus:status image:image interactive:YES onView:view];
}
//展示文字和图片
- (void)showImageTextWithStatus:(NSString *)statusStr image:(UIImage *)image onView:(UIView *)view {
[self showWithStatus:statusStr image:image interactive:YES onView:view];
}
//直接展示文字
- (void)showTextWithStatus:(NSString *)statusStr onView:(UIView *)view {
[self showWithStatus:statusStr image:nil interactive:YES onView:view];
}
- (void)showInfoWithStatus:(NSString*)status onView:(UIView *)view{
[self showInfoWithStatus:status onView:view autoHideTime:[self displayDurationForString:status]];
}
- (void)showInfoWithStatus:(NSString*)status onView:(UIView *)view autoHideTime:(NSTimeInterval)time{
// 删除此view上原有的hud
NSArray *array;
if (view) {
array = [MBProgressHUD allHUDsForView:view];
} else {
view = [self getTopController].view;
array = [MBProgressHUD allHUDsForView:[self getTopController].view];
}
for (MBProgressHUD *obj in array) {
[obj hide:NO];
}
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
hud.detailsLabelText = status;
hud.mode = MBProgressHUDModeText;
hud.mode = MBProgressHUDModeCustomView;
UIImage *successImage = [UIImage imageNamed:@"info.png"];
successImage = [successImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
hud.customView = [[UIImageView alloc] initWithImage:successImage];
[hud.customView setTintColor:[UIColor whiteColor]];
[hud hide:YES afterDelay:time];
[hud layoutSubviews];
}
- (void)showInfoOnWindowWithStatus:(NSString*)status{
UIWindow *window = [UIApplication sharedApplication].keyWindow;
[self showHudOnView: window caption: status image:nil acitivity: NO autoHideTime: 4.5];
}
- (NSTimeInterval)displayDurationForString:(NSString*)string{
return MIN((float)string.length * 0.06 + 0.5, 5.0);
}
- (UIViewController *)getTopController {
UIViewController *blockViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (blockViewController.presentedViewController) {
blockViewController = blockViewController.presentedViewController;
}
if ([blockViewController respondsToSelector:@selector(selectedViewController)]) {
blockViewController = [blockViewController performSelector:@selector(selectedViewController)];
}
if ([blockViewController isKindOfClass:[UINavigationController class]]) {
blockViewController = [(UINavigationController *)blockViewController topViewController];
}
return blockViewController;
}
@end
- iOS 之MBProgressHUD的简单封装
- MBProgressHUD的简单封装
- MBProgressHUD的简单封装
- iOS MBProgressHUD的简单使用
- MBProgressHUD提示框简单实用的封装
- IOS MBProgressHUD第三方库的封装
- 基于MBProgressHUD的封装
- IOS-39-对第三方MBProgressHUD库的再封装
- iOS开发之MBProgressHUD的使用
- iOS开发之MBProgressHUD
- iOS之网络请求的简单封装
- MBProgressHUD的简单应
- MBProgressHUD的简单使用
- iOS MBProgressHUD的使用
- MBProgressHUD的使用-ios
- iOS MBProgressHUD 的使用方法
- iOS 第三方框架-MBProgressHUD的简单使用
- iOS 第三方类库之MBProgressHUD
- BU_DATE_CHAR abap screen 日期字段搜索帮助
- BZOJ 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 曼哈顿距离转切比雪夫距离 Treap
- pyspark-MLlib(Classification and Regression)
- 数据分析团队正成为手游公司的标配,但我为什么说解散他
- 山东大学操作系统课后作业1-7章
- iOS 之MBProgressHUD的简单封装
- Waymo无人车报告:通往自动驾驶之路
- C# DataTable分页(不需要sql)
- 【DP+树状数组 or 贪心】Codeforces527D[Clique Problem]题解
- iOS 使用UIScrollView实现图片的缩放
- 11.Java数组
- 【theano-windows】学习笔记十二——卷积神经网络
- C#生成单号
- 操作系统与应用程序的关系 操作系统主要可以分为两大部分:内核和内核之外的一些程序。内核就是直接控制最底层的硬件,而我们日常所用到的软件,大都是通过内核之外一些程序与内核之间的接口完成的,例如WINDO