XBLoadingView.m 4.43 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
//
//  XBLoadingView.m
//  Lighting
//
//  Created by 曹云霄 on 2016/12/1.
//  Copyright © 2016年 上海勾芒科技有限公司. All rights reserved.
//

#import "XBLoadingView.h"

@implementation XBLoadingView

13 14 15 16 17
/**
 显示普通加载框
 */
+ (void)showHUDViewWithDefault
{
18 19 20 21 22 23 24
    UIActivityIndicatorView *loadingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    loadingView.frame = [UIScreen mainScreen].bounds;
    [[self hudShowWindow] addSubview:loadingView];
    loadingView.center = [[self hudShowWindow] center];
    loadingView.color = kMainBlueColor;
    loadingView.hidesWhenStopped = YES;
    [loadingView startAnimating];
25 26 27 28 29 30 31
}

/**
 显示普通加载框
 */
+ (void)showHUDViewWithDefaultWithView:(UIView *)view
{
32 33 34 35 36 37 38
    UIActivityIndicatorView *loadingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    loadingView.frame = view.bounds;
    [view addSubview:loadingView];
    loadingView.center = [view center];
    loadingView.color = kMainBlueColor;
    loadingView.hidesWhenStopped = YES;
    [loadingView startAnimating];
39 40 41 42 43 44 45 46 47
}

/**
 显示文本提示框
 
 @param text 提示信息
 */
+ (void)showHUDViewWithText:(NSString *)text
{
48
    [[self class] hideHUDViewWithDefault];
49
    XBLoadingView *hud = [XBLoadingView showHUDAddedTo:[self hudShowWindow] animated:YES];
50 51
    hud.labelText = text;
    hud.margin = 20.f;
52
    hud.color = [[UIColor blackColor] colorWithAlphaComponent:0.4];
53 54 55 56 57 58 59 60 61 62 63
    hud.animationType = MBProgressHUDAnimationZoom;
    hud.mode = MBProgressHUDModeText;
    hud.removeFromSuperViewOnHide = YES;
    [hud hide:YES afterDelay:3.0f];
}

/**
 显示成功提示框
 
 @param text 提示信息
 */
64
+ (void)showHUDViewWithSuccessText:(NSString *)text completeBlock:(void (^)())finish
65
{
66
    [[self class] hideHUDViewWithDefault];
67
    XBLoadingView *hud = [XBLoadingView showHUDAddedTo:[self hudShowWindow] animated:YES];
68
    hud.mode = MBProgressHUDModeCustomView;
69
    UIImage *image = [[UIImage imageNamed:@"success-1"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
70 71 72 73 74
    hud.customView = [[UIImageView alloc] initWithImage:image];
    hud.color = [[UIColor blackColor] colorWithAlphaComponent:0.6];
    hud.labelFont = [UIFont systemFontOfSize:15];
    hud.labelText = text;
    [hud hide:YES afterDelay:3];
75
    if (finish) {
76 77 78
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            finish();
        });
79
    }
80 81 82 83 84 85 86 87 88
}

/**
 显示进度框
 
 @param text 提示信息
 
 @return XBLoadingView
 */
曹云霄's avatar
曹云霄 committed
89
+ (XBLoadingView *)showHUDViewProgressLabel:(NSString *)text
90
{
91
    XBLoadingView *hud = [XBLoadingView showHUDAddedTo:[self hudShowWindow] animated:YES];
92
    hud.mode = MBProgressHUDModeDeterminateHorizontalBar;
93 94 95 96 97 98 99 100 101 102 103
    hud.labelText = text;
    hud.labelFont = [UIFont systemFontOfSize:12];
    hud.removeFromSuperViewOnHide = YES;
    return hud;
}

/**
 隐藏加载框
 */
+ (void)hideHUDViewWithDefault
{
104 105
//    [[self class] hideAllHUDsForView:[self hudShowWindow] animated:YES];
    [[self class] hideActivityIndicatorForView:[self hudShowWindow] animated:YES];
106 107 108 109 110 111 112 113
}


/**
 隐藏加载框
 */
+ (void)hideHUDViewWithDefaultWithView:(UIView *)view
{
114 115
//    [[self class] hideAllHUDsForView:view animated:YES];
    [[self class] hideActivityIndicatorForView:view animated:YES];
116 117
}

118

119 120 121 122 123 124
#pragma mark - 隐藏UIActivityIndicatorView
+ (void)hideActivityIndicatorForView:(UIView *)view animated:(BOOL)animated
{
    NSArray *indicators = [XBLoadingView allIndicatorViewForView:view];
    for (UIActivityIndicatorView *indicator in indicators) {
        [indicator stopAnimating];
125
        [indicator removeFromSuperview];
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
    }
}


#pragma mark - 获取所有的 UIActivityIndicatorView
+ (NSArray *)allIndicatorViewForView:(UIView *)view {
    NSMutableArray *indicatos = [NSMutableArray array];
    NSArray *subviews = view.subviews;
    for (UIView *aView in subviews) {
        if ([aView isKindOfClass:[UIActivityIndicatorView class]]) {
            [indicatos addObject:aView];
        }
    }
    return [NSArray arrayWithArray:indicatos];
}

#pragma mark - 获取主窗口
143 144 145 146 147 148 149 150 151 152 153 154
+ (UIWindow *)hudShowWindow
{
    UIWindow *showWindow = nil;
    NSArray *windows = [[UIApplication sharedApplication] windows];
    if ([windows count] >= 2) {
        showWindow = [windows objectAtIndex:1];
    }
    else {
        showWindow = [[UIApplication sharedApplication] keyWindow];
    }
    return showWindow;
}
155
@end