XBLoadingView.m 4.44 KB
Newer Older
曹云霄's avatar
曹云霄 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
//
//  XBLoadingView.m
//  Lighting
//
//  Created by 曹云霄 on 2016/12/1.
//  Copyright © 2016年 上海勾芒科技有限公司. All rights reserved.
//

#import "XBLoadingView.h"

@implementation XBLoadingView

/**
 显示普通加载框
 */
+ (void)showHUDViewWithDefault
{
    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];
}

/**
 显示普通加载框
 */
+ (void)showHUDViewWithDefaultWithView:(UIView *)view
{
    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];
}

/**
 显示文本提示框
 
 @param text 提示信息
 */
+ (void)showHUDViewWithText:(NSString *)text
{
    [[self class] hideHUDViewWithDefault];
    XBLoadingView *hud = [XBLoadingView showHUDAddedTo:[self hudShowWindow] animated:YES];
    hud.labelText = text;
    hud.margin = 20.f;
    hud.color = [[UIColor blackColor] colorWithAlphaComponent:0.4];
    hud.animationType = MBProgressHUDAnimationZoom;
    hud.mode = MBProgressHUDModeText;
    hud.removeFromSuperViewOnHide = YES;
    [hud hide:YES afterDelay:3.0f];
}

/**
 显示成功提示框
 
 @param text 提示信息
 */
+ (void)showHUDViewWithSuccessText:(NSString *)text completeBlock:(void (^)())finish
{
    [[self class] hideHUDViewWithDefault];
    XBLoadingView *hud = [XBLoadingView showHUDAddedTo:[self hudShowWindow] animated:YES];
    hud.mode = MBProgressHUDModeCustomView;
    UIImage *image = [[UIImage imageNamed:@"success-1"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    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];
    if (finish) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            finish();
        });
    }
}

/**
 显示进度框
 
 @param text 提示信息
 
 @return XBLoadingView
 */
+ (XBLoadingView *)showHUDViewProgressLabel:(NSString *)text
{
    XBLoadingView *hud = [XBLoadingView showHUDAddedTo:[self hudShowWindow] animated:YES];
    hud.mode = MBProgressHUDModeDeterminateHorizontalBar;
    hud.labelText = text;
    hud.labelFont = [UIFont systemFontOfSize:12];
    hud.removeFromSuperViewOnHide = YES;
    return hud;
}

/**
 隐藏加载框
 */
+ (void)hideHUDViewWithDefault
{
//    [[self class] hideAllHUDsForView:[self hudShowWindow] animated:YES];
    [[self class] hideActivityIndicatorForView:[self hudShowWindow] animated:YES];
}


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


#pragma mark - 隐藏UIActivityIndicatorView
+ (void)hideActivityIndicatorForView:(UIView *)view animated:(BOOL)animated
{
    NSArray *indicators = [XBLoadingView allIndicatorViewForView:view];
    for (UIActivityIndicatorView *indicator in indicators) {
        [indicator stopAnimating];
        [indicator removeFromSuperview];
    }
}


#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 - 获取主窗口
+ (UIWindow *)hudShowWindow
{
    UIWindow *showWindow = nil;
    NSArray *windows = [[UIApplication sharedApplication] windows];
    if ([windows count] >= 2) {
曹云霄's avatar
曹云霄 committed
148
        showWindow = [windows objectAtIndex_opple:1];
曹云霄's avatar
曹云霄 committed
149 150 151 152 153 154 155
    }
    else {
        showWindow = [[UIApplication sharedApplication] keyWindow];
    }
    return showWindow;
}
@end