JLChen
2021-10-08 f8457b624a75bf8e41489b74f66009eee6891b8b
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
//
//  Copyright © 2016 dahua. All rights reserved.
//
 
#import "LCProgressHUD.h"
#import "DHActivityIndicatorView.h"
#import "UIColor+LeChange.h"
 
#define COLOR_BG [[UIColor blackColor] colorWithAlphaComponent:0.65]
 
#define TAG_SUBHUD_VIEW 1903
 
//暂时先不处理1.0版本废弃的警告
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
 
static NSTimeInterval gDelayTime = 1.5; /**< Hud自动隐藏的时间(扩展后可通过接口设置) */
 
@implementation LCProgressHUD
 
+ (void)hideAllHuds:(UIView *)view animated:(BOOL)animated
{
    dispatch_async(dispatch_get_main_queue(), ^{
        UIView *mainView = view;
        mainView.userInteractionEnabled = YES;
        if (mainView == nil) {
            mainView = [self keyWindow];
        }
        
        UIView *subView = [mainView viewWithTag:TAG_SUBHUD_VIEW];
        
        if (subView) {
            [MBProgressHUD hideAllHUDsForView:mainView animated:animated];
            [MBProgressHUD hideAllHUDsForView:subView animated:animated];
            [UIView animateWithDuration:0.3 animations:^{
                subView.alpha = 0;
            } completion:^(BOOL finished) {
                [subView removeFromSuperview];
            }];
        }else {
            [MBProgressHUD hideAllHUDsForView:mainView animated:animated];
        }
    });
}
 
+ (void)hideAllHuds:(UIView *)view
{
    [self hideAllHuds:view animated:YES];
}
 
+ (void)showMsg:(NSString *)msg {
    [self showMsg:msg duration:1.0];
}
 
+ (void)showMsg:(NSString*)msg duration:(NSTimeInterval)duration {
    if (msg.length == 0) {
        return;
    }
    
    dispatch_async(dispatch_get_main_queue(), ^{
        //先隐藏重复的Hud,防止叠加显示
        UIView *superView = [self keyWindow];
        
        [MBProgressHUD hideAllHUDsForView:superView animated:YES];
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:superView animated:YES];
        
        hud.mode = MBProgressHUDModeText;
        hud.label.textColor = [UIColor dhcolor_c43];
        hud.label.numberOfLines = 0;
        hud.labelText = msg;
        hud.margin = 10.f;
        hud.yOffset = 0;
        hud.minShowTime = duration;
        hud.removeFromSuperViewOnHide = YES;
        
        hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
        hud.bezelView.backgroundColor = COLOR_BG;
        
        [hud hide:YES afterDelay:gDelayTime];
    });
}
 
+ (void)showMsg:(NSString *)msg inView:(UIView *)view
{
    if (msg.length == 0) {
        return;
    }
    if (view == nil) {
        view = [UIApplication sharedApplication].keyWindow;
    }
    
    dispatch_async(dispatch_get_main_queue(), ^{
        
        [MBProgressHUD hideAllHUDsForView:view animated:YES];
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
        
        hud.mode = MBProgressHUDModeText;
        hud.label.textColor = [UIColor dhcolor_c43];
        hud.label.numberOfLines = 0;
        hud.labelText = msg;
        hud.margin = 10.f;
        hud.yOffset = 0;
        hud.minShowTime = 1.0f;
        hud.removeFromSuperViewOnHide = YES;
        
        hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
        hud.bezelView.backgroundColor = COLOR_BG;
        
        [hud hide:YES afterDelay:gDelayTime];
    });
}
+ (MBProgressHUD *)showHudOnView:(UIView *)view tip:(NSString*)tip {
    return [self showHudOnView:view tip:tip animated:YES];
}
 
+ (MBProgressHUD *)showHudOnView:(UIView *)view animated:(BOOL)animated {
    return [self showHudOnView:view tip:nil animated:animated];
}
 
+ (MBProgressHUD *)showHudOnView:(UIView *)view animated:(BOOL)animated isInteract:(BOOL)isInteract {
    
    return [self showHudOnView:view tip:nil animated:animated isInteract:isInteract offHeight:0];
}
 
+ (MBProgressHUD *)showHudOnView:(UIView *)view {
    return [self showHudOnView:view tip:nil animated:YES];
}
 
+ (MBProgressHUD *)showHubOnView:(UIView *)view duration:(NSTimeInterval)duration {
    MBProgressHUD *hud = [self showHudOnView:view tip:nil animated:YES];
    [hud hide:YES afterDelay:duration];
    return hud;
}
 
+ (MBProgressHUD *)showHudOnView:(UIView *)view isInteract:(BOOL)isInteract {
    return [self showHudOnView:view tip:nil animated:YES isInteract:isInteract offHeight:0];
}
 
+ (MBProgressHUD *)showHudOnView:(UIView *)view bgColor:(UIColor*)bgColor {
    MBProgressHUD *hud = [self showHudOnView:view tip:nil animated:YES isInteract:false offHeight:0];
    //hud.color = bgColor; //deprecated
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.bezelView.color = bgColor;
    return hud;
}
 
+ (MBProgressHUD *)showHudOnView:(UIView *)view tip:(NSString*)tip animated:(BOOL)animated {
    CGFloat offHeight = 0;
    return [self showHudOnView:view tip:tip animated:animated isInteract:false offHeight:offHeight];
}
 
+ (MBProgressHUD *)showHudOnLowerView:(UIView *)view {
    return [self showHudOnLowerView:view tip:nil animated:YES];
}
 
+ (MBProgressHUD *)showHudOnLowerView:(UIView *)view tip:(NSString*)tip {
    return [self showHudOnLowerView:view tip:tip animated:YES];
}
 
+ (MBProgressHUD *)showHudOnLowerView:(UIView *)view tip:(NSString*)tip animated:(BOOL)animated
{
    CGFloat offHeight = -64;
    return [self showHudOnView:view tip:tip animated:animated isInteract:false offHeight:offHeight];
}
 
+ (MBProgressHUD *)showHudOnView:(UIView *)view tip:(NSString*)tip animated:(BOOL)animated isInteract:(BOOL)isInteract offHeight:(CGFloat)offHeight
{
    UIView *mainView = view;
    view.userInteractionEnabled = isInteract;
    
    CGFloat mOffHeight = offHeight;
    
    if (view.dh_y > 0) {
        mOffHeight = 0;
    }
    //y轴偏移量
    CGFloat subViewY = 0;
    
    if (mainView == nil) {
        mainView = [self keyWindow];
    }else if ([mainView isKindOfClass:[UIScrollView class]]) {
        
        //避免UIScrollView的子类滑动后显示产生偏移
        UIScrollView *scrollView = (UIScrollView *)mainView;
        if (scrollView.contentOffset.y != 0) {
            subViewY = scrollView.contentOffset.y;
        }
        //覆盖一层View,转圈加到覆盖层
        UIView *subView = [mainView viewWithTag:TAG_SUBHUD_VIEW];
        if (subView == nil) {
            subView = UIView.new;
            subView.frame = CGRectMake(0, subViewY, mainView.dh_width, mainView.dh_height + mOffHeight);
            subView.backgroundColor = [UIColor clearColor];
            subView.userInteractionEnabled = isInteract;
            subView.tag = TAG_SUBHUD_VIEW;
            [mainView addSubview:subView];
        }
        return [self addHudOnView:subView tip:tip animated:animated];
    }
    
    return [self addHudOnView:mainView tip:tip animated:animated];
}
 
+ (MBProgressHUD*)addHudOnView:(UIView*)view tip:(NSString*)tip animated:(BOOL)animated {
    //防止Hud重复加载
    [MBProgressHUD hideAllHUDsForView:view animated:animated];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:animated];
    DHActivityIndicatorView *customView = [[DHActivityIndicatorView alloc] init];
    [customView startAnimating];
    hud.customView = customView;
    hud.animationType = MBProgressHUDAnimationZoomOut;
    hud.mode = MBProgressHUDModeCustomView;
    hud.margin = 15;
    hud.removeFromSuperViewOnHide = YES;
    hud.label.textColor = [UIColor dhcolor_c43];
    hud.labelText = tip;
    hud.graceTime = 0.2;
    
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.bezelView.backgroundColor = COLOR_BG;
    
    return hud;
}
 
+ (void)showHudWithTip:(NSString *)tip image:(UIImage *)image onView:(UIView *)view
{
    UIView *superView = view ? : [self keyWindow];
    dispatch_async(dispatch_get_main_queue(), ^{
        
        [MBProgressHUD hideAllHUDsForView:superView animated:YES];
        MBProgressHUD* hud = [MBProgressHUD showHUDAddedTo:superView animated:YES];
        hud.mode = MBProgressHUDModeCustomView;
        hud.labelText = tip;
        hud.label.textColor = [UIColor dhcolor_c43];
        hud.contentColor = [UIColor dhcolor_c43];
        
        hud.customView = [[UIImageView alloc] initWithImage:image];
        hud.customView.frame = CGRectMake(0, 0, image.size.width/2, image.size.height/2);
        
        hud.margin = 20.f;
        hud.yOffset = 0;
        hud.minShowTime = 1.0;
        hud.removeFromSuperViewOnHide = YES;
        
        hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
        hud.bezelView.backgroundColor = COLOR_BG;
        
        [hud hide:YES afterDelay:gDelayTime];
    });
}
 
#pragma mark - Private
+ (UIView *)keyWindow
{
    int maxState = -1;
    UIWindow* keyWind = nil;
    for (UIWindow* wind in [UIApplication sharedApplication].windows )
    {
        if ([wind isMemberOfClass:NSClassFromString(@"UIRemoteKeyboardWindow")]) {
            continue;
        }
        
        if ([wind isMemberOfClass:NSClassFromString(@"UITextEffectsWindow")]) {
            continue;
        }
        
        if (wind.alpha == 0.0 ) {
            continue;
        }
        
        
        if (wind.hidden)
        {
            continue;
        }
        
        if (wind.windowLevel > maxState)
        {
            keyWind = wind;
            maxState = wind.windowLevel;
        }
    }
    
    if (keyWind != nil)
    {
        return keyWind;
    }
    
    return [UIApplication sharedApplication].keyWindow;
}
@end