forked from atomton/ATMHud
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathATMHudView.m
717 lines (619 loc) · 24.7 KB
/
ATMHudView.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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
/*
* ATMHudView.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>
#import "ATMHudView.h"
#import "ATMTextLayer.h"
#import "ATMProgressLayer.h"
#import "ATMHud.h"
#import "ATMHudDelegate.h"
#import "ATMHudQueueItem.h"
//@interface ATMHudView ()
//@end
#define SHADOW_OPACITY 0.4f
#define CORNER_RADIUS 10
@implementation ATMHudView
{
CGRect targetBounds;
CALayer *imageLayer;
ATMTextLayer *captionLayer;
ATMProgressLayer *progressLayer;
CGRect captionRect;
CGRect activityRect;
CGRect progressRect;
CGRect imageRect;
BOOL didHide;
UIFont *bsf14;
}
- (CGPoint)integralPoint:(CGPoint)point
{
CGPoint _p = point;
_p.x = rintf((float)_p.x);
_p.y = rintf((float)_p.y);
return _p;
}
- (void)removeFromSuperview
{
//NSLog(@"removeFromSuperview");
[super removeFromSuperview];
}
- (instancetype)initWithFrame:(CGRect)frame andController:(ATMHud *)h
{
if ((self = [super initWithFrame:frame])) {
_hud = h;
self.backgroundColor = [UIColor clearColor];
self.opaque = NO;
self.alpha = 0.0;
bsf14 = [UIFont preferredFontForTextStyle:UIFontTextStyleSubheadline];
_backgroundLayer = [CALayer new];
_backgroundLayer.cornerRadius = CORNER_RADIUS;
_backgroundLayer.backgroundColor = _hudBackgroundColor.CGColor;
[self.layer addSublayer:_backgroundLayer];
captionLayer = [ATMTextLayer new];
captionLayer.contentsScale = [[UIScreen mainScreen] scale];
captionLayer.anchorPoint = CGPointMake(0, 0);
[self.layer addSublayer:captionLayer];
imageLayer = [CALayer new];
imageLayer.anchorPoint = CGPointMake(0, 0);
[self.layer addSublayer:imageLayer];
progressLayer = [ATMProgressLayer new];
progressLayer.contentsScale = [[UIScreen mainScreen] scale];
progressLayer.anchorPoint = CGPointMake(0, 0);
[self.layer addSublayer:progressLayer];
_activityStyle = UIActivityIndicatorViewStyleWhiteLarge;
_activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: _activityStyle]; // UIActivityIndicatorView
_activity.hidesWhenStopped = YES;
[self addSubview:_activity];
// Remove shadow - its just an old school effect
// self.layer.shadowColor = [UIColor blackColor].CGColor;
// self.layer.shadowRadius = 5.0; // DFH: was 8
// self.layer.shadowOffset = CGSizeMake(0.0, 0.0); // DFH: was 3
// self.layer.shadowOpacity = 0.05f; // DFH: was 0.3
_activitySize = CGSizeMake(20, 20);
didHide = YES;
if(_hud.usesParallax) {
// Set vertical effect
UIInterpolatingMotionEffect *verticalMotionEffect =
[[UIInterpolatingMotionEffect alloc]
initWithKeyPath:@"center.y"
type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
verticalMotionEffect.minimumRelativeValue = @(-10);
verticalMotionEffect.maximumRelativeValue = @(10);
// Set horizontal effect
UIInterpolatingMotionEffect *horizontalMotionEffect =
[[UIInterpolatingMotionEffect alloc]
initWithKeyPath:@"center.x"
type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
horizontalMotionEffect.minimumRelativeValue = @(-10);
horizontalMotionEffect.maximumRelativeValue = @(10);
// Create group to combine both
UIMotionEffectGroup *group = [UIMotionEffectGroup new];
group.motionEffects = @[horizontalMotionEffect, verticalMotionEffect];
// Add both effects to your view
[self addMotionEffect:group];
}
}
return self;
}
- (void)dealloc
{
//NSLog(@"ATM_HUD_VIEW DEALLOC");
}
- (void)setProgress:(CGFloat)_p
{
_p = MIN(MAX(0,_p),1);
if (_p > 0 && _p < 0.08f) _p = 0.08f;
if (_p == _progress) return;
_progress = _p;
progressLayer.theProgress = _progress;
}
- (void)setHudBackgroundColor:(UIColor *)color {
_hudBackgroundColor = color;
_backgroundLayer.backgroundColor = color.CGColor;
}
- (CGRect)calcString:(NSString *)str sizeForSize:(CGSize)origSize
{
NSStringDrawingContext *sdc = [NSStringDrawingContext new];
sdc.minimumScaleFactor = 0;
NSParagraphStyle *paragraphStyle = [NSMutableParagraphStyle defaultParagraphStyle]; // uses LineBreakWordWrapping
NSDictionary *dict = @{ NSFontAttributeName : bsf14, NSParagraphStyleAttributeName : paragraphStyle };
CGRect r = [_caption boundingRectWithSize:origSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:sdc];
return CGRectIntegral(r);
}
- (void)calculate
{
progressLayer.progressStyle = _hud.progressStyle;
progressRect.size = progressLayer.progressSize;
if (![_caption length]) {
activityRect = CGRectMake(_hud.margin, _hud.margin, _activitySize.width, _activitySize.height);
targetBounds = CGRectMake(0, 0, _hud.margin*2+_activitySize.width, _hud.margin*2+_activitySize.height);
} else {
BOOL hasFixedSize = NO;
captionRect = [self calcString:_caption sizeForSize:CGSizeMake(160, 200)];
if (_fixedSize.width > 0 & _fixedSize.height > 0) {
CGSize s = _fixedSize;
if (_progress > 0 && (_fixedSize.width < progressRect.size.width+_hud.margin*2)) {
s.width = progressRect.size.width+_hud.margin*2;
}
hasFixedSize = YES;
captionRect = [self calcString:_caption sizeForSize:CGSizeMake(s.width-_hud.margin*2, 200)];
targetBounds = CGRectMake(0, 0, s.width, s.height);
}
float adjustment = 0;
CGFloat marginX = _hud.margin;
CGFloat marginY = _hud.margin;
if (!hasFixedSize) {
if (_hud.accessoryPosition == ATMHudAccessoryPositionTop || _hud.accessoryPosition == ATMHudAccessoryPositionBottom) {
if (_progress > 0) {
adjustment = _hud.padding+progressRect.size.height;
if (captionRect.size.width+_hud.margin*2 < progressRect.size.width) {
captionRect = [self calcString:_caption sizeForSize:CGSizeMake(progressRect.size.width, 200)];
targetBounds = CGRectMake(0, 0, progressRect.size.width+_hud.margin*2, captionRect.size.height+_hud.margin*2+adjustment);
} else {
targetBounds = CGRectMake(0, 0, captionRect.size.width+_hud.margin*2, captionRect.size.height+_hud.margin*2+adjustment);
}
} else {
if (_image) {
adjustment = _hud.padding+_image.size.height;
} else if (_showActivity) {
adjustment = _hud.padding+_activitySize.height;
}
targetBounds = CGRectMake(0, 0, captionRect.size.width+_hud.margin*2, captionRect.size.height+_hud.margin*2+adjustment);
}
} else if (_hud.accessoryPosition == ATMHudAccessoryPositionLeft || _hud.accessoryPosition == ATMHudAccessoryPositionRight) {
if (_image) {
adjustment = _hud.padding+_image.size.width;
} else if (_showActivity) {
adjustment = _hud.padding+_activitySize.height;
}
targetBounds = CGRectMake(0, 0, captionRect.size.width+_hud.margin*2+adjustment, captionRect.size.height+_hud.margin*2);
}
} else {
if (_hud.accessoryPosition == ATMHudAccessoryPositionTop || _hud.accessoryPosition == ATMHudAccessoryPositionBottom) {
if (_progress > 0) {
adjustment = _hud.padding+progressRect.size.height;
if (captionRect.size.width+_hud.margin*2 < progressRect.size.width) {
captionRect = [self calcString:_caption sizeForSize:CGSizeMake(progressRect.size.width, 200)];
}
} else {
if (_image) {
adjustment = _hud.padding+_image.size.height;
} else if (_showActivity) {
adjustment = _hud.padding+_activitySize.height;
}
}
long deltaWidth = lrintf(targetBounds.size.width - captionRect.size.width);
marginX = 0.5f*deltaWidth;
if (marginX < _hud.margin) {
captionRect = [self calcString:_caption sizeForSize:CGSizeMake(160, 200)];
targetBounds = CGRectMake(0, 0, captionRect.size.width+2*_hud.margin, targetBounds.size.height);
marginX = _hud.margin;
}
long deltaHeight = lrintf(targetBounds.size.height - (adjustment+captionRect.size.height));
marginY = 0.5f*deltaHeight;
if (marginY < _hud.margin) {
targetBounds = CGRectMake(0, 0, targetBounds.size.width, captionRect.size.height+2*_hud.margin+adjustment);
marginY = _hud.margin;
}
} else if (_hud.accessoryPosition == ATMHudAccessoryPositionLeft || _hud.accessoryPosition == ATMHudAccessoryPositionRight) {
if (_image) {
adjustment = _hud.padding+_image.size.width;
} else if (_showActivity) {
adjustment = _hud.padding+_activitySize.width;
}
long deltaWidth = lrintf(targetBounds.size.width-(adjustment+captionRect.size.width));
marginX = 0.5f*deltaWidth;
if (marginX < _hud.margin) {
captionRect = [self calcString:_caption sizeForSize:CGSizeMake(160, 200)];
targetBounds = CGRectMake(0, 0, adjustment+captionRect.size.width+2*_hud.margin, targetBounds.size.height);
marginX = _hud.margin;
}
long deltaHeight = lrintf(targetBounds.size.height-captionRect.size.height);
marginY = 0.5f*deltaHeight;
if (marginY < _hud.margin) {
targetBounds = CGRectMake(0, 0, targetBounds.size.width, captionRect.size.height+2*_hud.margin);
marginY = _hud.margin;
}
}
}
switch (_hud.accessoryPosition) {
case ATMHudAccessoryPositionTop: {
activityRect = CGRectMake((targetBounds.size.width-_activitySize.width)*0.5f, marginY, _activitySize.width, _activitySize.height);
imageRect = CGRectZero;
if (_image)
imageRect.origin.x = (targetBounds.size.width-_image.size.width)*0.5f;
else
imageRect.origin.x = (targetBounds.size.width)*0.5f;
imageRect.origin.y = marginY;
if (_image && _image.size.width > 0.0f && _image.size.height > 0.0f) {
imageRect.size = _image.size;
}
//progressRect = CGRectMake((targetBounds.size.width-progressRect.size.width)*0.5f, marginY, progressRect.size.width, progressRect.size.height);
progressRect.origin = CGPointMake((targetBounds.size.width-progressRect.size.width)*0.5f, marginY);
captionRect.origin.x = (targetBounds.size.width-captionRect.size.width)*0.5f;
captionRect.origin.y = adjustment+marginY;
break;
}
case ATMHudAccessoryPositionRight: {
activityRect = CGRectMake(marginX+_hud.padding+captionRect.size.width, (targetBounds.size.height-_activitySize.height)*0.5f, _activitySize.width, _activitySize.height);
imageRect = CGRectZero;
imageRect.origin.x = marginX+_hud.padding+captionRect.size.width;
if (_image) {
imageRect.origin.y = (targetBounds.size.height-_image.size.height)*0.5f;
imageRect.size = _image.size;
}
captionRect.origin.x = marginX;
captionRect.origin.y = marginY;
break;
}
case ATMHudAccessoryPositionBottom: {
activityRect = CGRectMake((targetBounds.size.width-_activitySize.width)*0.5f, captionRect.size.height+marginY+_hud.padding, _activitySize.width, _activitySize.height);
imageRect = CGRectZero;
if (_image)
imageRect.origin.x = (targetBounds.size.width-_image.size.width)*0.5f;
else
imageRect.origin.x = (targetBounds.size.width)*0.5f;
imageRect.origin.y = captionRect.size.height+marginY+_hud.padding;
if (_image)
imageRect.size = _image.size;
//progressRect = CGRectMake((targetBounds.size.width-progressRect.size.width)*0.5f, captionRect.size.height+marginY+_hud.padding, progressRect.size.width, progressRect.size.height);
progressRect.origin = CGPointMake((targetBounds.size.width-progressRect.size.width)*0.5f, captionRect.size.height+marginY+_hud.padding);
captionRect.origin.x = (targetBounds.size.width-captionRect.size.width)*0.5f;
captionRect.origin.y = marginY;
break;
}
case ATMHudAccessoryPositionLeft: {
activityRect = CGRectMake(marginX, (targetBounds.size.height-_activitySize.height)*0.5f, _activitySize.width, _activitySize.height);
imageRect = CGRectZero;
imageRect.origin.x = marginX;
if (_image) {
imageRect.origin.y = (targetBounds.size.height-_image.size.height)*0.5f;
imageRect.size = _image.size;
} else {
imageRect.origin.y = (targetBounds.size.height)*0.5f;
}
captionRect.origin.x = marginX+adjustment;
captionRect.origin.y = marginY;
break;
}
}
}
}
- (CGSize)sizeForActivityStyle:(UIActivityIndicatorViewStyle)style
{
CGSize size;
if (style == UIActivityIndicatorViewStyleWhiteLarge) {
size = CGSizeMake(37, 37);
} else {
size = CGSizeMake(20, 20);
}
return size;
}
- (CGSize)calculateSizeForQueueItem:(ATMHudQueueItem *)item
{
CGSize targetSize = CGSizeZero;
CGSize styleSize = [self sizeForActivityStyle:item.activityStyle];
if (!item.caption || [item.caption isEqualToString:@""]) {
targetSize = CGSizeMake(_hud.margin*2+styleSize.width, _hud.margin*2+styleSize.height);
} else {
BOOL hasFixedSize = NO;
captionRect = [self calcString:item.caption sizeForSize:CGSizeMake(160, 200)];
float adjustment = 0;
CGFloat marginX = 0;
CGFloat marginY = 0;
if (!hasFixedSize) {
if (item.accessoryPosition == ATMHudAccessoryPositionTop || item.accessoryPosition == ATMHudAccessoryPositionBottom) {
if (item.image) {
adjustment = _hud.padding+item.image.size.height;
} else if (item.showActivity) {
adjustment = _hud.padding+styleSize.height;
}
targetSize = CGSizeMake(captionRect.size.width+_hud.margin*2, captionRect.size.height+_hud.margin*2+adjustment);
} else if (item.accessoryPosition == ATMHudAccessoryPositionLeft || item.accessoryPosition == ATMHudAccessoryPositionRight) {
if (item.image) {
adjustment = _hud.padding+item.image.size.width;
} else if (item.showActivity) {
adjustment = _hud.padding+styleSize.width;
}
targetSize = CGSizeMake(captionRect.size.width+_hud.margin*2+adjustment, captionRect.size.height+_hud.margin*2);
}
} else {
if (item.accessoryPosition == ATMHudAccessoryPositionTop || item.accessoryPosition == ATMHudAccessoryPositionBottom) {
if (item.image) {
adjustment = _hud.padding+item.image.size.height;
} else if (item.showActivity) {
adjustment = _hud.padding+styleSize.height;
}
long deltaWidth = lrintf(targetSize.width-captionRect.size.width);
marginX = 0.5f*deltaWidth;
if (marginX < _hud.margin) {
captionRect = [self calcString:_caption sizeForSize:CGSizeMake(160, 200)];
targetSize = CGSizeMake(captionRect.size.width+2*_hud.margin, targetSize.height);
}
long deltaHeight = lrintf(targetSize.height-(adjustment+captionRect.size.height));
marginY = 0.5f*deltaHeight;
if (marginY < _hud.margin) {
targetSize = CGSizeMake(targetSize.width, captionRect.size.height+2*_hud.margin+adjustment);
}
} else if (item.accessoryPosition == ATMHudAccessoryPositionLeft || item.accessoryPosition == ATMHudAccessoryPositionRight) {
if (item.image) {
adjustment = _hud.padding+item.image.size.width;
} else if (item.showActivity) {
adjustment = _hud.padding+styleSize.width;
}
long deltaWidth = lrintf(targetSize.width-(adjustment+captionRect.size.width));
marginX = 0.5f*deltaWidth;
if (marginX < _hud.margin) {
captionRect = [self calcString:_caption sizeForSize:CGSizeMake(160, 200)];
targetSize = CGSizeMake(adjustment+captionRect.size.width+2*_hud.margin, targetSize.height);
}
long deltaHeight = lrintf(targetSize.height-captionRect.size.height);
marginY = 0.5f*deltaHeight;
if (marginY < _hud.margin) {
targetSize = CGSizeMake(targetSize.width, captionRect.size.height+2*_hud.margin);
}
}
}
}
return targetSize;
}
- (void)applyWithMode:(ATMHudApplyMode)mode
{
id delegate = (id)_hud.delegate;
ATMblockDelegate blockDelegate = _hud.blockDelegate;
switch (mode) {
case ATMHudApplyModeShow: {
// NSLog(@"ATMHud: ATMHudApplyModeShow delegate=%@", delegate);
if (CGPointEqualToPoint(_hud.center, CGPointZero)) {
self.frame = CGRectMake((self.superview.bounds.size.width-targetBounds.size.width)*0.5f, (self.superview.bounds.size.height-targetBounds.size.height)*0.5f, targetBounds.size.width, targetBounds.size.height);
} else {
self.bounds = CGRectMake(0, 0, targetBounds.size.width, targetBounds.size.height);
self.center = _hud.center;
}
[CATransaction begin];
[CATransaction setDisableActions:YES];
[CATransaction setCompletionBlock:^{
if (_showActivity) {
_activity.activityIndicatorViewStyle = _activityStyle;
_activity.frame = CGRectIntegral(activityRect);
}
CGRect r = self.frame;
self.frame = CGRectIntegral(r);
if ([delegate respondsToSelector:@selector(hudWillAppear:)]) {
[delegate hudWillAppear:_hud];
}
if (blockDelegate) {
blockDelegate(ATMHudActionWillAppear, _hud);
}
self.transform = CGAffineTransformMakeScale(_hud.appearScaleFactor, _hud.appearScaleFactor);
[UIView animateWithDuration:_hud.animateDuration
animations:^{
self.transform = CGAffineTransformMakeScale(1.0, 1.0);
self.alpha = 1.0;
}
completion:^(BOOL finished){
// if (finished) Got to do this regardless of whether it finished or not.
{
if (!_hud.allowSuperviewInteraction) {
self.superview.userInteractionEnabled = YES;
}
#ifdef ATM_SOUND
if (![_hud.showSound isEqualToString:@""] && _hud.showSound != NULL) {
[_hud playSound:_hud.showSound];
}
#endif
if ([delegate respondsToSelector:@selector(hudDidAppear:)]) {
[delegate hudDidAppear:_hud];
}
if (blockDelegate) {
blockDelegate(ATMHudActionDidAppear, _hud);
}
}
}];
}];
_backgroundLayer.position = CGPointMake(0.5f*targetBounds.size.width, 0.5f*targetBounds.size.height);
_backgroundLayer.bounds = targetBounds;
captionLayer.position = [self integralPoint:CGPointMake(captionRect.origin.x, captionRect.origin.y)];
captionLayer.bounds = CGRectMake(0, 0, captionRect.size.width, captionRect.size.height);
CABasicAnimation *cAnimation = [CABasicAnimation animationWithKeyPath:@"caption"];
cAnimation.duration = 0.001;
cAnimation.toValue = _caption;
[captionLayer addAnimation:cAnimation forKey:@"captionAnimation"];
captionLayer.caption = _caption;
imageLayer.contents = (id)_image.CGImage;
imageLayer.position = [self integralPoint:CGPointMake(imageRect.origin.x, imageRect.origin.y)];
imageLayer.bounds = CGRectMake(0, 0, imageRect.size.width, imageRect.size.height);
progressLayer.position = [self integralPoint:CGPointMake(progressRect.origin.x, progressRect.origin.y)];
progressLayer.bounds = CGRectMake(0, 0, progressRect.size.width, progressRect.size.height);
progressLayer.progressBorderRadius = _hud.progressBorderRadius;
progressLayer.progressBorderWidth = _hud.progressBorderWidth;
progressLayer.progressRadius = _hud.progressRadius;
progressLayer.progressInset = _hud.progressInset;
progressLayer.theProgress = _progress;
[progressLayer setNeedsDisplay];
[CATransaction commit];
break;
}
case ATMHudApplyModeUpdate: {
// NSLog(@"ATMHud: ATMHudApplyModeUpdate delegate=%@", delegate);
if ([delegate respondsToSelector:@selector(hudWillUpdate:)]) {
[delegate hudWillUpdate:_hud];
}
if (blockDelegate) {
blockDelegate(ATMHudActionWillUpdate, _hud);
}
if (CGPointEqualToPoint(_hud.center, CGPointZero)) {
self.frame = CGRectMake((self.superview.bounds.size.width-targetBounds.size.width)*0.5f, (self.superview.bounds.size.height-targetBounds.size.height)*0.5f, targetBounds.size.width, targetBounds.size.height);
} else {
self.bounds = CGRectMake(0, 0, targetBounds.size.width, targetBounds.size.height);
self.center = _hud.center;
}
CABasicAnimation *ccAnimation = [CABasicAnimation animationWithKeyPath:@"caption"];
ccAnimation.duration = 0.001;
ccAnimation.toValue = @"";
ccAnimation.delegate = self;
[captionLayer addAnimation:ccAnimation forKey:@"captionClearAnimation"];
captionLayer.caption = @"";
[CATransaction begin];
[CATransaction setDisableActions:YES];
[CATransaction setCompletionBlock:^{
_backgroundLayer.bounds = targetBounds;
progressLayer.theProgress = _progress;
[progressLayer setNeedsDisplay];
CABasicAnimation *cAnimation = [CABasicAnimation animationWithKeyPath:@"caption"];
cAnimation.duration = 0.001;
cAnimation.toValue = _caption;
[captionLayer addAnimation:cAnimation forKey:@"captionAnimation"];
captionLayer.caption = _caption;
if (_showActivity) {
_activity.activityIndicatorViewStyle = _activityStyle;
_activity.frame = CGRectIntegral(activityRect);
}
CGRect r = self.frame;
[self setFrame:CGRectIntegral(r)];
#ifdef ATM_SOUND
if (![_hud.updateSound isEqualToString:@""] && _hud.updateSound != NULL) {
[_hud playSound:_hud.updateSound];
}
#endif
if ([delegate respondsToSelector:@selector(hudDidUpdate:)]) {
[delegate hudDidUpdate:_hud];
}
if (blockDelegate) {
blockDelegate(ATMHudActionDidUpdate, _hud);
}
}];
_backgroundLayer.position = CGPointMake(0.5f*targetBounds.size.width, 0.5f*targetBounds.size.height);
imageLayer.position = [self integralPoint:CGPointMake(imageRect.origin.x, imageRect.origin.y)];
progressLayer.position = [self integralPoint:CGPointMake(progressRect.origin.x, progressRect.origin.y)];
imageLayer.bounds = CGRectMake(0, 0, imageRect.size.width, imageRect.size.height);
progressLayer.bounds = CGRectMake(0, 0, progressRect.size.width, progressRect.size.height);
progressLayer.progressBorderRadius = _hud.progressBorderRadius;
progressLayer.progressBorderWidth = _hud.progressBorderWidth;
progressLayer.progressRadius = _hud.progressRadius;
progressLayer.progressInset = _hud.progressInset;
captionLayer.position = [self integralPoint:CGPointMake(captionRect.origin.x, captionRect.origin.y)];
captionLayer.bounds = CGRectMake(0, 0, captionRect.size.width, captionRect.size.height);
imageLayer.contents = (id)_image.CGImage;
[CATransaction commit];
break;
}
case ATMHudApplyModeHide: {
// NSLog(@"ATMHud: ATMHudApplyModeHide delegate=%@", delegate);
if ([delegate respondsToSelector:@selector(hudWillDisappear:)]) {
[delegate hudWillDisappear:_hud];
}
if (blockDelegate) {
blockDelegate(ATMHudActionWillDisappear, _hud);
}
#ifdef ATM_SOUND
if (![_hud.hideSound isEqualToString:@""] && _hud.hideSound != NULL) {
[_hud playSound:_hud.hideSound];
}
#endif
//NSLog(@"GOT TO ATMHudApplyModeHide duration=%f delegate=%x _hud=%x", _hud.animateDuration, (unsigned int)delegate, (unsigned int)_hud);
__weak ATMHudView *weakSelf = self;
[UIView animateWithDuration:_hud.animateDuration
animations:^{
self.alpha = 0.0;
self.transform = CGAffineTransformMakeScale(_hud.disappearScaleFactor, _hud.disappearScaleFactor);
}
completion:^(BOOL finished){
// if (finished) Got to do this regardless of whether it finished or not.
{
weakSelf.superview.userInteractionEnabled = NO;
weakSelf.transform = CGAffineTransformMakeScale(1.0f, 1.0f);
#define NO_CRASH
[weakSelf reset];
#ifdef NO_CRASH
[weakSelf.hud.view removeFromSuperview];
#else
dispatch_async(dispatch_get_main_queue(), ^
{
[weakSelf.hud.view removeFromSuperview];
} );
#endif
if ([delegate respondsToSelector:@selector(hudDidDisappear:)]) {
[delegate hudDidDisappear:weakSelf.hud];
}
if (blockDelegate) {
blockDelegate(ATMHudActionDidDisappear, weakSelf.hud);
}
}
}];
break;
}
}
}
- (void)show
{
if (didHide) {
//NSLog(@"ATMHUD SHOW!!!");
didHide = NO;
[self calculate];
[self applyWithMode:ATMHudApplyModeShow];
} else {
//NSLog(@"ATMHUD Asked to show, but already showing!!!");
}
}
- (void)hide
{
if (!didHide) {
didHide = YES; // multiple calls to hide wrecks havoc, might get called in a cleanup routine in user code just to be sure.
//NSLog(@"ATMHUD HIDE!!!");
[self applyWithMode:ATMHudApplyModeHide];
} else {
//NSLog(@"ATMHUD Asked to hide, but already hidden!!!");
}
}
- (void)update
{
[self calculate];
[self applyWithMode:ATMHudApplyModeUpdate];
}
- (void)reset
{
ATMHud *hud = _hud;
if(!hud) return;
assert([NSThread isMainThread]);
[hud setCaption:@""];
[hud setImage:nil];
[hud setProgress:0];
[hud setActivity:NO];
[hud setActivityStyle:UIActivityIndicatorViewStyleWhiteLarge];
[hud setAccessoryPosition:ATMHudAccessoryPositionBottom];
[hud setBlockTouches:NO];
[hud setAllowSuperviewInteraction:NO];
[hud setFixedSize:CGSizeZero];
[hud setCenter:CGPointZero];
[CATransaction begin];
[CATransaction setDisableActions:YES];
imageLayer.contents = nil;
[CATransaction commit];
CABasicAnimation *cAnimation = [CABasicAnimation animationWithKeyPath:@"caption"];
cAnimation.duration = 0.001;
cAnimation.toValue = @"";
[captionLayer addAnimation:cAnimation forKey:@"captionAnimation"];
captionLayer.caption = @"";
#ifdef ATM_SOUND
[hud setShowSound:@""];
[hud setUpdateSound:@""];
[hud setHideSound:@""];
#endif
}
#pragma mark -
// Issue #21 - provided by paweldudek
- (void)layoutSubviews
{
[super layoutSubviews];
self.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.bounds].CGPath;
}
@end