-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSVProgressHUD.m
More file actions
572 lines (433 loc) · 19.5 KB
/
Copy pathSVProgressHUD.m
File metadata and controls
572 lines (433 loc) · 19.5 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
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
//
// SVProgressHUD.m
//
// Created by Sam Vermette on 27.03.11.
// Copyright 2011 Sam Vermette. All rights reserved.
//
// https://github.com/samvermette/SVProgressHUD
//
#import "SVProgressHUD.h"
#import <QuartzCore/QuartzCore.h>
@interface SVProgressHUD ()
@property (nonatomic, readwrite) SVProgressHUDMaskType maskType;
@property (nonatomic, readwrite) BOOL showNetworkIndicator;
@property (nonatomic, retain) NSTimer *fadeOutTimer;
@property (nonatomic, readonly) UIWindow *overlayWindow;
@property (nonatomic, readonly) UIView *hudView;
@property (nonatomic, readonly) UILabel *stringLabel;
@property (nonatomic, readonly) UIImageView *imageView;
@property (nonatomic, readonly) UIActivityIndicatorView *spinnerView;
@property (nonatomic, readonly) CGFloat visibleKeyboardHeight;
- (void)showWithStatus:(NSString*)string maskType:(SVProgressHUDMaskType)hudMaskType networkIndicator:(BOOL)show;
- (void)setStatus:(NSString*)string;
- (void)registerNotifications;
- (void)moveToPoint:(CGPoint)newCenter rotateAngle:(CGFloat)angle;
- (void)positionHUD:(NSNotification*)notification;
- (void)dismiss;
- (void)dismissWithStatus:(NSString*)string error:(BOOL)error;
- (void)dismissWithStatus:(NSString*)string error:(BOOL)error afterDelay:(NSTimeInterval)seconds;
@end
@implementation SVProgressHUD
@synthesize overlayWindow, hudView, maskType, showNetworkIndicator, fadeOutTimer, stringLabel, imageView, spinnerView, visibleKeyboardHeight;
static SVProgressHUD *sharedView = nil;
- (void)dealloc {
self.fadeOutTimer = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self];
[hudView release];
[stringLabel release];
[imageView release];
[spinnerView release];
[super dealloc];
}
+ (SVProgressHUD*)sharedView {
if(sharedView == nil)
sharedView = [[SVProgressHUD alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
return sharedView;
}
+ (void)setStatus:(NSString *)string {
[[SVProgressHUD sharedView] setStatus:string];
}
#pragma mark - Show Methods
+ (void)show {
[[SVProgressHUD sharedView] showWithStatus:nil maskType:SVProgressHUDMaskTypeNone networkIndicator:NO];
}
+ (void)showWithStatus:(NSString *)status {
[[SVProgressHUD sharedView] showWithStatus:status maskType:SVProgressHUDMaskTypeNone networkIndicator:NO];
}
+ (void)showWithMaskType:(SVProgressHUDMaskType)maskType {
[[SVProgressHUD sharedView] showWithStatus:nil maskType:maskType networkIndicator:NO];
}
+ (void)showWithStatus:(NSString*)status maskType:(SVProgressHUDMaskType)maskType {
[[SVProgressHUD sharedView] showWithStatus:status maskType:maskType networkIndicator:NO];
}
+ (void)showWithStatus:(NSString *)status networkIndicator:(BOOL)show {
[[SVProgressHUD sharedView] showWithStatus:status maskType:SVProgressHUDMaskTypeNone networkIndicator:show];
}
+ (void)showWithMaskType:(SVProgressHUDMaskType)maskType networkIndicator:(BOOL)show {
[[SVProgressHUD sharedView] showWithStatus:nil maskType:maskType networkIndicator:show];
}
+ (void)showWithStatus:(NSString*)status maskType:(SVProgressHUDMaskType)maskType networkIndicator:(BOOL)show {
[[SVProgressHUD sharedView] showWithStatus:status maskType:maskType networkIndicator:show];
}
+ (void)showSuccessWithStatus:(NSString *)string {
[SVProgressHUD showSuccessWithStatus:string duration:1];
}
+ (void)showSuccessWithStatus:(NSString *)string duration:(NSTimeInterval)duration {
[SVProgressHUD show];
[SVProgressHUD dismissWithSuccess:string afterDelay:duration];
}
+ (void)showErrorWithStatus:(NSString *)string {
[SVProgressHUD showErrorWithStatus:string duration:1];
}
+ (void)showErrorWithStatus:(NSString *)string duration:(NSTimeInterval)duration {
[SVProgressHUD show];
[SVProgressHUD dismissWithError:string afterDelay:duration];
}
#pragma mark - Dismiss Methods
+ (void)dismiss {
[[SVProgressHUD sharedView] dismiss];
}
+ (void)dismissWithSuccess:(NSString*)successString {
[[SVProgressHUD sharedView] dismissWithStatus:successString error:NO];
}
+ (void)dismissWithSuccess:(NSString *)successString afterDelay:(NSTimeInterval)seconds {
[[SVProgressHUD sharedView] dismissWithStatus:successString error:NO afterDelay:seconds];
}
+ (void)dismissWithError:(NSString*)errorString {
[[SVProgressHUD sharedView] dismissWithStatus:errorString error:YES];
}
+ (void)dismissWithError:(NSString *)errorString afterDelay:(NSTimeInterval)seconds {
[[SVProgressHUD sharedView] dismissWithStatus:errorString error:YES afterDelay:seconds];
}
#pragma mark - Instance Methods
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
[self.overlayWindow addSubview:self];
self.userInteractionEnabled = NO;
self.backgroundColor = [UIColor clearColor];
self.alpha = 0;
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
}
return self;
}
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
switch (self.maskType) {
case SVProgressHUDMaskTypeBlack: {
[[UIColor colorWithWhite:0 alpha:0.5] set];
CGContextFillRect(context, self.bounds);
break;
}
case SVProgressHUDMaskTypeGradient: {
size_t locationsCount = 2;
CGFloat locations[2] = {0.0f, 1.0f};
CGFloat colors[8] = {0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.75f};
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, colors, locations, locationsCount);
CGColorSpaceRelease(colorSpace);
CGPoint center = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);
float radius = MIN(self.bounds.size.width , self.bounds.size.height) ;
CGContextDrawRadialGradient (context, gradient, center, 0, center, radius, kCGGradientDrawsAfterEndLocation);
CGGradientRelease(gradient);
break;
}
}
}
- (void)setStatus:(NSString *)string {
CGFloat hudWidth = 100;
CGFloat hudHeight = 100;
CGFloat stringWidth = 0;
CGFloat stringHeight = 0;
CGRect labelRect = CGRectZero;
if(string) {
CGSize stringSize = [string sizeWithFont:self.stringLabel.font constrainedToSize:CGSizeMake(200, 300)];
stringWidth = stringSize.width;
stringHeight = stringSize.height;
hudHeight = 80+stringHeight;
if(stringWidth > hudWidth)
hudWidth = ceil(stringWidth/2)*2;
if(hudHeight > 100) {
labelRect = CGRectMake(12, 66, hudWidth, stringHeight);
hudWidth+=24;
} else {
hudWidth+=24;
labelRect = CGRectMake(0, 66, hudWidth, stringHeight);
}
}
self.hudView.bounds = CGRectMake(0, 0, hudWidth, hudHeight);
if(string)
self.imageView.center = CGPointMake(CGRectGetWidth(self.hudView.bounds)/2, 36);
else
self.imageView.center = CGPointMake(CGRectGetWidth(self.hudView.bounds)/2, CGRectGetHeight(self.hudView.bounds)/2);
self.stringLabel.hidden = NO;
self.stringLabel.text = string;
self.stringLabel.frame = labelRect;
if(string)
self.spinnerView.center = CGPointMake(ceil(CGRectGetWidth(self.hudView.bounds)/2)+0.5, 40.5);
else
self.spinnerView.center = CGPointMake(ceil(CGRectGetWidth(self.hudView.bounds)/2)+0.5, ceil(self.hudView.bounds.size.height/2)+0.5);
}
- (void)setFadeOutTimer:(NSTimer *)newTimer {
if(fadeOutTimer)
[fadeOutTimer invalidate], [fadeOutTimer release], fadeOutTimer = nil;
if(newTimer)
fadeOutTimer = [newTimer retain];
}
- (void)registerNotifications {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(positionHUD:)
name:UIApplicationDidChangeStatusBarOrientationNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(positionHUD:)
name:UIKeyboardWillHideNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(positionHUD:)
name:UIKeyboardDidHideNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(positionHUD:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(positionHUD:)
name:UIKeyboardDidShowNotification
object:nil];
}
- (void)positionHUD:(NSNotification*)notification {
CGFloat keyboardHeight;
double animationDuration;
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if(notification) {
NSDictionary* keyboardInfo = [notification userInfo];
CGRect keyboardFrame = [[keyboardInfo valueForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
animationDuration = [[keyboardInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
if(notification.name == UIKeyboardWillShowNotification || notification.name == UIKeyboardDidShowNotification) {
if(UIInterfaceOrientationIsPortrait(orientation))
keyboardHeight = keyboardFrame.size.height;
else
keyboardHeight = keyboardFrame.size.width;
} else
keyboardHeight = 0;
} else {
keyboardHeight = self.visibleKeyboardHeight;
}
CGRect orientationFrame = [UIScreen mainScreen].bounds;
CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame;
if(UIInterfaceOrientationIsLandscape(orientation)) {
float temp = orientationFrame.size.width;
orientationFrame.size.width = orientationFrame.size.height;
orientationFrame.size.height = temp;
temp = statusBarFrame.size.width;
statusBarFrame.size.width = statusBarFrame.size.height;
statusBarFrame.size.height = temp;
}
CGFloat activeHeight = orientationFrame.size.height;
if(keyboardHeight > 0)
activeHeight += statusBarFrame.size.height*2;
activeHeight -= keyboardHeight;
CGFloat posY = floor(activeHeight*0.45);
CGFloat posX = orientationFrame.size.width/2;
CGPoint newCenter;
CGFloat rotateAngle;
switch (orientation) {
case UIInterfaceOrientationPortraitUpsideDown:
rotateAngle = M_PI;
newCenter = CGPointMake(posX, orientationFrame.size.height-posY);
break;
case UIInterfaceOrientationLandscapeLeft:
rotateAngle = -M_PI/2.0f;
newCenter = CGPointMake(posY, posX);
break;
case UIInterfaceOrientationLandscapeRight:
rotateAngle = M_PI/2.0f;
newCenter = CGPointMake(orientationFrame.size.height-posY, posX);
break;
default: // as UIInterfaceOrientationPortrait
rotateAngle = 0.0;
newCenter = CGPointMake(posX, posY);
break;
}
if(notification) {
[UIView animateWithDuration:animationDuration
delay:0
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
[self moveToPoint:newCenter rotateAngle:rotateAngle];
} completion:NULL];
}
else {
[self moveToPoint:newCenter rotateAngle:rotateAngle];
}
}
- (void)moveToPoint:(CGPoint)newCenter rotateAngle:(CGFloat)angle {
self.hudView.transform = CGAffineTransformMakeRotation(angle);
self.hudView.center = newCenter;
}
#pragma mark - Master show/dismiss methods
- (void)showWithStatus:(NSString*)string maskType:(SVProgressHUDMaskType)hudMaskType networkIndicator:(BOOL)show {
self.fadeOutTimer = nil;
if(self.showNetworkIndicator)
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
self.showNetworkIndicator = show;
if(self.showNetworkIndicator)
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
self.imageView.hidden = YES;
self.maskType = hudMaskType;
[self setStatus:string];
[self.spinnerView startAnimating];
if(self.maskType != SVProgressHUDMaskTypeNone) {
self.overlayWindow.userInteractionEnabled = YES;
} else {
self.overlayWindow.userInteractionEnabled = NO;
}
[self.overlayWindow makeKeyAndVisible];
[self positionHUD:nil];
if(self.alpha != 1) {
[self registerNotifications];
self.hudView.transform = CGAffineTransformScale(self.hudView.transform, 1.3, 1.3);
[UIView animateWithDuration:0.15
delay:0
options:UIViewAnimationOptionAllowUserInteraction | UIViewAnimationCurveEaseOut | UIViewAnimationOptionBeginFromCurrentState
animations:^{
self.hudView.transform = CGAffineTransformScale(self.hudView.transform, 1/1.3, 1/1.3);
self.alpha = 1;
}
completion:NULL];
}
[self setNeedsDisplay];
}
- (void)dismissWithStatus:(NSString*)string error:(BOOL)error {
[self dismissWithStatus:string error:error afterDelay:0.9];
}
- (void)dismissWithStatus:(NSString *)string error:(BOOL)error afterDelay:(NSTimeInterval)seconds {
if(self.alpha != 1)
return;
if(self.showNetworkIndicator) {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
self.showNetworkIndicator = NO;
}
if(error)
self.imageView.image = [UIImage imageNamed:@"SVProgressHUD.bundle/error.png"];
else
self.imageView.image = [UIImage imageNamed:@"SVProgressHUD.bundle/success.png"];
self.imageView.hidden = NO;
[self setStatus:string];
[self.spinnerView stopAnimating];
self.fadeOutTimer = [NSTimer scheduledTimerWithTimeInterval:seconds target:self selector:@selector(dismiss) userInfo:nil repeats:NO];
}
- (void)dismiss {
if(self.showNetworkIndicator) {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
self.showNetworkIndicator = NO;
}
[UIView animateWithDuration:0.15
delay:0
options:UIViewAnimationCurveEaseIn | UIViewAnimationOptionAllowUserInteraction
animations:^{
sharedView.hudView.transform = CGAffineTransformScale(sharedView.hudView.transform, 0.8, 0.8);
sharedView.alpha = 0;
}
completion:^(BOOL finished){
if(sharedView.alpha == 0) {
[[NSNotificationCenter defaultCenter] removeObserver:sharedView];
[overlayWindow release], overlayWindow = nil;
[sharedView release], sharedView = nil;
// find the frontmost window that is an actual UIWindow and make it keyVisible
[[UIApplication sharedApplication].windows enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(UIWindow *window, NSUInteger idx, BOOL *stop) {
if([window isKindOfClass:[UIWindow class]] && window.windowLevel == UIWindowLevelNormal) {
[window makeKeyWindow];
*stop = YES;
}
}];
// uncomment to make sure UIWindow is gone from app.windows
//NSLog(@"%@", [UIApplication sharedApplication].windows);
//NSLog(@"keyWindow = %@", [UIApplication sharedApplication].keyWindow);
}
}];
}
#pragma mark - Utilities
+ (BOOL)isVisible {
return (sharedView.alpha == 1);
}
#pragma mark - Getters
- (UIWindow *)overlayWindow {
if(!overlayWindow) {
overlayWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
overlayWindow.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
overlayWindow.backgroundColor = [UIColor clearColor];
overlayWindow.userInteractionEnabled = NO;
}
return overlayWindow;
}
- (UIView *)hudView {
if(!hudView) {
hudView = [[UIView alloc] initWithFrame:CGRectZero];
hudView.layer.cornerRadius = 10;
hudView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.8];
hudView.autoresizingMask = (UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin);
[self addSubview:hudView];
}
return hudView;
}
- (UILabel *)stringLabel {
if (stringLabel == nil) {
stringLabel = [[UILabel alloc] initWithFrame:CGRectZero];
stringLabel.textColor = [UIColor whiteColor];
stringLabel.backgroundColor = [UIColor clearColor];
stringLabel.adjustsFontSizeToFitWidth = YES;
stringLabel.textAlignment = UITextAlignmentCenter;
stringLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
stringLabel.font = [UIFont boldSystemFontOfSize:16];
stringLabel.shadowColor = [UIColor blackColor];
stringLabel.shadowOffset = CGSizeMake(0, -1);
stringLabel.numberOfLines = 0;
[self.hudView addSubview:stringLabel];
}
return stringLabel;
}
- (UIImageView *)imageView {
if (imageView == nil) {
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 28, 28)];
[self.hudView addSubview:imageView];
}
return imageView;
}
- (UIActivityIndicatorView *)spinnerView {
if (spinnerView == nil) {
spinnerView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
spinnerView.hidesWhenStopped = YES;
spinnerView.bounds = CGRectMake(0, 0, 37, 37);
[self.hudView addSubview:spinnerView];
}
return spinnerView;
}
- (CGFloat)visibleKeyboardHeight {
NSAutoreleasePool *autoreleasePool = [[NSAutoreleasePool alloc] init];
UIWindow *keyboardWindow = nil;
for (UIWindow *testWindow in [[UIApplication sharedApplication] windows]) {
if(![[testWindow class] isEqual:[UIWindow class]]) {
keyboardWindow = testWindow;
break;
}
}
// Locate UIKeyboard.
UIView *foundKeyboard = nil;
for (UIView *possibleKeyboard in [keyboardWindow subviews]) {
// iOS 4 sticks the UIKeyboard inside a UIPeripheralHostView.
if ([[possibleKeyboard description] hasPrefix:@"<UIPeripheralHostView"]) {
possibleKeyboard = [[possibleKeyboard subviews] objectAtIndex:0];
}
if ([[possibleKeyboard description] hasPrefix:@"<UIKeyboard"]) {
foundKeyboard = possibleKeyboard;
break;
}
}
[autoreleasePool release];
if(foundKeyboard && foundKeyboard.bounds.size.height > 100)
return foundKeyboard.bounds.size.height;
return 0;
}
@end