-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGVLoadingView.m
184 lines (151 loc) · 5.2 KB
/
GVLoadingView.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
//
// GVLoadingView.m
// Loader
//
// Created by Gabriel Vincent on 14/07/12.
// Copyright (c) 2012 _A_Z. All rights reserved.
//
#define OriginX self.frame.origin.x
#define OriginY self.frame.origin.y
#define Width self.frame.size.width
#define Height self.frame.size.height
#define HiddenFrame CGRectMake(self.frame.origin.x, self.superview.frame.size.height, self.frame.size.width, self.frame.size.height);
#define SuperviewHeight self.superview.frame.size.height
#import "GVLoadingView.h"
@implementation GVLoadingView
@synthesize delegate, reloadMethod;
@synthesize messageLabel;
@synthesize spinner;
@synthesize animationTime;
@synthesize reloadImage;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
finalFrame = frame;
messageLabel = [[UILabel alloc] init];
spinner = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
}
return self;
}
- (id) init {
self = [super init];
if (self) {
isFirstCall = YES;
messageLabel = [[UILabel alloc] init];
spinner = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
}
return self;
}
- (void)setFrame:(CGRect)frame
{
[super setFrame:frame];
if (!CGRectIsNull(frame) && isFirstCall) {
finalFrame = frame;
isFirstCall = NO;
}
}
- (void)drawRect:(CGRect)rect
{
// View configuration
// Message label configuration
messageLabel.frame = CGRectMake(0, 0, Width, Height);
messageLabel.backgroundColor = [UIColor clearColor];
messageLabel.textAlignment = UITextAlignmentCenter;
// Activity Indicator View configuration
spinner.center = CGPointMake(20, Height/2);
[self addSubview:messageLabel];
[self addSubview:spinner];
[spinner startAnimating];
}
- (void) exitReloadModeWithMessage:(NSString *) reloadMessage; {
messageLabel.text = reloadMessage;
spinner.center = CGPointMake(20, self.frame.size.height/2);
[spinner startAnimating];
[reloadButton removeFromSuperview];
[self addSubview:spinner];
}
- (void) enterReloadModeWithMessage:(NSString *) reloadMessage {
reloadTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:delegate action:reloadMethod];
[self addGestureRecognizer:reloadTapGesture];
messageLabel.text = reloadMessage;
reloadButton = [UIButton buttonWithType:UIButtonTypeCustom];
reloadButton.frame = CGRectMake(0, 0, reloadImage.size.width, reloadImage.size.height);
[reloadButton setImage:reloadImage forState:UIControlStateNormal];
reloadButton.center = spinner.center;
[reloadButton addTarget:delegate action:reloadMethod forControlEvents:UIControlEventTouchUpInside];
[self addSubview:reloadButton];
[spinner stopAnimating];
[spinner removeFromSuperview];
}
- (void) showWithAnimation:(GVLoadingViewShowAnimation)animation {
[delegate.view addSubview:self];
if (animation == GVLoadingViewShowAnimationNone) {
self.frame = finalFrame;
}
else {
if (animation == GVLoadingViewShowAnimationAppear) {
self.frame = CGRectMake(OriginX, SuperviewHeight, Width, Height);
}
else if (animation == GVLoadingViewShowAnimationFade) {
self.frame = finalFrame;
self.alpha = 0.0;
}
else if (animation == GVLoadingViewShowAnimationShutter) {
self.frame = CGRectMake(OriginX, SuperviewHeight, Width, finalFrame.size.height);
self.frame = CGRectMake(self.frame.origin.x, SuperviewHeight-Height, Width, 0);
}
else if (animation == GVLoadingViewShowAnimationDrop) {
self.frame = CGRectMake(OriginX, finalFrame.origin.y-40, Width, Height);
self.alpha = 0.0;
}
[UIView animateWithDuration:animationTime animations:^{
self.frame = finalFrame;
self.alpha = 1.0;
} completion:^(BOOL finished){
if (animation == GVLoadingViewShowAnimationDrop) {
[UIView animateWithDuration:animationTime animations:^{
self.frame = CGRectMake(OriginX, OriginY-7, Width, Height+4);
} completion:^(BOOL finished) {
[UIView animateWithDuration:animationTime animations:^{
self.frame = CGRectMake(OriginX, OriginY+7, Width, Height-4);
} completion:^(BOOL finished) {
[UIView animateWithDuration:animationTime animations:^{
self.frame = CGRectMake(OriginX, OriginY-2, Width, Height+2);
} completion:^(BOOL finished) {
[UIView animateWithDuration:animationTime animations:^{
self.frame = CGRectMake(OriginX, OriginY+2, Width, Height-2);
}];
}];
}];
}];
}
}];
}
}
- (void) dismissWithAnimation:(GVLoadingViewDismissAnimation)animation {
if (animation == GVLoadingViewDismissAnimationNone) {
self.frame = HiddenFrame;
[self removeFromSuperview];
}
else {
if (animation == GVLoadingViewDismissAnimationDisappear) {
[UIView animateWithDuration:animationTime animations:^{
self.frame = HiddenFrame;
}];
}
else if (animation == GVLoadingViewDismissAnimationFade) {
[UIView animateWithDuration:animationTime animations:^{
self.alpha = 0.0;
}];
}
else if (animation == GVLoadingViewDismissAnimationShutter) {
[UIView animateWithDuration:animationTime animations:^{
self.frame = CGRectMake(self.frame.origin.x, SuperviewHeight-Height, Width, 0);
self.alpha = 0.0;
}];
}
[self performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:animationTime];
}
}
@end