-
Notifications
You must be signed in to change notification settings - Fork 3
/
AMTumblrHud.m
132 lines (106 loc) · 3.55 KB
/
AMTumblrHud.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
//
// Created by Mustafin Askar on 22/05/2014.
// Copyright (c) 2014 Asich. All rights reserved.
//
#import "AMTumblrHud.h"
#define kShowHideAnimateDuration 0.2
@implementation AMTumblrHud {
NSMutableArray *hudRects;
}
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self configUI];
self.userInteractionEnabled = NO;
self.alpha = 0;
}
return self;
}
#pragma mark - config UI
- (void)configUI {
self.backgroundColor = [UIColor clearColor];
UIView *rect1 = [self drawRectAtPosition:CGPointMake(0, 0)];
UIView *rect2 = [self drawRectAtPosition:CGPointMake(20, 0)];
UIView *rect3 = [self drawRectAtPosition:CGPointMake(40, 0)];
[self addSubview:rect1];
[self addSubview:rect2];
[self addSubview:rect3];
[self doAnimateCycleWithRects:@[rect1, rect2, rect3]];
}
#pragma mark - animation
- (void)doAnimateCycleWithRects:(NSArray *)rects {
__weak typeof(self) wSelf = self;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t) (0.25 * 0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[wSelf animateRect:rects[0] withDuration:0.25];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t) (0.25 * 0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[wSelf animateRect:rects[1] withDuration:0.2];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t) (0.2 * 0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[wSelf animateRect:rects[2] withDuration:0.15];
});
});
});
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t) (0.6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[wSelf doAnimateCycleWithRects:rects];
});
}
- (void)animateRect:(UIView *)rect withDuration:(NSTimeInterval)duration {
[rect setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
[UIView animateWithDuration:duration
animations:^{
rect.alpha = 1;
rect.transform = CGAffineTransformMakeScale(1, 1.3);
} completion:^(BOOL finished) {
[UIView animateWithDuration:duration
animations:^{
rect.alpha = 0.5;
rect.transform = CGAffineTransformMakeScale(1, 1);
} completion:^(BOOL f) {
}];
}];
}
#pragma mark - drawing
- (UIView *)drawRectAtPosition:(CGPoint)positionPoint {
UIView *rect = [[UIView alloc] init];
CGRect rectFrame;
rectFrame.size.width = 15;
rectFrame.size.height = 16.5;
rectFrame.origin.x = positionPoint.x;
rectFrame.origin.y = 0;
rect.frame = rectFrame;
rect.backgroundColor = [UIColor redColor];
rect.alpha = 0.5;
rect.layer.cornerRadius = 3;
if (hudRects == nil) {
hudRects = [[NSMutableArray alloc] init];
}
[hudRects addObject:rect];
return rect;
}
#pragma mark - Setters
- (void)setHudColor:(UIColor *)hudColor {
for (UIView *rect in hudRects) {
rect.backgroundColor = hudColor;
}
}
#pragma mark -
#pragma mark - show / hide
- (void)hide {
[UIView animateWithDuration:kShowHideAnimateDuration animations:^{
self.alpha = 0;
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}
- (void)showAnimated:(BOOL)animated {
if (animated) {
[UIView animateWithDuration:kShowHideAnimateDuration animations:^{
self.alpha = 1;
}];
} else {
self.alpha = 1;
}
}
- (void)dealloc {
hudRects = nil;
}
@end