| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- //
- // XFHudHelper.m
- //
- // Created by TopDev on 10/23/14.
- // Copyright (c) 2014 TopDev. All rights reserved.
- //
- #import "XCHudHelper.h"
- @implementation XCHudHelper
- + (XCHudHelper *)sharedInstance {
- static XCHudHelper *_instance = nil;
-
- @synchronized (self) {
- if (_instance == nil) {
- _instance = [[self alloc] init];
- }
- }
-
- return _instance;
- }
- - (void)showHudAcitivityOnWindow
- {
- [self showHudOnWindow:nil image:nil acitivity:YES autoHideTime:0];
- }
- - (void)showHudOnWindow:(NSString *)caption
- image:(UIImage *)image
- acitivity:(BOOL)active
- autoHideTime:(NSTimeInterval)time1 {
-
- if (_hud) {
- [_hud hideAnimated:NO];
- }
-
-
- self.hud = [[MBProgressHUD alloc] initWithView:[[UIApplication sharedApplication] delegate].window];
- [[[UIApplication sharedApplication] delegate].window addSubview:self.hud];
- self.hud.label.text = caption;
- self.hud.customView = [[UIImageView alloc] initWithImage:image];
- self.hud.customView.bounds = CGRectMake(0, 0, 100, 100);
- self.hud.mode = image ? MBProgressHUDModeCustomView : MBProgressHUDModeIndeterminate;
- self.hud.animationType = MBProgressHUDAnimationFade;
- self.hud.removeFromSuperViewOnHide = YES;
- [self.hud showAnimated:YES];
- if (time1 > 0) {
- [self.hud hideAnimated:YES afterDelay:time1];
- }
- }
- - (void)showHudOnView:(UIView *)view
- caption:(NSString *)caption
- image:(UIImage *)image
- acitivity:(BOOL)active
- autoHideTime:(NSTimeInterval)time1 {
-
- if (_hud) {
- [_hud hideAnimated:NO];
- }
-
- self.hud = [[MBProgressHUD alloc] initWithView:view];
- [view addSubview:self.hud];
- self.hud.label.text = caption;
- self.hud.customView = [[UIImageView alloc] initWithImage:image];
- self.hud.customView.bounds = CGRectMake(0, 0, 100, 100);
- self.hud.mode = image ? MBProgressHUDModeCustomView : MBProgressHUDModeIndeterminate;
- self.hud.animationType = MBProgressHUDAnimationFade;
- [self.hud showAnimated:YES];
- if (time1 > 0) {
- [self.hud hideAnimated:YES afterDelay:time1];
- }
- }
- - (void)setCaption:(NSString *)caption {
- self.hud.label.text = caption;
- }
- - (void)hideHud {
- if (_hud) {
- [_hud hideAnimated:YES];
- }
- }
- - (void)hideHudAfter:(NSTimeInterval)time1 {
- if (_hud) {
- [_hud hideAnimated:YES afterDelay:time1];
- }
- }
- + (void)showSuccess:(NSString *)success
- {
- [self showSuccess:success toView:nil];
- }
- + (void)showError:(NSString *)error
- {
- [self showError:error toView:nil];
- }
- + (void)showError:(NSString *)error toView:(UIView *)view{
- [self show:error icon:@"error.png" view:view];
- }
- + (void)showSuccess:(NSString *)success toView:(UIView *)view
- {
- [self show:success icon:@"success.png" view:view];
- }
- #pragma mark 显示信息
- + (void)show:(NSString *)text icon:(NSString *)icon view:(UIView *)view
- {
- if (view == nil) view = [[UIApplication sharedApplication].windows lastObject];
- // 快速显示一个提示信息
- MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
- hud.label.text = text;
- // 设置图片
- hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"MBProgressHUD.bundle/%@", icon]]];
- // 再设置模式
- hud.mode = MBProgressHUDModeCustomView;
-
- // 隐藏时候从父控件中移除
- hud.removeFromSuperViewOnHide = YES;
-
- // 1.0秒之后再消失
- [hud hideAnimated:YES afterDelay:1.0];
- }
- + (void)showMessage:(NSString *)message
- {
- [self show:message icon:@"" view:nil];
- }
- + (void)showMessage:(NSString *)message toView:(UIView *)view {
- if (view == nil) view = [[UIApplication sharedApplication].windows lastObject];
- // 快速显示一个提示信息
- MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
- hud.label.text = message;
- // 隐藏时候从父控件中移除
- hud.removeFromSuperViewOnHide = YES;
- // YES代表需要蒙版效果
- //hud.dimBackground = YES;
- // 100秒之后再消失
- [hud hideAnimated:YES afterDelay:100.0];
- }
- + (void)hideHUD
- {
- UIView *view = [[UIApplication sharedApplication].windows lastObject];
- [MBProgressHUD hideHUDForView:view animated:YES];
- }
- @end
|