forked from mwkoelb/MWKProgressIndicator
-
Notifications
You must be signed in to change notification settings - Fork 2
/
MWKProgressIndicator.m
233 lines (185 loc) · 7.36 KB
/
MWKProgressIndicator.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
//
// MWKProgressIndicator.m
// routemev1
//
// Created by Max Kölb on 28/03/14.
// Copyright (c) 2014 Max Kölb. All rights reserved.
//
#import "MWKProgressIndicator.h"
#import <AVFoundation/AVFoundation.h>
#define MWKProgressIndicatorHeight 64.0f
@implementation MWKProgressIndicator
{
float _progress;
UIView *_progressTrack;
UILabel *_titleLabel;
BOOL _lock;
}
+ (instancetype)sharedIndicator
{
static MWKProgressIndicator *indicator = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken,^
{
indicator = [MWKProgressIndicator new];
});
return indicator;
}
- (id)init
{
self = [super init];
if (!self) return nil;
self.backgroundColor = [UIColor whiteColor];
self.frame = CGRectMake(0, -MWKProgressIndicatorHeight, [UIScreen mainScreen].bounds.size.width, MWKProgressIndicatorHeight);
[[[[[UIApplication sharedApplication] keyWindow] subviews] firstObject] addSubview:self];
_progress = 0.0;
_progressTrack = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, self.bounds.size.height)];
[_progressTrack setBackgroundColor:[UIColor colorWithRed:0.53 green:0.82 blue:1 alpha:1]];
[self addSubview:_progressTrack];
float statusBarHeight = 14.0f;
_titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, statusBarHeight, self.bounds.size.width, MWKProgressIndicatorHeight - statusBarHeight)];
[_titleLabel setBackgroundColor:[UIColor clearColor]];
[_titleLabel setTextColor:[UIColor blackColor]];
[_titleLabel setTextAlignment:NSTextAlignmentCenter];
[_titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue-Thin" size:20]];
[_titleLabel setText:@"Loading..."];
[self addSubview:_titleLabel];
_lock = NO;
return self;
}
+ (void)show
{
MWKProgressIndicator *indicator = [MWKProgressIndicator sharedIndicator];
if (CGRectGetMinY(indicator.frame) == 0.0) return;
[indicator setTopLocationValue:0];
}
+ (void)dismiss
{
MWKProgressIndicator *indicator = [MWKProgressIndicator sharedIndicator];
[indicator dismissAnimated:YES];
}
+ (void)dismissWithoutAnimation
{
MWKProgressIndicator *indicator = [MWKProgressIndicator sharedIndicator];
[indicator dismissAnimated:NO];
}
- (void)dismissAnimated:(BOOL)animated
{
if (CGRectGetMinY(self.frame) == -MWKProgressIndicatorHeight) return;
if (animated)
{
[self updateProgress:0.0];
[self setTopLocationValue:-MWKProgressIndicatorHeight];
}
else
{
_progressTrack.frame = CGRectMake(0, 0, 0, self.frame.size.height);
[self setTopLocationValue:-MWKProgressIndicatorHeight withDuration:0.0];
}
}
- (void)setTopLocationValue:(float)value
{
[self setTopLocationValue:value withDuration:0.4];
}
- (void)setTopLocationValue:(float)value withDuration:(float)duration
{
[UIView animateWithDuration:duration delay:0.0 usingSpringWithDamping:0.7 initialSpringVelocity:1.0 options:UIViewAnimationOptionCurveEaseOut animations:^
{
[self setFrame:CGRectMake(0, value, self.bounds.size.width, self.bounds.size.height)];
}
completion:nil];
}
+ (void)updateProgress:(float)progress
{
MWKProgressIndicator *indicator = [MWKProgressIndicator sharedIndicator];
[indicator updateProgress:progress];
}
- (void)updateProgress:(float)progress
{
if (0.0f <= progress && progress < 1.1f)
{
float units = self.frame.size.width / 1;
_progress = progress;
[UIView animateWithDuration:0.5 delay:0.0 usingSpringWithDamping:0.8 initialSpringVelocity:1.0 options:UIViewAnimationOptionCurveEaseInOut animations:^
{
_progressTrack.frame = CGRectMake(0, 0, units * progress, self.bounds.size.height);
}
completion:nil];
}
}
+ (void)updateMessage:(NSString *)message
{
[MWKProgressIndicator updateMessage:message type:MWKProgressMessageUpdateTypeText];
}
+ (void)updateMessage:(NSString *)message type:(MWKProgressMessageUpdateType)type
{
dispatch_async(dispatch_get_main_queue(), ^
{
if (type == MWKProgressMessageUpdateTypeAll || type == MWKProgressMessageUpdateTypeText)
{
MWKProgressIndicator *indicator = [MWKProgressIndicator sharedIndicator];
[indicator updateMessage:message];
}
if (type == MWKProgressMessageUpdateTypeAll || type == MWKProgressMessageUpdateTypeVoice)
{
[MWKProgressIndicator speakMessage:message];
}
});
}
- (void)updateMessage:(NSString *)message
{
dispatch_async(dispatch_get_main_queue(), ^
{
_titleLabel.text = message;
});
}
+ (void)showErrorMessage:(NSString *)errorMessage
{
[[MWKProgressIndicator sharedIndicator] showWithColor:[UIColor redColor] duration:2 message:errorMessage];
}
+ (void)showSuccessMessage:(NSString *)successMessage
{
[[MWKProgressIndicator sharedIndicator] showWithColor:[UIColor greenColor] duration:2 message:successMessage];
}
- (void)showWithColor:(UIColor *)color duration:(float)duration message:(NSString *)message
{
if (_lock) return;
_lock = YES;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
float hideDuration = 0.5;
[self setTopLocationValue:-MWKProgressIndicatorHeight withDuration:hideDuration];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(hideDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^
{
[self updateProgress:0.0];
[self setTopLocationValue:0 withDuration:hideDuration];
self.backgroundColor = color;
[self updateMessage:message];
});
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)((duration+hideDuration) * NSEC_PER_SEC)), dispatch_get_main_queue(),^
{
[self setTopLocationValue:-MWKProgressIndicatorHeight withDuration:hideDuration];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(hideDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^
{
self.backgroundColor = [UIColor whiteColor];
_lock = NO;
});
});
});
}
- (void)updateTrackColor:(UIColor *)color
{
_progressTrack.backgroundColor = color;
}
+ (void)speakMessage:(NSString *)message
{
static AVSpeechSynthesizer *synthesizer = nil;
if (!synthesizer)
{
synthesizer = [AVSpeechSynthesizer new];
}
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:message];
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"];
utterance.rate = 0.3;
[synthesizer speakUtterance:utterance];
}
@end