YH_Tool.m
4.27 KB
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
//
// YH_Tool.m
// YH_Mall
//
// Created by XingYaXin on 14-10-10.
// Copyright (c) 2014年 YOHO. All rights reserved.
//
#import "YH_Tool.h"
#import <UIKit/UIKit.h>
NSTimeInterval const kAlertDelayTime = 1.0f;
@implementation YH_Tool
+ (void)alert:(NSString *)message type:(YHBAlertType)type autoHide:(BOOL)isAutoHide inView:(UIView *)view
{
[self alert:message type:type autoHide:isAutoHide inView:view durationTime:kAlertDelayTime];
}
+ (void)alert:(NSString *)message type:(YHBAlertType)type autoHide:(BOOL)isAutoHide inView:(UIView *)view durationTime:(NSTimeInterval)duration
{
[self alert:message type:type autoHide:isAutoHide inView:view durationTime:duration alertImage:nil];
}
+ (void)alert:(NSString *)message type:(YHBAlertType)type autoHide:(BOOL)isAutoHide inView:(UIView *)view durationTime:(NSTimeInterval)duration alertImage:(UIImage *)alertImage
{
NSString *msg = @"";
if (IsNilOrNull(message) || [message isEqualToString:@""]) {
if (type == YHBAlertTypeFail) {
return;
}
msg = NSLocalizedString(@"loading...", nil);
}else{
msg = message;
}
if (view == nil) {
view = [[UIApplication sharedApplication] keyWindow];
}
[self hideAlertInView:view];
MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:view];
hud.detailsLabelFont = [UIFont systemFontOfSize:12];
hud.detailsLabelText = msg;
hud.yOffset = -60.0f;
hud.removeFromSuperViewOnHide = YES;
NSString *alertImageName = @"";
MBProgressHUDMode mode = MBProgressHUDModeCustomView;
if (type == YHBAlertTypeFail) {
alertImageName = @"shared_alert_fail";
}
else if (type == YHBAlertTypeSuccess) {
alertImageName = @"shared_alert_success";
}
else if (type == YHBAlertTypeNetwork) {
alertImageName = @"shared_alert_network";
}
else if (type == YHBAlertTypeMessage) {
alertImageName = @"shared_alert_message";
}
else if (type == YHBAlertTypeWait) {
mode = MBProgressHUDModeIndeterminate;
}
if (type == YHBAlertTypeCustom) {
hud.customView = [[UIImageView alloc] initWithImage:alertImage];
} else if (mode == MBProgressHUDModeCustomView){
hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:alertImageName]];
}
hud.mode = mode;
[view addSubview:hud];
[hud show:YES];
if (isAutoHide) {
double delayInSeconds = (duration > 0 ? duration : kAlertDelayTime);
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[hud hide:YES];
});
}
}
+ (void)alert:(NSString *)message type:(YHBAlertType)type autoHide:(BOOL)isAutoHide
{
[self alert:message type:type autoHide:isAutoHide inView:[[UIApplication sharedApplication] keyWindow]];
}
+ (void)alert:(NSString *)message type:(YHBAlertType)type durationTime:(NSTimeInterval)time
{
[self alert:message type:type autoHide:YES inView:[[UIApplication sharedApplication] keyWindow] durationTime:time];
}
+ (void)alert:(NSString *)message type:(YHBAlertType)type
{
[self alert:message type:type autoHide:YES];
}
+ (void)alertMessage:(NSString *)message
{
[self alert:message type:YHBAlertTypeMessage];
}
+ (void)alertSuccess:(NSString *)message
{
[self alert:message type:YHBAlertTypeSuccess];
}
+ (void)alertFail:(NSString *)message
{
[self alert:message type:YHBAlertTypeFail];
}
+ (void)alertNetwork:(NSString *)message
{
[self alert:message type:YHBAlertTypeNetwork];
}
+ (void)alertNetworkLess
{
[self alertNetwork:NSLocalizedString(@"bad network try later", nil)];
}
+ (void)alertWait
{
[self alert:NSLocalizedString(@"loading...", nil) type:YHBAlertTypeWait autoHide:NO];
}
+ (void)hideAlert
{
[MBProgressHUD hideAllHUDsForView:[[UIApplication sharedApplication] keyWindow] animated:NO];
}
+ (void)hideAlertInView:(UIView *)view
{
[MBProgressHUD hideAllHUDsForView:view animated:NO];
}
+ (void)alertCustomView:(UIImage *)alertImage message:(NSString *)message durationTime:(NSTimeInterval)time
{
[self alert:message type:YHBAlertTypeCustom autoHide:YES inView:[[UIApplication sharedApplication] keyWindow] durationTime:time alertImage:alertImage];
}
@end