forked from zero2launch/z2l-ios-social-network
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgressHUD.m
executable file
·352 lines (322 loc) · 17.7 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
//
// Copyright (c) 2014 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"
@implementation ProgressHUD
@synthesize interaction, window, background, hud, spinner, image, label;
//-------------------------------------------------------------------------------------------------------------------------------------------------
+ (ProgressHUD *)shared
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
static dispatch_once_t once = 0;
static ProgressHUD *progressHUD;
//---------------------------------------------------------------------------------------------------------------------------------------------
dispatch_once(&once, ^{ progressHUD = [[ProgressHUD alloc] init]; });
//---------------------------------------------------------------------------------------------------------------------------------------------
return progressHUD;
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
+ (void)dismiss
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
[[self shared] hudHide];
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
+ (void)show:(NSString *)status
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
[self shared].interaction = YES;
[[self shared] hudMake:status image:nil spin:YES hide:NO];
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
+ (void)show:(NSString *)status Interaction:(BOOL)Interaction
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
[self shared].interaction = Interaction;
[[self shared] hudMake:status image:nil spin:YES hide:NO];
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
+ (void)showSuccess:(NSString *)status
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
[self shared].interaction = YES;
[[self shared] hudMake:status image:HUD_IMAGE_SUCCESS spin:NO hide:YES];
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
+ (void)showSuccess:(NSString *)status Interaction:(BOOL)Interaction
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
[self shared].interaction = Interaction;
[[self shared] hudMake:status image:HUD_IMAGE_SUCCESS spin:NO hide:YES];
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
+ (void)showError:(NSString *)status
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
[self shared].interaction = YES;
[[self shared] hudMake:status image:HUD_IMAGE_ERROR spin:NO hide:YES];
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
+ (void)showError:(NSString *)status Interaction:(BOOL)Interaction
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
[self shared].interaction = Interaction;
[[self shared] hudMake:status image:HUD_IMAGE_ERROR spin:NO hide:YES];
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (id)init
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
self = [super initWithFrame:[[UIScreen mainScreen] bounds]];
//---------------------------------------------------------------------------------------------------------------------------------------------
id<UIApplicationDelegate> delegate = [[UIApplication sharedApplication] delegate];
//---------------------------------------------------------------------------------------------------------------------------------------------
if ([delegate respondsToSelector:@selector(window)])
window = [delegate performSelector:@selector(window)];
else window = [[UIApplication sharedApplication] keyWindow];
//---------------------------------------------------------------------------------------------------------------------------------------------
background = nil; hud = nil; spinner = nil; image = nil; label = nil;
//---------------------------------------------------------------------------------------------------------------------------------------------
self.alpha = 0;
//---------------------------------------------------------------------------------------------------------------------------------------------
return self;
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)hudMake:(NSString *)status image:(UIImage *)img spin:(BOOL)spin hide:(BOOL)hide
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
[self hudCreate];
//---------------------------------------------------------------------------------------------------------------------------------------------
label.text = status;
label.hidden = (status == nil) ? YES : NO;
//---------------------------------------------------------------------------------------------------------------------------------------------
image.image = img;
image.hidden = (img == nil) ? YES : NO;
//---------------------------------------------------------------------------------------------------------------------------------------------
if (spin) [spinner startAnimating]; else [spinner stopAnimating];
//---------------------------------------------------------------------------------------------------------------------------------------------
[self hudSize];
[self hudPosition:nil];
[self hudShow];
//---------------------------------------------------------------------------------------------------------------------------------------------
if (hide) [NSThread detachNewThreadSelector:@selector(timedHide) toTarget:self withObject:nil];
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)hudCreate
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
if (hud == nil)
{
hud = [[UIToolbar alloc] initWithFrame:CGRectZero];
hud.translucent = YES;
hud.backgroundColor = HUD_BACKGROUND_COLOR;
hud.layer.cornerRadius = 10;
hud.layer.masksToBounds = YES;
[self registerNotifications];
}
//---------------------------------------------------------------------------------------------------------------------------------------------
if (hud.superview == nil)
{
if (interaction == NO)
{
background = [[UIView alloc] initWithFrame:window.frame];
background.backgroundColor = HUD_WINDOW_COLOR;
[window addSubview:background];
[background addSubview:hud];
}
else [window addSubview:hud];
}
//---------------------------------------------------------------------------------------------------------------------------------------------
if (spinner == nil)
{
spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
spinner.color = HUD_SPINNER_COLOR;
spinner.hidesWhenStopped = YES;
}
if (spinner.superview == nil) [hud addSubview:spinner];
//---------------------------------------------------------------------------------------------------------------------------------------------
if (image == nil)
{
image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 28, 28)];
}
if (image.superview == nil) [hud addSubview:image];
//---------------------------------------------------------------------------------------------------------------------------------------------
if (label == nil)
{
label = [[UILabel alloc] initWithFrame:CGRectZero];
label.font = HUD_STATUS_FONT;
label.textColor = HUD_STATUS_COLOR;
label.backgroundColor = [UIColor clearColor];
label.textAlignment = NSTextAlignmentCenter;
label.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
label.numberOfLines = 0;
}
if (label.superview == nil) [hud addSubview:label];
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (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];
//---------------------------------------------------------------------------------------------------------------------------------------------
[label removeFromSuperview]; label = nil;
[image removeFromSuperview]; image = nil;
[spinner removeFromSuperview]; spinner = nil;
[hud removeFromSuperview]; hud = nil;
[background removeFromSuperview]; background = nil;
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)hudSize
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
CGRect labelRect = CGRectZero;
CGFloat hudWidth = 100, hudHeight = 100;
//---------------------------------------------------------------------------------------------------------------------------------------------
if (label.text != nil)
{
NSDictionary *attributes = @{NSFontAttributeName:label.font};
NSInteger options = NSStringDrawingUsesFontLeading | NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin;
labelRect = [label.text boundingRectWithSize:CGSizeMake(200, 300) options:options attributes:attributes context:NULL];
labelRect.origin.x = 12;
labelRect.origin.y = 66;
hudWidth = labelRect.size.width + 24;
hudHeight = labelRect.size.height + 80;
if (hudWidth < 100)
{
hudWidth = 100;
labelRect.origin.x = 0;
labelRect.size.width = 100;
}
}
//---------------------------------------------------------------------------------------------------------------------------------------------
hud.bounds = CGRectMake(0, 0, hudWidth, hudHeight);
//---------------------------------------------------------------------------------------------------------------------------------------------
CGFloat imagex = hudWidth/2;
CGFloat imagey = (label.text == nil) ? hudHeight/2 : 36;
image.center = spinner.center = CGPointMake(imagex, imagey);
//---------------------------------------------------------------------------------------------------------------------------------------------
label.frame = labelRect;
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (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:^{
hud.center = CGPointMake(center.x, center.y);
} completion:nil];
//---------------------------------------------------------------------------------------------------------------------------------------------
if (background != nil) background.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;
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)hudShow
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
if (self.alpha == 0)
{
self.alpha = 1;
hud.alpha = 0;
hud.transform = CGAffineTransformScale(hud.transform, 1.4, 1.4);
NSUInteger options = UIViewAnimationOptionAllowUserInteraction | UIViewAnimationCurveEaseOut;
[UIView animateWithDuration:0.15 delay:0 options:options animations:^{
hud.transform = CGAffineTransformScale(hud.transform, 1/1.4, 1/1.4);
hud.alpha = 1;
} completion:nil];
}
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)hudHide
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
if (self.alpha == 1)
{
NSUInteger options = UIViewAnimationOptionAllowUserInteraction | UIViewAnimationCurveEaseIn;
[UIView animateWithDuration:0.15 delay:0 options:options animations:^{
hud.transform = CGAffineTransformScale(hud.transform, 0.7, 0.7);
hud.alpha = 0;
}
completion:^(BOOL finished) {
[self hudDestroy];
self.alpha = 0;
}];
}
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)timedHide
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
@autoreleasepool
{
double length = label.text.length;
NSTimeInterval sleep = length * 0.04 + 0.5;
[NSThread sleepForTimeInterval:sleep];
dispatch_async(dispatch_get_main_queue(), ^{
[self hudHide];
});
}
}
@end