forked from SVProgressHUD/SVProgressHUD
/
SVProgressHUD.m
519 lines (384 loc) · 17.7 KB
/
SVProgressHUD.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
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
//
// 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, retain) NSTimer *fadeOutTimer;
@property (nonatomic, readonly) UIView *hudView;
@property (nonatomic, readonly) UILabel *stringLabel;
@property (nonatomic, readonly) UIImageView *imageView;
@property (nonatomic, readonly) UIActivityIndicatorView *spinnerView;
@property (nonatomic, assign) UIWindow *previousKeyWindow;
@property (nonatomic, readonly) CGFloat visibleKeyboardHeight;
- (void)showWithStatus:(NSString*)string maskType:(SVProgressHUDMaskType)hudMaskType networkIndicator:(BOOL)show;
- (void)setStatus:(NSString*)string;
- (void)registerNotifications;
- (void)positionHUD;
- (void)dismiss;
- (void)dismissWithStatus:(NSString*)string error:(BOOL)error;
- (void)dismissWithStatus:(NSString*)string error:(BOOL)error afterDelay:(NSTimeInterval)seconds;
- (void)memoryWarning:(NSNotification*)notification;
@end
@implementation SVProgressHUD
@synthesize hudView, maskType, fadeOutTimer, stringLabel, imageView, spinnerView, previousKeyWindow, visibleKeyboardHeight;
static SVProgressHUD *sharedView = nil;
- (void)dealloc {
if(fadeOutTimer != nil)
[fadeOutTimer invalidate], [fadeOutTimer release], fadeOutTimer = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self];
[sharedView release];
[hudView release];
[stringLabel release];
[imageView release];
[spinnerView release];
[super dealloc];
}
- (void)memoryWarning:(NSNotification *)notification {
if(sharedView.superview == nil) {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[sharedView release], sharedView = nil;
[hudView release], hudView = nil;
[stringLabel release], stringLabel = nil;
[imageView release], imageView = nil;
[spinnerView release], spinnerView = nil;
}
}
+ (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 showWithStatus:nil networkIndicator:SVProgressHUDShowNetworkIndicator];
}
+ (void)showWithStatus:(NSString *)status {
[SVProgressHUD showWithStatus:status networkIndicator:SVProgressHUDShowNetworkIndicator];
}
+ (void)showWithMaskType:(SVProgressHUDMaskType)maskType {
[SVProgressHUD showWithStatus:nil maskType:maskType networkIndicator:SVProgressHUDShowNetworkIndicator];
}
+ (void)showWithStatus:(NSString*)status maskType:(SVProgressHUDMaskType)maskType {
[SVProgressHUD showWithStatus:status maskType:maskType networkIndicator:SVProgressHUDShowNetworkIndicator];
}
+ (void)showWithStatus:(NSString *)status networkIndicator:(BOOL)show {
[SVProgressHUD showWithStatus:status maskType:SVProgressHUDMaskTypeNone networkIndicator:show];
}
+ (void)showWithMaskType:(SVProgressHUDMaskType)maskType networkIndicator:(BOOL)show {
[SVProgressHUD 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 show];
[SVProgressHUD dismissWithSuccess:string afterDelay:1];
}
#pragma mark - Deprecated show methods
+ (void)showInView:(UIView*)view {
[SVProgressHUD showWithStatus:nil maskType:SVProgressHUDMaskTypeNone networkIndicator:SVProgressHUDShowNetworkIndicator];
}
+ (void)showInView:(UIView*)view status:(NSString*)string {
[SVProgressHUD showWithStatus:string maskType:SVProgressHUDMaskTypeNone networkIndicator:SVProgressHUDShowNetworkIndicator];
}
+ (void)showInView:(UIView*)view status:(NSString*)string networkIndicator:(BOOL)show {
[SVProgressHUD showWithStatus:string maskType:SVProgressHUDMaskTypeNone networkIndicator:show];
}
+ (void)showInView:(UIView*)view status:(NSString*)string networkIndicator:(BOOL)show posY:(CGFloat)posY {
[SVProgressHUD showWithStatus:string maskType:SVProgressHUDMaskTypeNone networkIndicator:show];
}
+ (void)showInView:(UIView*)view status:(NSString*)string networkIndicator:(BOOL)show posY:(CGFloat)posY maskType:(SVProgressHUDMaskType)hudMaskType {
[SVProgressHUD showWithStatus:string maskType:hudMaskType networkIndicator:show];
}
#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.windowLevel = UIWindowLevelAlert;
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);
self.imageView.center = CGPointMake(CGRectGetWidth(self.hudView.bounds)/2, 36);
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)showWithStatus:(NSString*)string maskType:(SVProgressHUDMaskType)hudMaskType networkIndicator:(BOOL)show {
if(fadeOutTimer != nil)
[fadeOutTimer invalidate], [fadeOutTimer release], fadeOutTimer = nil;
[UIApplication sharedApplication].networkActivityIndicatorVisible = show;
self.imageView.hidden = YES;
self.maskType = hudMaskType;
[self setStatus:string];
[self.spinnerView startAnimating];
if(self.maskType != SVProgressHUDMaskTypeNone)
self.userInteractionEnabled = YES;
else
self.userInteractionEnabled = NO;
if(![self isKeyWindow]) {
self.previousKeyWindow = [UIApplication sharedApplication].keyWindow;
[self makeKeyAndVisible];
}
[self positionHUD];
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
animations:^{
self.hudView.transform = CGAffineTransformScale(self.hudView.transform, 1/1.3, 1/1.3);
self.alpha = 1;
}
completion:NULL];
}
[self setNeedsDisplay];
}
- (void)registerNotifications {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(positionHUD)
name:UIApplicationDidChangeStatusBarOrientationNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(memoryWarning:)
name:UIApplicationDidReceiveMemoryWarningNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(positionWithKeyboardNotification:)
name:UIKeyboardWillHideNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(positionWithKeyboardNotification:)
name:UIKeyboardWillShowNotification
object:nil];
}
- (void)positionHUD {
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
CGRect orientationFrame = [UIScreen mainScreen].bounds;
if(UIInterfaceOrientationIsLandscape(orientation)) {
float temp = orientationFrame.size.width;
orientationFrame.size.width = orientationFrame.size.height;
orientationFrame.size.height = temp;
}
CGFloat posX = orientationFrame.size.width/2;
CGFloat activeHeight = orientationFrame.size.height;
if(self.visibleKeyboardHeight > 0)
activeHeight += [UIApplication sharedApplication].statusBarFrame.size.height*2;
activeHeight -= self.visibleKeyboardHeight;
CGFloat posY = floor(activeHeight*0.45);
switch (orientation) {
case UIInterfaceOrientationPortraitUpsideDown:
self.hudView.transform = CGAffineTransformMakeRotation(M_PI);
self.hudView.center = CGPointMake(posX, orientationFrame.size.height-posY);
break;
case UIInterfaceOrientationLandscapeLeft:
self.hudView.transform = CGAffineTransformMakeRotation(- M_PI / 2.0f);
self.hudView.center = CGPointMake(posY, posX);
break;
case UIInterfaceOrientationLandscapeRight:
self.hudView.transform = CGAffineTransformMakeRotation(M_PI / 2.0f);
self.hudView.center = CGPointMake(orientationFrame.size.height-posY, posX);
break;
default: // as UIInterfaceOrientationPortrait
self.hudView.transform = CGAffineTransformMakeRotation(0.0);
self.hudView.center = CGPointMake(posX, posY);
break;
}
}
- (void)positionWithKeyboardNotification:(NSNotification*)notification {
NSDictionary* keyboardInfo = [notification userInfo];
CGRect keyboardFrame = [[keyboardInfo valueForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
double animationDuration = [[keyboardInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
[UIView animateWithDuration:animationDuration delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
if(notification.name == UIKeyboardWillHideNotification)
self.hudView.frame = CGRectOffset(self.hudView.frame, 0, floor(keyboardFrame.size.height/2));
else
self.hudView.frame = CGRectOffset(self.hudView.frame, 0, 0-floor(keyboardFrame.size.height/2));
} completion:NULL];
}
- (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;
[UIApplication sharedApplication].networkActivityIndicatorVisible = 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];
if(fadeOutTimer != nil)
[fadeOutTimer invalidate], [fadeOutTimer release], fadeOutTimer = nil;
fadeOutTimer = [[NSTimer scheduledTimerWithTimeInterval:seconds target:self selector:@selector(dismiss) userInfo:nil repeats:NO] retain];
}
- (void)dismiss {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
__block SVProgressHUD *bSelf = sharedView;
[UIView animateWithDuration:0.15
delay:0
options:UIViewAnimationCurveEaseIn | UIViewAnimationOptionAllowUserInteraction
animations:^{
bSelf.hudView.transform = CGAffineTransformScale(bSelf.hudView.transform, 0.8, 0.8);
bSelf.alpha = 0;
}
completion:^(BOOL finished){
if(bSelf.alpha == 0) {
[[NSNotificationCenter defaultCenter] removeObserver:bSelf];
[bSelf.previousKeyWindow makeKeyWindow];
[bSelf release], bSelf = nil;
}
}];
sharedView = nil;
}
#pragma mark - Getters
- (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)
return foundKeyboard.bounds.size.height;
return 0;
}
@end