forked from progrmr/SDK_Utilities
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GMProgressHUD.m
210 lines (177 loc) · 6.45 KB
/
GMProgressHUD.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
//
// GMProgressHUD.m
// NextSprinter
//
// Created by Gary Morris on 7/28/11.
// Copyright 2011 Gary A. Morris. All rights reserved.
//
// This file is part of SDK_Utilities.repo
//
// This is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This file is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this file. If not, see <http://www.gnu.org/licenses/>.
//
#import "GMProgressHUD.h"
#import <QuartzCore/QuartzCore.h>
#import "Utilities.h"
#import "UtilitiesUI.h"
@implementation GMProgressHUD
@dynamic visible;
@dynamic progress;
@dynamic message;
#define kFadeInOutDuration (0.3)
#define kDisplayAlpha (0.6)
#define kSpacer (12)
#define kProgressBarWidth (180)
#define kProgressBarHeight (20)
#define kActivityIndicatorWidth (20)
#define kActivityIndicatorHeight (20)
-(void)dealloc
{
[msgLabel release];
[progressView release];
[activityView release];
[super dealloc];
}
-(CGSize)sizeThatFits:(CGSize)oldSize
{
// size the msgView frame
CGSize size = CGSizeMake(kSpacer*2, kSpacer*2); // start with spacers
if (activityView) {
size.width += kSpacer + MAX(messageSize.width, kActivityIndicatorWidth);
size.height += kSpacer + messageSize.height + kActivityIndicatorHeight;
} else if (progressView) {
size.width += kSpacer + MAX(messageSize.width, kProgressBarWidth);
size.height += kSpacer + messageSize.height + kProgressBarHeight;
} else {
size.width += messageSize.width;
size.height += messageSize.height;
}
return size;
}
-(void)layoutSubviews
{
CGSize viewSize = self.bounds.size;
msgLabel.frame = CGRectMake((viewSize.width-messageSize.width)/2, kSpacer, messageSize.width, messageSize.height);
if (activityView) {
activityView.frame = CGRectMake((viewSize.width-kActivityIndicatorWidth)/2,
(viewSize.height-kActivityIndicatorHeight)-kSpacer,
kActivityIndicatorWidth,
kActivityIndicatorHeight);
} else if (progressView) {
progressView.frame = CGRectMake((viewSize.width-kProgressBarWidth)/2,
(viewSize.height-kProgressBarHeight)-kSpacer,
kProgressBarWidth,
kProgressBarHeight);
}
}
-(id)initWithMessage:(NSString*)newMessage showProgress:(BOOL)useProgressView
{
self = [super initWithFrame:CGRectZero];
if (self) {
self.backgroundColor = [UIColor colorWithWhite:0 alpha:kDisplayAlpha];
self.autoresizesSubviews = YES;
self.layer.cornerRadius = 10;
// create the label for the message
msgLabel = [[UILabel alloc] initWithFrame:CGRectZero];
msgLabel.backgroundColor = [UIColor clearColor];
msgLabel.textColor = [UIColor whiteColor];
msgLabel.font = [UIFont systemFontOfSize:14];
msgLabel.textAlignment = NSTextAlignmentCenter;
msgLabel.clipsToBounds = NO;
[self addSubview:msgLabel];
if (useProgressView) {
// create the progress bar view
progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(kSpacer, kSpacer,
kProgressBarWidth,
kProgressBarHeight)];
[self addSubview:progressView];
} else {
// create the activity indicator view
activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[self addSubview:activityView];
}
// set the message, which resizes to fit the text
self.message = newMessage;
}
return self;
}
-(void)show
{
// add view centered in the window (but up a little from center)
UIWindow* window = [UIApplication sharedApplication].keyWindow;
CGRect wBounds = window.bounds;
self.center = CGPointMake(CGRectGetMidX(wBounds), CGRectGetMidY(wBounds) * 0.7f);
[window addSubview:self];
if (activityView) {
[activityView startAnimating];
}
// animate the alpha from 0 to kDisplayAlpha to fade in
self.alpha = 0;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:kFadeInOutDuration];
[UIView setAnimationDelegate:nil];
self.alpha = 1;
[UIView commitAnimations];
}
+(GMProgressHUD*)showHUDWithMessage:(NSString*)newMessage showProgress:(BOOL)useProgressView
{
GMProgressHUD* hud = [[[GMProgressHUD alloc] initWithMessage:newMessage
showProgress:useProgressView] autorelease];
[hud show];
return hud;
}
-(void)animationFinished:(NSString*)animationID finished:(BOOL)finished context:(void*)context
{
[self removeFromSuperview];
}
-(void)dismiss
{
if (activityView) {
[activityView stopAnimating];
}
// remove hud view from the window
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:kFadeInOutDuration];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationFinished:finished:context:)];
self.alpha = 0;
[UIView commitAnimations];
}
-(void)setMessage:(NSString *)newMessage
{
msgLabel.text = newMessage;
messageSize = [newMessage sizeWithFont:msgLabel.font];
// resize the view to fit
CGRect bounds = self.bounds;
bounds.size = [self sizeThatFits:bounds.size];
self.bounds = bounds;
[self setNeedsLayout];
DLog(@"msg=\"%@\", size=%@, bounds=%@", newMessage, NSStringFromCGSize(messageSize), NSStringFromCGSize(bounds.size));
}
-(NSString*)message
{
return msgLabel.text;
}
-(void)setProgress:(float)newProgress
{
progressView.progress = newProgress;
}
-(float)progress
{
return progressView.progress;
}
-(BOOL)isVisible
{
return self.window != nil;
}
@end