+ (MBProgressHUD *)sharedMBProgressHUD {
static MBProgressHUD *_sharedHUD = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedHUD = [[MBProgressHUD alloc] initWithView:[UIApplication sharedApplication].keyWindow];
});
return _sharedHUD;
}
//
// UIViewController+ZYHUD.h
// test_HUD_01
//
// Created by admin on 4/13/16.
// Copyright © 2016 jeffasd. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "MBProgressHUD.h"
@interface UIViewController (ZYHUD)
@property (nonatomic, strong) MBProgressHUD *hudAddNew;
- (void)zyShowHudInView:(UIView *)view hint:(NSString *)hint;
- (void)zyHideHud;
- (void)zyHideHud:(void(^)())result;
- (void)zyShowHint:(NSString *)hint;
- (void)zyShowHint:(NSString *)hint DismissAfter:(NSTimeInterval)time;
//// 从默认(showHint:)显示的位置再往上(下)yOffset
//- (void)showHint:(NSString *)hint yOffset:(float)yOffset;
@end
//
// UIViewController+ZYHUD.m
// test_HUD_01
//
// Created by admin on 4/13/16.
// Copyright © 2016 jeffasd. All rights reserved.
//
#import "UIViewController+ZYHUD.h"
#import <objc/runtime.h>
#import <RACEXTScope.h>
#define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )
@implementation UIViewController (ZYHUD)
- (MBProgressHUD *)hudAddNew{
return objc_getAssociatedObject(self, @selector(hudAddNew));
}
- (void)setHudAddNew:(MBProgressHUD *)hudAddNew{
objc_setAssociatedObject(self, @selector(hudAddNew), hudAddNew, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (void)zyShowHudInView:(UIView *)view hint:(NSString *)hint{
self.hudAddNew = [[MBProgressHUD alloc] initWithView:view];
self.hudAddNew.labelText = hint;
[view addSubview:self.hudAddNew];
[self.hudAddNew show:YES];
}
- (void)zyShowHint:(NSString *)hint
{
// UIView *view = [[UIApplication sharedApplication].delegate window];
// self.hudAddNew = [MBProgressHUD sharedMBProgressHUD];
// [view addSubview:self.hudAddNew];
// BOOL animated = YES;
// [self.hudAddNew show:YES];
// self.hudAddNew.userInteractionEnabled = NO;
// self.hudAddNew.labelText = hint;
// self.hudAddNew.margin = 10.f;
// self.hudAddNew.removeFromSuperViewOnHide = YES;
@weakify(self);
dispatch_async(dispatch_get_main_queue(), ^{
@strongify(self);
UIView *view = [[UIApplication sharedApplication].delegate window];
self.hudAddNew = [MBProgressHUD sharedMBProgressHUD];
[view addSubview:self.hudAddNew];
BOOL animated = YES;
[self.hudAddNew show:YES];
self.hudAddNew.userInteractionEnabled = NO;
self.hudAddNew.labelText = hint;
self.hudAddNew.margin = 10.f;
self.hudAddNew.removeFromSuperViewOnHide = YES;
});
}
- (void)zyShowHint:(NSString *)hint DismissAfter:(NSTimeInterval)time
{
@weakify(self);
dispatch_async(dispatch_get_main_queue(), ^{
@strongify(self);
UIView *view = [[UIApplication sharedApplication].delegate window];
self.hudAddNew = [MBProgressHUD sharedMBProgressHUD];
[view addSubview:self.hudAddNew];
BOOL animated = YES;
[self.hudAddNew show:YES];
self.hudAddNew.userInteractionEnabled = NO;
self.hudAddNew.labelText = hint;
self.hudAddNew.margin = 10.f;
self.hudAddNew.removeFromSuperViewOnHide = YES;
[self.hudAddNew hide:YES afterDelay:time];
});
}
- (void)zyHideHud{
NSLog(@"self class is %@", [self class]);
NSLog(@"self is %@", self);
// @weakify(self);
// dispatch_async(dispatch_get_main_queue(), ^{
// @strongify(self);
//// [self.hudAddNew removeFromSuperview];
// [self.hudAddNew hide:YES];
//
// });
[self performSelectorOnMainThread:@selector(hidezyHud) withObject:nil waitUntilDone:YES];
}
- (void)hidezyHud{
[self.hudAddNew removeFromSuperview];
}
- (void)zyHideHud:(void(^)())result{
@weakify(self);
dispatch_async(dispatch_get_main_queue(), ^{
@strongify(self);
[self.hudAddNew removeFromSuperview];
result();
});
}
@end