forked from scottjacksonx/SJNotificationViewController
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSJNotificationViewController.m
264 lines (216 loc) · 7.84 KB
/
SJNotificationViewController.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
/*
Copyright (c) <YEAR>, <OWNER>
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#import "SJNotificationViewController.h"
#import <QuartzCore/QuartzCore.h>
#define SLIDE_DURATION 0.25f
#define LABEL_RESIZE_DURATION 0.1f
#define COLOR_FADE_DURATION 0.25f
#define ERROR_HEX_COLOR 0xff0000
#define MESSAGE_HEX_COLOR 0x0f5297
#define SUCCESS_HEX_COLOR 0x00ff00
#define NOTIFICATION_VIEW_OPACITY 0.85f
@implementation SJNotificationViewController
@synthesize parentView, notificationPosition;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
showSpinner = NO;
[self setNotificationLevel:SJNotificationLevelMessage];
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - Showing/Hiding the Notification
- (void)show {
NSLog(@"showing notification view");
/* Attach to the bottom of the parent view. */
CGFloat yPosition;
switch (notificationPosition) {
case SJNotificationPositionTop:
yPosition = self.view.frame.size.height * -1;
break;
default:
yPosition = [parentView frame].size.height;
break;
}
[self.view setFrame:CGRectMake(0, yPosition, self.view.frame.size.width, self.view.frame.size.height)];
[parentView addSubview:self.view];
[UIView animateWithDuration:SLIDE_DURATION
animations:^{
/* Slide the notification view up. */
CGRect shownRect = CGRectMake(0,
[self yPositionWhenHidden:NO],
self.view.frame.size.width,
self.view.frame.size.height);
[self.view setFrame:shownRect];
}
];
}
- (void)hide {
NSLog(@"hiding notification view");
if (!self.view.superview) {
return;
}
[UIView animateWithDuration:SLIDE_DURATION
animations:^{
/* Slide the notification view down. */
[self.view setFrame:CGRectMake(0, [self yPositionWhenHidden:YES], self.view.frame.size.width, self.view.frame.size.height)];
}
completion:^(BOOL finished) {
[self.view removeFromSuperview];
}
];
}
#pragma mark - Calculating position
- (CGFloat)yPositionWhenHidden:(BOOL)hidden {
CGFloat y;
// when hidden
if (hidden) {
switch (notificationPosition) {
case SJNotificationPositionTop:
y = self.view.frame.size.height * -1;
break;
case SJNotificationPositionBottom:
default:
y = [parentView frame].size.height;
break;
}
// when shown
} else {
switch (notificationPosition) {
case SJNotificationPositionTop:
y = 0;
break;
case SJNotificationPositionBottom:
default:
y = [parentView frame].size.height - self.view.frame.size.height;
break;
}
}
return y;
}
#pragma mark - Setting Notification Title
- (void)setNotificationTitle:(NSString *)t {
notificationTitle = t;
[label setText:t];
}
#pragma mark - Setting Tap Action
- (void)setTapTarget:(id)target selector:(SEL)selector {
for (UIGestureRecognizer *r in [self.view gestureRecognizers]) {
[self.view removeGestureRecognizer:r];
}
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:target action:selector];
[self.view addGestureRecognizer:tap];
}
#pragma mark - Setting Notification Level
- (void)setNotificationLevel:(SJNotificationLevel)level {
notificationLevel = level;
UIColor *color;
switch (notificationLevel) {
case SJNotificationLevelError:
color = [UIColor colorWithRed:((float)((ERROR_HEX_COLOR & 0xFF0000) >> 16))/255.0
green:((float)((ERROR_HEX_COLOR & 0xFF00) >> 8))/255.0
blue:((float)(ERROR_HEX_COLOR & 0xFF))/255.0 alpha:NOTIFICATION_VIEW_OPACITY];
break;
case SJNotificationLevelMessage:
color = [UIColor colorWithRed:((float)((MESSAGE_HEX_COLOR & 0xFF0000) >> 16))/255.0
green:((float)((MESSAGE_HEX_COLOR & 0xFF00) >> 8))/255.0
blue:((float)(MESSAGE_HEX_COLOR & 0xFF))/255.0 alpha:NOTIFICATION_VIEW_OPACITY];
break;
case SJNotificationLevelSuccess:
color = [UIColor colorWithRed:((float)((SUCCESS_HEX_COLOR & 0xFF0000) >> 16))/255.0
green:((float)((SUCCESS_HEX_COLOR & 0xFF00) >> 8))/255.0
blue:((float)(SUCCESS_HEX_COLOR & 0xFF))/255.0 alpha:NOTIFICATION_VIEW_OPACITY];
break;
default:
break;
}
[UIView animateWithDuration:COLOR_FADE_DURATION
animations:^ {
[self.view setBackgroundColor:color];
}
];
}
#pragma mark - Spinner
- (void)setShowSpinner:(BOOL)b {
showSpinner = b;
if (showSpinner) {
NSLog(@"spinner showing");
[spinner.layer setOpacity:1.0];
[UIView animateWithDuration:LABEL_RESIZE_DURATION
animations:^{
[label setFrame:CGRectMake(44, label.frame.origin.y, 258, label.frame.size.height)];
}
completion:^(BOOL finished) {
[spinner startAnimating];
}
];
} else {
NSLog(@"spinner not showing");
[spinner stopAnimating];
[spinner.layer setOpacity:0.0];
[UIView animateWithDuration:LABEL_RESIZE_DURATION
animations:^{
[label setFrame:CGRectMake(12, label.frame.origin.y, 290, label.frame.size.height)];
}
];
}
}
#pragma mark - Hide
- (void)showFor:(NSInteger)seconds
{
if (seconds > 0)
{
NSTimer *notificationTimer;
notificationTimer = [NSTimer scheduledTimerWithTimeInterval:seconds target:self
selector:@selector(notificationTimerHide) userInfo:nil repeats:NO];
[self show];
}
else
{
[self show];
}
}
- (void)notificationTimerHide
{
[self hide];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
/* By default, tapping the notification view hides it. */
[self setTapTarget:self selector:@selector(hide)];
if (showSpinner) {
[self setShowSpinner:YES];
} else {
[self setShowSpinner:NO];
}
[label setText:notificationTitle];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end