-
Notifications
You must be signed in to change notification settings - Fork 0
/
MEActivityHUD.m
285 lines (218 loc) · 9.58 KB
/
MEActivityHUD.m
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
//
// MEActivityHUD.m
// eMAGine EDU
//
// Created by Manuel Escrig Ventura on 9/13/13.
//
//
#import "MEActivityHUD.h"
@implementation MEActivityHUD
#pragma mark - Class methods
static const CGFloat kPadding = 10.0f;
static const CGFloat kTitleFontSize = 17.0f;
static const CGFloat kDescriptionlFontSize = 13.0f;
static const CGFloat kLightBoxAlpha = 0.4f;
static const CGFloat kMessageBoxAlpha = 0.9f;
static const CGFloat kIconSize = 24.0f;
static const CGFloat KAnimationDuration = 0.3f;
CGPoint CGRectCenter(CGRect rect)
{
return CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
}
#pragma mark - Class methods
+ (id)showHUDAddedTo:(UIView *)view animated:(BOOL)animated
{
DebugLog(@"");
MEActivityHUD *hud = [[self alloc] initWithView:view];
[view addSubview:hud];
[hud show:animated];
return hud;
}
+ (BOOL)hideHUDForView:(UIView *)view animated:(BOOL)animated
{
DebugLog(@"");
MEActivityHUD *hud = [self HUDForView:view];
if (hud != nil) {
[hud hide:animated];
return YES;
}
return NO;
}
+ (BOOL)changeHUDForView:(UIView *)view animated:(BOOL)animated withTitle:(NSString *)title withDescription:(NSString *)description andMode:(MBProgressHUDMode)mode
{
DebugLog(@"");
MEActivityHUD *hud = [self HUDForView:view];
if (hud != nil) {
hud.titleLabel.text = title;
hud.descriptionLabel.text = description;
switch (mode) {
case MEActivityHUDModeIndeterminate: {
hud.activitiyView.hidden = NO;
hud.iconViewHolder.hidden = YES;
} break;
case MEActivityHUDModeSucced: {
hud.activitiyView.hidden = YES;
UIImage *image = [UIImage imageNamed:@"checkmark_icon.png"];
hud.iconViewHolder.image = image;
} break;
case MEActivityHUDModeFailed: {
hud.activitiyView.hidden = YES;
UIImage *image = [UIImage imageNamed:@"cross_icon.png"];
hud.iconViewHolder.image = image;
} break;
default:
break;
}
return YES;
}
return NO;
}
+ (id)showHUDAddedTo:(UIView *)view animated:(BOOL)animated withTime:(float)time
{
DebugLog(@"");
MEActivityHUD *hud = [[self alloc] initWithView:view];
[view addSubview:hud];
[hud show:animated];
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, time * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
// Hide
[hud hide:animated];
});
return hud;
}
+ (id)HUDForView:(UIView *)view
{
DebugLog(@"");
NSEnumerator *subviewsEnum = [view.subviews reverseObjectEnumerator];
for (UIView *subview in subviewsEnum) {
if ([subview isKindOfClass:self]) {
return (MEActivityHUD *)subview;
}
}
return nil;
}
#pragma mark - Lifecycle
- (id)initWithView:(UIView *)view
{
DebugLog(@"");
NSAssert(view, @"View must not be nil.");
return [self initWithFrame:view.bounds];
}
- (id)initWithFrame:(CGRect)frame
{
DebugLog(@"");
self = [super initWithFrame:frame];
if (self) {
// Initialization code
float floatLabelPadding = kPadding;
// The light box
self.lightBoxBackgroundView = [[UIView alloc] initWithFrame:frame];
[self.lightBoxBackgroundView setBackgroundColor:[UIColor blackColor]];
[self.lightBoxBackgroundView setHidden:YES];
[self addSubview:self.lightBoxBackgroundView];
// The message box
self.messageBoxView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 280.0f, 140.0f)];
[self.messageBoxView setBackgroundColor:[UIColor colorWithRed:250/255.0f green:250/255.0f blue:250/255.0f alpha:1.0f]];
[self.messageBoxView setHidden:YES];
[self.messageBoxView setCenter:CGRectCenter(frame)];
[self.messageBoxView.layer setCornerRadius:10.0f];
[self addSubview:self.messageBoxView];
// The title
self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(floatLabelPadding, floatLabelPadding, self.messageBoxView.frame.size.width - (floatLabelPadding*2), 30.0f)];
[self.titleLabel setBackgroundColor:[UIColor clearColor]];
[self.titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:kTitleFontSize]];
//[self.titleLabel setFont:[UIFont boldSystemFontOfSize:17.0]];
[self.titleLabel setText:@""];
[self.titleLabel setNumberOfLines:1];
self.titleLabel.textAlignment = NSTextAlignmentCenter;
[self.messageBoxView addSubview:self.titleLabel];
// The description
self.descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(floatLabelPadding, 30.0f, self.messageBoxView.frame.size.width - (floatLabelPadding*2), 60.0f)];
[self.descriptionLabel setBackgroundColor:[UIColor clearColor]];
[self.descriptionLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:kDescriptionlFontSize]];
//[self.descriptionLabel setFont:[UIFont systemFontOfSize:13.0]];
[self.descriptionLabel setText:@""];
[self.descriptionLabel setNumberOfLines:3];
self.descriptionLabel.textAlignment = NSTextAlignmentCenter;
[self.messageBoxView addSubview:self.descriptionLabel];
// The activity view
self.activitiyView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[self.activitiyView setFrame:CGRectMake(140-12, 100, kIconSize, kIconSize)];
[self.activitiyView startAnimating];
[self.messageBoxView addSubview:self.activitiyView];
// The icon view
self.iconView = [[UIView alloc] initWithFrame:CGRectMake(floatLabelPadding, 90.0f, self.messageBoxView.frame.size.width - (floatLabelPadding*2), 40.0f)];
[self.iconView setBackgroundColor:[UIColor clearColor]];
[self.iconView setHidden:NO];
[self.messageBoxView addSubview:self.iconView];
// The icon view holder
self.iconViewHolder = [[UIImageView alloc] initWithFrame:CGRectMake(((self.messageBoxView.frame.size.width - (floatLabelPadding*2))/2)-(kIconSize/2), kIconSize/2, kIconSize, kIconSize)];
[self.iconViewHolder setBackgroundColor:[UIColor clearColor]];
[self.iconViewHolder setHidden:NO];
[self.iconView addSubview:self.iconViewHolder];
}
return self;
}
#pragma mark - Show & hide
- (void)show:(BOOL)animated
{
[self.lightBoxBackgroundView setAlpha:0.0f];
[self.messageBoxView setAlpha:0.0f];
[self.titleLabel setAlpha:0.0f];
[self.descriptionLabel setAlpha:0.0f];
[self.activitiyView setAlpha:0.0f];
[self.iconView setAlpha:0.0f];
[self.lightBoxBackgroundView setHidden:NO];
[self.messageBoxView setHidden:NO];
[self.titleLabel setHidden:NO];
[self.descriptionLabel setHidden:NO];
// Delay execution
float floatAnimationBoxPadding = kPadding;
CGRect oldFrame = self.messageBoxView.frame;
[self.messageBoxView setFrame:CGRectMake(oldFrame.origin.x - floatAnimationBoxPadding, oldFrame.origin.y - floatAnimationBoxPadding, oldFrame.size.width + (floatAnimationBoxPadding*2), oldFrame.size.height + (floatAnimationBoxPadding*2))];
[UIView animateWithDuration: animated ? KAnimationDuration : 0.0f
delay:0.0f
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[self.titleLabel setAlpha:1.0f];
[self.descriptionLabel setAlpha:1.0f];
[self.activitiyView setAlpha:1.0f];
[self.iconView setAlpha:1.0f];
[self.lightBoxBackgroundView setAlpha:kLightBoxAlpha];
[self.messageBoxView setAlpha:kMessageBoxAlpha];
[self.messageBoxView setFrame:oldFrame];
}
completion:^(BOOL finished) {
}];
}
- (void)hide:(BOOL)animated
{
DebugLog(@"");
float floatAnimationBoxPadding = kPadding;
CGRect oldFrame = self.messageBoxView.frame;
[self.titleLabel setAlpha:0.0f];
[self.descriptionLabel setAlpha:0.0f];
[self.activitiyView setAlpha:0.0f];
[self.iconView setAlpha:0.0f];
[UIView animateWithDuration:animated ? KAnimationDuration : 0.0f
delay:0.0f
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[self.messageBoxView setAlpha:0.0f];
[self.messageBoxView setFrame:CGRectMake(oldFrame.origin.x + floatAnimationBoxPadding, oldFrame.origin.y + floatAnimationBoxPadding, oldFrame.size.width - (floatAnimationBoxPadding*2), oldFrame.size.height - (floatAnimationBoxPadding*2))];
}
completion:^(BOOL finished) {
[self.messageBoxView removeFromSuperview];
[self.lightBoxBackgroundView removeFromSuperview];
[self removeFromSuperview];
}];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end