-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgressHUD.m
executable file
·414 lines (375 loc) · 21.8 KB
/
ProgressHUD.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
//
// Copyright (c) 2018 Related Code - http://relatedcode.com
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#import "ProgressHUD.h"
//-------------------------------------------------------------------------------------------------------------------------------------------------
@interface ProgressHUD()
{
UIWindow *window;
UIView *viewBackground;
UIToolbar *toolbarHUD;
UIActivityIndicatorView *spinner;
UIImageView *imageView;
UILabel *labelStatus;
}
@end
//-------------------------------------------------------------------------------------------------------------------------------------------------
@implementation ProgressHUD
//-------------------------------------------------------------------------------------------------------------------------------------------------
+ (ProgressHUD *)shared
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
static dispatch_once_t once;
static ProgressHUD *progressHUD;
//---------------------------------------------------------------------------------------------------------------------------------------------
dispatch_once(&once, ^{ progressHUD = [[ProgressHUD alloc] init]; });
//---------------------------------------------------------------------------------------------------------------------------------------------
return progressHUD;
}
#pragma mark - Display methods
//-------------------------------------------------------------------------------------------------------------------------------------------------
+ (void)dismiss
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
dispatch_async(dispatch_get_main_queue(), ^{
[[self shared] hudHide];
});
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
+ (void)show
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
dispatch_async(dispatch_get_main_queue(), ^{
[[self shared] hudCreate:nil image:nil spin:YES hide:NO interaction:YES];
});
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
+ (void)show:(NSString *)status
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
dispatch_async(dispatch_get_main_queue(), ^{
[[self shared] hudCreate:status image:nil spin:YES hide:NO interaction:YES];
});
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
+ (void)show:(NSString *)status Interaction:(BOOL)interaction
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
dispatch_async(dispatch_get_main_queue(), ^{
[[self shared] hudCreate:status image:nil spin:YES hide:NO interaction:interaction];
});
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
+ (void)showSuccess
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
dispatch_async(dispatch_get_main_queue(), ^{
[[self shared] hudCreate:nil image:[self shared].imageSuccess spin:NO hide:YES interaction:YES];
});
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
+ (void)showSuccess:(NSString *)status
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
dispatch_async(dispatch_get_main_queue(), ^{
[[self shared] hudCreate:status image:[self shared].imageSuccess spin:NO hide:YES interaction:YES];
});
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
+ (void)showSuccess:(NSString *)status Interaction:(BOOL)interaction
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
dispatch_async(dispatch_get_main_queue(), ^{
[[self shared] hudCreate:status image:[self shared].imageSuccess spin:NO hide:YES interaction:interaction];
});
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
+ (void)showError
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
dispatch_async(dispatch_get_main_queue(), ^{
[[self shared] hudCreate:nil image:[self shared].imageError spin:NO hide:YES interaction:YES];
});
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
+ (void)showError:(NSString *)status
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
dispatch_async(dispatch_get_main_queue(), ^{
[[self shared] hudCreate:status image:[self shared].imageError spin:NO hide:YES interaction:YES];
});
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
+ (void)showError:(NSString *)status Interaction:(BOOL)interaction
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
dispatch_async(dispatch_get_main_queue(), ^{
[[self shared] hudCreate:status image:[self shared].imageError spin:NO hide:YES interaction:interaction];
});
}
#pragma mark - Property methods
//-------------------------------------------------------------------------------------------------------------------------------------------------
+ (void)statusFont:(UIFont *)font { [self shared].statusFont = font; }
+ (void)statusColor:(UIColor *)color { [self shared].statusColor = color; }
+ (void)spinnerColor:(UIColor *)color { [self shared].spinnerColor = color; }
+ (void)hudColor:(UIColor *)color { [self shared].hudColor = color; }
+ (void)backgroundColor:(UIColor *)color { [self shared].backgroundColor = color; }
+ (void)imageSuccess:(UIImage *)image { [self shared].imageSuccess = image; }
+ (void)imageError:(UIImage *)image { [self shared].imageError = image; }
//-------------------------------------------------------------------------------------------------------------------------------------------------
#pragma mark -
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (id)init
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
self = [super initWithFrame:[[UIScreen mainScreen] bounds]];
//---------------------------------------------------------------------------------------------------------------------------------------------
self.statusFont = [UIFont boldSystemFontOfSize:16];
self.statusColor = [UIColor blackColor];
self.spinnerColor = [UIColor grayColor];
self.hudColor = [UIColor colorWithWhite:0.0 alpha:0.1];
self.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.2];
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
self.imageSuccess = [UIImage imageNamed:@"ProgressHUD.bundle/progresshud-success" inBundle:bundle compatibleWithTraitCollection:nil];
self.imageError = [UIImage imageNamed:@"ProgressHUD.bundle/progresshud-error" inBundle:bundle compatibleWithTraitCollection:nil];
//---------------------------------------------------------------------------------------------------------------------------------------------
id<UIApplicationDelegate> delegate = [[UIApplication sharedApplication] delegate];
//---------------------------------------------------------------------------------------------------------------------------------------------
if ([delegate respondsToSelector:@selector(window)])
window = [delegate performSelector:@selector(window)];
else window = [[UIApplication sharedApplication] keyWindow];
//---------------------------------------------------------------------------------------------------------------------------------------------
viewBackground = nil; toolbarHUD = nil; spinner = nil; imageView = nil; labelStatus = nil;
//---------------------------------------------------------------------------------------------------------------------------------------------
self.alpha = 0;
//---------------------------------------------------------------------------------------------------------------------------------------------
return self;
}
#pragma mark -
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)hudCreate:(NSString *)status image:(UIImage *)image spin:(BOOL)spin hide:(BOOL)hide interaction:(BOOL)interaction
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
if (toolbarHUD == nil)
{
toolbarHUD = [[UIToolbar alloc] initWithFrame:CGRectZero];
toolbarHUD.translucent = YES;
toolbarHUD.backgroundColor = self.hudColor;
toolbarHUD.layer.cornerRadius = 10;
toolbarHUD.layer.masksToBounds = YES;
[self registerNotifications];
}
//---------------------------------------------------------------------------------------------------------------------------------------------
if (toolbarHUD.superview == nil)
{
if (interaction == NO)
{
viewBackground = [[UIView alloc] initWithFrame:window.frame];
viewBackground.backgroundColor = self.backgroundColor;
[window addSubview:viewBackground];
[viewBackground addSubview:toolbarHUD];
}
else [window addSubview:toolbarHUD];
}
//---------------------------------------------------------------------------------------------------------------------------------------------
if (spinner == nil)
{
spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
spinner.color = self.spinnerColor;
spinner.hidesWhenStopped = YES;
}
if (spinner.superview == nil) [toolbarHUD addSubview:spinner];
//---------------------------------------------------------------------------------------------------------------------------------------------
if (imageView == nil)
{
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 28, 28)];
}
if (imageView.superview == nil) [toolbarHUD addSubview:imageView];
//---------------------------------------------------------------------------------------------------------------------------------------------
if (labelStatus == nil)
{
labelStatus = [[UILabel alloc] initWithFrame:CGRectZero];
labelStatus.font = self.statusFont;
labelStatus.textColor = self.statusColor;
labelStatus.backgroundColor = [UIColor clearColor];
labelStatus.textAlignment = NSTextAlignmentCenter;
labelStatus.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
labelStatus.numberOfLines = 0;
}
if (labelStatus.superview == nil) [toolbarHUD addSubview:labelStatus];
//---------------------------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------------------------
labelStatus.text = status;
labelStatus.hidden = (status == nil) ? YES : NO;
//---------------------------------------------------------------------------------------------------------------------------------------------
imageView.image = image;
imageView.hidden = (image == nil) ? YES : NO;
//---------------------------------------------------------------------------------------------------------------------------------------------
if (spin) [spinner startAnimating]; else [spinner stopAnimating];
//---------------------------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------------------------
[self hudSize];
[self hudPosition:nil];
[self hudShow];
//---------------------------------------------------------------------------------------------------------------------------------------------
if (hide) [self timedHide];
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)registerNotifications
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hudPosition:)
name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hudPosition:) name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hudPosition:) name:UIKeyboardDidHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hudPosition:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hudPosition:) name:UIKeyboardDidShowNotification object:nil];
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)hudDestroy
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
//---------------------------------------------------------------------------------------------------------------------------------------------
[labelStatus removeFromSuperview]; labelStatus = nil;
[imageView removeFromSuperview]; imageView = nil;
[spinner removeFromSuperview]; spinner = nil;
[toolbarHUD removeFromSuperview]; toolbarHUD = nil;
[viewBackground removeFromSuperview]; viewBackground = nil;
}
#pragma mark -
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)hudSize
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
CGRect rectLabel = CGRectZero;
CGFloat widthHUD = 100, heightHUD = 100;
//---------------------------------------------------------------------------------------------------------------------------------------------
if (labelStatus.text != nil)
{
NSDictionary *attributes = @{NSFontAttributeName:labelStatus.font};
NSInteger options = NSStringDrawingUsesFontLeading | NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin;
rectLabel = [labelStatus.text boundingRectWithSize:CGSizeMake(200, 300) options:options attributes:attributes context:NULL];
widthHUD = rectLabel.size.width + 50;
heightHUD = rectLabel.size.height + 75;
if (widthHUD < 100) widthHUD = 100;
if (heightHUD < 100) heightHUD = 100;
rectLabel.origin.x = (widthHUD - rectLabel.size.width) / 2;
rectLabel.origin.y = (heightHUD - rectLabel.size.height) / 2 + 25;
}
//---------------------------------------------------------------------------------------------------------------------------------------------
toolbarHUD.bounds = CGRectMake(0, 0, widthHUD, heightHUD);
//---------------------------------------------------------------------------------------------------------------------------------------------
CGFloat imageX = widthHUD/2;
CGFloat imageY = (labelStatus.text == nil) ? heightHUD/2 : 36;
imageView.center = spinner.center = CGPointMake(imageX, imageY);
//---------------------------------------------------------------------------------------------------------------------------------------------
labelStatus.frame = rectLabel;
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)hudPosition:(NSNotification *)notification
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
CGFloat heightKeyboard = 0;
NSTimeInterval duration = 0;
//---------------------------------------------------------------------------------------------------------------------------------------------
if (notification != nil)
{
NSDictionary *info = [notification userInfo];
CGRect keyboard = [[info valueForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
duration = [[info valueForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
if ((notification.name == UIKeyboardWillShowNotification) || (notification.name == UIKeyboardDidShowNotification))
{
heightKeyboard = keyboard.size.height;
}
}
else heightKeyboard = [self keyboardHeight];
//---------------------------------------------------------------------------------------------------------------------------------------------
CGRect screen = [UIScreen mainScreen].bounds;
CGPoint center = CGPointMake(screen.size.width/2, (screen.size.height-heightKeyboard)/2);
//---------------------------------------------------------------------------------------------------------------------------------------------
[UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
self->toolbarHUD.center = CGPointMake(center.x, center.y);
} completion:nil];
//---------------------------------------------------------------------------------------------------------------------------------------------
if (viewBackground != nil) viewBackground.frame = window.frame;
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (CGFloat)keyboardHeight
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
for (UIWindow *testWindow in [[UIApplication sharedApplication] windows])
{
if ([[testWindow class] isEqual:[UIWindow class]] == NO)
{
for (UIView *possibleKeyboard in [testWindow subviews])
{
if ([[possibleKeyboard description] hasPrefix:@"<UIPeripheralHostView"])
{
return possibleKeyboard.bounds.size.height;
}
else if ([[possibleKeyboard description] hasPrefix:@"<UIInputSetContainerView"])
{
for (UIView *hostKeyboard in [possibleKeyboard subviews])
{
if ([[hostKeyboard description] hasPrefix:@"<UIInputSetHost"])
{
return hostKeyboard.frame.size.height;
}
}
}
}
}
}
return 0;
}
#pragma mark -
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)hudShow
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
if (self.alpha == 0)
{
self.alpha = 1;
toolbarHUD.alpha = 0;
toolbarHUD.transform = CGAffineTransformScale(toolbarHUD.transform, 1.4, 1.4);
UIViewAnimationOptions options = UIViewAnimationOptionAllowUserInteraction | UIViewAnimationCurveEaseOut;
[UIView animateWithDuration:0.15 delay:0 options:options animations:^{
self->toolbarHUD.transform = CGAffineTransformScale(self->toolbarHUD.transform, 1/1.4, 1/1.4);
self->toolbarHUD.alpha = 1;
} completion:nil];
}
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)hudHide
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
if (self.alpha == 1)
{
UIViewAnimationOptions options = UIViewAnimationOptionAllowUserInteraction | UIViewAnimationCurveEaseIn;
[UIView animateWithDuration:0.15 delay:0 options:options animations:^{
self->toolbarHUD.transform = CGAffineTransformScale(self->toolbarHUD.transform, 0.7, 0.7);
self->toolbarHUD.alpha = 0;
}
completion:^(BOOL finished) {
[self hudDestroy];
self.alpha = 0;
}];
}
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)timedHide
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
NSTimeInterval delay = labelStatus.text.length * 0.04 + 0.5;
dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, delay * NSEC_PER_SEC);
dispatch_after(time, dispatch_get_main_queue(), ^(void){ [self hudHide]; });
}
@end