XBLoadingView.m 5.07 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 25 26 27 28 29 30 31
    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];
    
//    XBLoadingView *hud = [XBLoadingView showHUDAddedTo:[self hudShowWindow] animated:YES];
//    hud.animationType = MBProgressHUDAnimationZoom;
//    hud.mode = MBProgressHUDModeIndeterminate;
//    hud.color = [UIColor clearColor];
//    hud.activityIndicatorColor = kMainBlueColor;
//    hud.removeFromSuperViewOnHide = YES;
32 33 34 35 36 37 38
}

/**
 显示普通加载框
 */
+ (void)showHUDViewWithDefaultWithView:(UIView *)view
{
39 40 41 42 43 44 45 46 47 48 49 50 51 52
//    XBLoadingView *hud = [XBLoadingView showHUDAddedTo:view animated:YES];
//    hud.animationType = MBProgressHUDAnimationZoom;
//    hud.mode = MBProgressHUDModeIndeterminate;
//    hud.color = [UIColor clearColor];
//    hud.activityIndicatorColor = kMainBlueColor;
//    hud.removeFromSuperViewOnHide = YES;
    
    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];
53 54 55 56 57 58 59 60 61
}

/**
 显示文本提示框
 
 @param text 提示信息
 */
+ (void)showHUDViewWithText:(NSString *)text
{
62
    [[self class] hideHUDViewWithDefault];
63
    XBLoadingView *hud = [XBLoadingView showHUDAddedTo:[self hudShowWindow] animated:YES];
64 65
    hud.labelText = text;
    hud.margin = 20.f;
66
    hud.color = [[UIColor blackColor] colorWithAlphaComponent:0.4];
67 68 69 70 71 72 73 74 75 76 77
    hud.animationType = MBProgressHUDAnimationZoom;
    hud.mode = MBProgressHUDModeText;
    hud.removeFromSuperViewOnHide = YES;
    [hud hide:YES afterDelay:3.0f];
}

/**
 显示成功提示框
 
 @param text 提示信息
 */
78
+ (void)showHUDViewWithSuccessText:(NSString *)text completeBlock:(void (^)())finish
79
{
80
    [[self class] hideHUDViewWithDefault];
81
    XBLoadingView *hud = [XBLoadingView showHUDAddedTo:[self hudShowWindow] animated:YES];
82
    hud.mode = MBProgressHUDModeCustomView;
83
    UIImage *image = [[UIImage imageNamed:@"success-1"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
84 85 86 87 88
    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];
89
    if (finish) {
90 91 92
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            finish();
        });
93
    }
94 95 96 97 98 99 100 101 102
}

/**
 显示进度框
 
 @param text 提示信息
 
 @return XBLoadingView
 */
曹云霄's avatar
曹云霄 committed
103
+ (XBLoadingView *)showHUDViewProgressLabel:(NSString *)text
104
{
105
    XBLoadingView *hud = [XBLoadingView showHUDAddedTo:[self hudShowWindow] animated:YES];
106
    hud.mode = MBProgressHUDModeDeterminateHorizontalBar;
107 108 109 110 111 112 113 114 115 116 117
    hud.labelText = text;
    hud.labelFont = [UIFont systemFontOfSize:12];
    hud.removeFromSuperViewOnHide = YES;
    return hud;
}

/**
 隐藏加载框
 */
+ (void)hideHUDViewWithDefault
{
118 119
//    [[self class] hideAllHUDsForView:[self hudShowWindow] animated:YES];
    [[self class] hideActivityIndicatorForView:[self hudShowWindow] animated:YES];
120 121 122 123 124 125 126 127
}


/**
 隐藏加载框
 */
+ (void)hideHUDViewWithDefaultWithView:(UIView *)view
{
128 129
//    [[self class] hideAllHUDsForView:view animated:YES];
    [[self class] hideActivityIndicatorForView:view animated:YES];
130 131
}

132

133 134 135 136 137 138
#pragma mark - 隐藏UIActivityIndicatorView
+ (void)hideActivityIndicatorForView:(UIView *)view animated:(BOOL)animated
{
    NSArray *indicators = [XBLoadingView allIndicatorViewForView:view];
    for (UIActivityIndicatorView *indicator in indicators) {
        [indicator stopAnimating];
139
        [indicator removeFromSuperview];
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
    }
}


#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 - 获取主窗口
157 158 159 160 161 162 163 164 165 166 167 168
+ (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;
}
169
@end