forked from atomton/ATMHud
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathATMHud.m
470 lines (405 loc) · 10.4 KB
/
ATMHud.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
/*
* ATMHud.m
* ATMHud
*
* Created by Marcel Müller on 2011-03-01.
* Copyright (c) 2010-2011, Marcel Müller (atomcraft)
* Copyright (c) 2012-2014, David Hoerl
* All rights reserved.
*
* https://github.com/atomton/ATMHud (original)
*/
#import <QuartzCore/QuartzCore.h>
#ifdef ATM_SOUND
#import <AudioToolbox/AudioServices.h>
#endif
#import "ATMHud.h"
#import "ATMHudView.h"
#import "ATMHudDelegate.h"
#import "ATMHudQueueItem.h"
#ifdef ATM_SOUND
#import "ATMSoundFX.h"
#endif
#define VERSION @"atomHUD 4.0.0 • 2016-02-15"
#define SHADOW_OPACITY 0.4f
@interface ATMHud ()
@property (nonatomic, assign) NSUInteger queuePosition;
@property (nonatomic, assign) ATMHudAccessoryPosition accessoryPosition;
@property (nonatomic, assign) BOOL blockTouches;
@property (nonatomic, assign) BOOL allowSuperviewInteraction;
#ifdef ATM_SOUND
@property (nonatomic, copy) NSString *showSound;
@property (nonatomic, copy) NSString *updateSound;
@property (nonatomic, copy) NSString *hideSound;
#endif
@end
@implementation ATMHud
{
ATMHudView *hudView;
NSMutableArray *displayQueue;
NSDate *minShowDate;
}
+ (NSString *)version
{
return VERSION;
}
- (instancetype)init
{
if ((self = [super init])) {
_margin = 20.0f; // DFH: was 10
_padding = 10.0f;
_alpha = 0.95f; // DFH: originally 0.7
_gray = 0.1f; // DFH: originally 0.0
_animateDuration = 0.1f;
_progressStyle = ATMHudProgressStyleBar;
_progressBorderRadius = 4.0f; // DFH: was 8
_progressBorderWidth = 0.5; // DFH: was 2
_progressRadius = 2.0f; // DFH: was 5
_progressInset = 3.0f; // DFH: was 3
_appearScaleFactor = 0.8; // DFH: originally 1.4f
_disappearScaleFactor = 0.8; // DFH: originally 1.4f
_backgroundAlpha = 0.15;
_usesParallax = YES;
_hudBackgroundColor = [[UIColor alloc] initWithRed:.98 green:.99 blue:1.0 alpha:_alpha]; // DFH was [UIColor colorWithWhite:_hud.gray alpha:_hud.alpha]
#if 0 // these default to these
_minShowTime = 0;
_center = CGPointZero;
_blockTouches = NO;
_allowSuperviewInteraction = NO;
_removeViewWhenHidden = NO;
_shadowEnabled = NO;
_queuePosition = 0;
#endif
hudView = [[ATMHudView alloc] initWithFrame:CGRectZero andController:self];
hudView.autoresizingMask = (UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleBottomMargin |
UIViewAutoresizingFlexibleLeftMargin );
hudView.hudBackgroundColor = _hudBackgroundColor;
[hudView reset]; // actually sets many of our variables
}
return self;
}
- (instancetype)initWithDelegate:(id)hudDelegate
{
if ((self = [self init])) {
_delegate = hudDelegate;
}
return self;
}
- (void)dealloc
{
//NSLog(@"ATM HUD DEALLOC...");
[self unloadView];
//NSLog(@"...ATM HUD DEALLOC");
}
- (void)unloadView
{
assert([NSThread isMainThread]);
if(self.view.window != nil) {
//[hudView removeFromSuperview]; // TODO: this should be redundant, since its a child view of the ATMHud view"
[self.view removeFromSuperview];
}
}
- (void)loadView
{
UIView *base = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
base.backgroundColor = [UIColor colorWithWhite:_backgroundAlpha alpha:_backgroundAlpha];
base.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight);
base.userInteractionEnabled = NO;
[base addSubview:hudView];
self.view = base;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
#pragma mark -
#pragma mark Overrides
- (void)setAppearScaleFactor:(CGFloat)value
{
if (!isnormal(value)) {
value = 0.01f;
}
_appearScaleFactor = value;
}
- (void)setDisappearScaleFactor:(CGFloat)value
{
if (!isnormal(value)) {
value = 0.01f;
}
_disappearScaleFactor = value;
}
- (void)setAlpha:(CGFloat)value
{
_alpha = value;
[CATransaction begin];
[CATransaction setDisableActions:YES];
hudView.backgroundLayer.backgroundColor = [UIColor colorWithWhite:_gray alpha:value].CGColor;
[CATransaction commit];
}
- (void)setGray:(CGFloat)value
{
_gray = value;
[CATransaction begin];
[CATransaction setDisableActions:YES];
hudView.backgroundLayer.backgroundColor = [UIColor colorWithWhite:_gray alpha:_alpha].CGColor;
[CATransaction commit];
}
- (void)setCenter:(CGPoint)pt
{
_center = pt;
hudView.center = pt;
}
- (void)setShadowEnabled:(BOOL)value
{
_shadowEnabled = value;
hudView.layer.shadowOpacity = value ? SHADOW_OPACITY :0.0f;
}
- (void)setHudBackgroundColor:(UIColor *)color {
_hudBackgroundColor = color;
hudView.backgroundColor = _hudBackgroundColor;
}
- (NSString *)description
{
return [NSString stringWithFormat:@"HUD: caption=%@", hudView.caption];
}
#pragma mark -
#pragma mark Property forwards
- (void)setCaption:(NSString *)caption
{
hudView.caption = caption;
}
- (void)setImage:(UIImage *)image
{
hudView.image = image;
}
#if 1
- (void)setActivity:(BOOL)activity
{
hudView.showActivity = activity;
if (activity) {
[hudView.activity startAnimating];
hudView.activity.alpha = 0;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^
{
// Workaround for bug where Apple ignores the color property
[self changeColor];
} );
} else {
[hudView.activity stopAnimating];
}
}
- (void)changeColor
{
hudView.activity.color = [UIColor blackColor];
[UIView animateWithDuration:.250+_animateDuration animations: ^{ hudView.activity.alpha = 1; }];
}
#else // in case need to test some other better solution
- (void)setActivity:(BOOL)activity
{
hudView.showActivity = activity;
if (activity) {
assert([NSThread isMainThread]);
hudView.activity.color = [UIColor colorWithRed:252.0/255.0 green:113.0/255.0 blue:9.0/255.0 alpha:1.0];
[hudView.activity startAnimating];
} else {
[hudView.activity stopAnimating];
}
}
- (void)changeColor
{
NSLog(@"?????!!!");
}
#endif
- (void)setActivityStyle:(UIActivityIndicatorViewStyle)activityStyle
{
hudView.activityStyle = activityStyle;
hudView.activitySize = activityStyle == UIActivityIndicatorViewStyleWhiteLarge ? CGSizeMake(37, 37) : CGSizeMake(20, 20);
}
- (void)setFixedSize:(CGSize)fixedSize
{
hudView.fixedSize = fixedSize;
}
- (void)setProgress:(CGFloat)progress
{
if (progress < 0) progress = 0;
else
if (progress > 1.0f) progress = 1;
hudView.progress = progress;
}
#pragma mark -
#pragma mark Queue
- (void)addQueueItem:(ATMHudQueueItem *)item
{
if(!displayQueue) {
displayQueue = [NSMutableArray arrayWithCapacity:4];
}
[displayQueue addObject:item];
}
- (void)addQueueItems:(NSArray *)items
{
[displayQueue addObjectsFromArray:items];
}
- (void)clearQueue
{
[displayQueue removeAllObjects];
}
- (void)startQueueInView:(UIView *)view
{
_queuePosition = 0;
if (!CGSizeEqualToSize(hudView.fixedSize, CGSizeZero)) {
CGSize newSize = hudView.fixedSize;
CGSize targetSize;
ATMHudQueueItem *queueItem;
for (NSUInteger i = 0; i < [displayQueue count]; i++) {
queueItem = displayQueue[i];
targetSize = [hudView calculateSizeForQueueItem:queueItem];
if (targetSize.width > newSize.width) {
newSize.width = targetSize.width;
}
if (targetSize.height > newSize.height) {
newSize.height = targetSize.height;
}
}
[self setFixedSize:newSize];
}
[self showQueueAtIndex:_queuePosition inView:view];
}
- (void)showNextInQueue
{
_queuePosition++;
[self showQueueAtIndex:_queuePosition inView:nil];
}
- (void)showQueueAtIndex:(NSUInteger)index inView:view
{
if ([displayQueue count] > 0) {
_queuePosition = index;
if (_queuePosition == [displayQueue count]) {
[self hide];
return;
}
ATMHudQueueItem *item = displayQueue[_queuePosition];
hudView.caption = item.caption;
hudView.image = item.image;
BOOL flag = item.showActivity;
hudView.showActivity = flag;
if (flag) {
[hudView.activity startAnimating];
hudView.activity.alpha = 0;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^
{
// Workaround for bug where Apple ignores the color property
[self changeColor];
} );
} else {
[hudView.activity stopAnimating];
}
_accessoryPosition = item.accessoryPosition;
[self setActivityStyle:item.activityStyle];
if (_queuePosition == 0) {
[self showInView:view];
} else {
[self update];
}
}
}
#pragma mark -
#pragma mark Controlling
- (void)showInView:(UIView *)v
{
self.view.frame = v.bounds;
[v addSubview:self.view];
[self _show];
}
- (void)show
{
[self _show];
}
- (void)_show
{
[self updateHideTime];
[hudView show];
}
- (void)update
{
[self updateHideTime];
[hudView update];
}
- (void)updateHideTime
{
if (isnormal(_minShowTime)) {
//NSLog(@"NOW %@", [NSDate new]);
minShowDate = [NSDate dateWithTimeIntervalSinceNow:_minShowTime];
//NSLog(@"LATER %@", minShowDate);
} else {
minShowDate = nil; // just be sure
}
}
- (void)hide
{
_blockTouches = YES;
NSTimeInterval x = [minShowDate timeIntervalSinceDate:[NSDate date]]; // if minShowDate==nil then x==0
if (x <= 0 && hudView != nil) {
[hudView hide];
} else {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, x * NSEC_PER_SEC), dispatch_get_main_queue(), ^
{
[hudView hide];
} );
}
}
- (void)hideAfter:(NSTimeInterval)delay
{
_blockTouches = YES;
[self performSelector:@selector(hide) withObject:nil afterDelay:delay];
}
#pragma mark -
#pragma mark Internal methods
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if (!_blockTouches) {
_blockTouches = YES;
UITouch *aTouch = [touches anyObject];
if (aTouch.tapCount == 1) {
CGPoint p = [aTouch locationInView:self.view];
if (CGRectContainsPoint(hudView.frame, p)) {
if ([(id)_delegate respondsToSelector:@selector(userDidTapHud:)]) {
[_delegate userDidTapHud:self];
}
if (_blockDelegate) {
_blockDelegate(ATMHudActionUserDidTapHud, self);
}
} else {
if ([(id)_delegate respondsToSelector:@selector(userDidTapOutsideHud:)]) {
[_delegate userDidTapOutsideHud:self];
}
if (_blockDelegate) {
_blockDelegate(ATMHudActionUserDidTapOutsideHud, self);
}
}
}
_blockTouches = NO;
}
}
#ifdef ATM_SOUND
- (void)playSound:(NSString *)soundPath
{
_sound = [[ATMSoundFX alloc] initWithContentsOfFile:soundPath];
[_sound play];
}
#endif
@end