/
MPNotificationView.m
541 lines (447 loc) · 19.5 KB
/
MPNotificationView.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
//
// MPNotificationView.m
// Moped
//
// Created by Engin Kurutepe on 1/2/13.
// Copyright (c) 2013 Moped Inc. All rights reserved.
//
#import "MPNotificationView.h"
#import "OBGradientView.h"
#define kMPNotificationHeight 40.0f
#define kMPNotificationIPadWidth 480.0f
#define RADIANS(deg) ((deg) * M_PI / 180.0f)
static NSMutableDictionary * _registeredTypes;
static CGRect notificationRect()
{
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))
{
return CGRectMake(0.0f, 0.0f, [UIScreen mainScreen].bounds.size.height, kMPNotificationHeight);
}
return CGRectMake(0.0f, 0.0f, [UIScreen mainScreen].bounds.size.width, kMPNotificationHeight);
}
NSString *kMPNotificationViewTapReceivedNotification = @"kMPNotificationViewTapReceivedNotification";
#pragma mark MPNotificationWindow
@interface MPNotificationWindow : UIWindow
@property (nonatomic, strong) NSMutableArray *notificationQueue;
@property (nonatomic, strong) UIView *currentNotification;
@end
@implementation MPNotificationWindow
- (void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (id) initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
self.windowLevel = UIWindowLevelStatusBar + 1;
self.backgroundColor = [UIColor clearColor];
_notificationQueue = [[NSMutableArray alloc] initWithCapacity:4];
UIView *topHalfBlackView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMinX(frame),
CGRectGetMinY(frame),
CGRectGetWidth(frame),
0.5 * CGRectGetHeight(frame))];
topHalfBlackView.backgroundColor = [UIColor blackColor];
topHalfBlackView.layer.zPosition = -100;
topHalfBlackView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self addSubview:topHalfBlackView];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(willRotateScreen:)
name:UIApplicationWillChangeStatusBarFrameNotification
object:nil];
[self rotateNotificationWindow];
}
return self;
}
- (void) willRotateScreen:(NSNotification *)notification
{
if (self.hidden)
{
double delayInSeconds = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self rotateNotificationWindow];
});
}
else
{
[self rotateNotificationWindowAnimated];
}
}
- (void) rotateNotificationWindowAnimated
{
CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;
[UIView animateWithDuration:duration
animations:^{
self.alpha = 0;
} completion:^(BOOL finished) {
[self rotateNotificationWindow];
[UIView animateWithDuration:duration
animations:^{
self.alpha = 1;
}];
}];
}
- (void) rotateNotificationWindow
{
CGRect frame = notificationRect();
BOOL isPortrait = (frame.size.width == [UIScreen mainScreen].bounds.size.width);
if (isPortrait)
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
frame.size.width = kMPNotificationIPadWidth;
}
if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown)
{
frame.origin.y = [UIScreen mainScreen].bounds.size.height - kMPNotificationHeight;
self.transform = CGAffineTransformMakeRotation(RADIANS(180.0f));
}
else
{
self.transform = CGAffineTransformIdentity;
}
}
else
{
frame.size.height = frame.size.width;
frame.size.width = kMPNotificationHeight;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
frame.size.height = kMPNotificationIPadWidth;
}
if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft)
{
frame.origin.x = [UIScreen mainScreen].bounds.size.width - frame.size.width;
self.transform = CGAffineTransformMakeRotation(RADIANS(90.0f));
}
else
{
self.transform = CGAffineTransformMakeRotation(RADIANS(-90.0f));
}
}
self.frame = frame;
CGPoint center = self.center;
if (isPortrait)
{
center.x = CGRectGetMidX([UIScreen mainScreen].bounds);
}
else
{
center.y = CGRectGetMidY([UIScreen mainScreen].bounds);
}
self.center = center;
}
@end
static MPNotificationWindow * __notificationWindow = nil;
static CGFloat const __imagePadding = 8.0f;
#pragma mark -
#pragma mark MPNotificationView
@interface MPNotificationView ()
@property (nonatomic, strong) OBGradientView * contentView;
@property (nonatomic, copy) MPNotificationSimpleAction tapBlock;
@property (nonatomic, strong) UITapGestureRecognizer *tapGestureRecognizer;
+ (UIImage*) screenImageWithRect:(CGRect)rect;
@end
@implementation MPNotificationView
- (void) dealloc
{
_delegate = nil;
[self removeGestureRecognizer:_tapGestureRecognizer];
}
- (id) initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
CGFloat notificationWidth = notificationRect().size.width;
self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
_contentView = [[OBGradientView alloc] initWithFrame:self.bounds];
_contentView.colors = @[(id)[[UIColor colorWithWhite:0.99f alpha:1.0f] CGColor],
(id)[[UIColor colorWithWhite:0.9f alpha:1.0f] CGColor]];
_contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
_contentView.layer.cornerRadius = 8.0f;
_contentView.clipsToBounds = YES;
[self addSubview:_contentView];
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(6, 6, 28, 28)];
_imageView.contentMode = UIViewContentModeScaleAspectFill;
_imageView.layer.cornerRadius = 4.0f;
_imageView.clipsToBounds = YES;
[self addSubview:_imageView];
UIFont *textFont = [UIFont boldSystemFontOfSize:14.0f];
CGRect textFrame = CGRectMake(__imagePadding + CGRectGetMaxX(_imageView.frame),
2,
notificationWidth - __imagePadding * 2 - CGRectGetMaxX(_imageView.frame),
textFont.lineHeight);
_textLabel = [[UILabel alloc] initWithFrame:textFrame];
_textLabel.font = textFont;
_textLabel.numberOfLines = 1;
_textLabel.textAlignment = UITextAlignmentLeft;
_textLabel.lineBreakMode = UILineBreakModeTailTruncation;
_textLabel.backgroundColor = [UIColor clearColor];
[_contentView addSubview:_textLabel];
UIFont *detailFont = [UIFont systemFontOfSize:13.0f];
CGRect detailFrame = CGRectMake(CGRectGetMinX(textFrame),
CGRectGetMaxY(textFrame),
notificationWidth - __imagePadding * 2 - CGRectGetMaxX(_imageView.frame),
detailFont.lineHeight);
_detailTextLabel = [[UILabel alloc] initWithFrame:detailFrame];
_detailTextLabel.font = detailFont;
_detailTextLabel.numberOfLines = 1;
_detailTextLabel.textAlignment = UITextAlignmentLeft;
_detailTextLabel.lineBreakMode = UILineBreakModeTailTruncation;
_detailTextLabel.backgroundColor = [UIColor clearColor];
[_contentView addSubview:_detailTextLabel];
UIView *bottomLine = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMinX(frame),
CGRectGetHeight(frame) - 1.0f,
CGRectGetWidth(frame),
1.0f)];
bottomLine.backgroundColor = [UIColor colorWithWhite:0.92 alpha:1.0];
[_contentView addSubview:bottomLine];
}
return self;
}
+ (MPNotificationView *) notifyWithText:(NSString*)text
andDetail:(NSString*)detail
{
return [self notifyWithText:text
detail:detail
andDuration:2.0f];
}
+ (MPNotificationView *) notifyWithText:(NSString*)text
detail:(NSString*)detail
andDuration:(NSTimeInterval)duration
{
return [self notifyWithText:text
detail:detail
image:nil
andDuration:duration];
}
+ (MPNotificationView *) notifyWithText:(NSString*)text
detail:(NSString*)detail
image:(UIImage*)image
andDuration:(NSTimeInterval)duration
{
return [self notifyWithText:text
detail:detail
image:image
duration:duration
andTouchBlock:nil];
}
+ (MPNotificationView *) notifyWithText:(NSString*)text
detail:(NSString*)detail
duration:(NSTimeInterval)duration
andTouchBlock:(MPNotificationSimpleAction)block
{
return [self notifyWithText:text
detail:detail
image:nil
duration:duration
andTouchBlock:block];
}
+ (MPNotificationView *) notifyWithText:(NSString*)text
detail:(NSString*)detail
andTouchBlock:(MPNotificationSimpleAction)block
{
return [self notifyWithText:text
detail:detail
image:nil
duration:2.0
andTouchBlock:block];
}
+ (MPNotificationView *) notifyWithText:(NSString*)text
detail:(NSString*)detail
image:(UIImage*)image
duration:(NSTimeInterval)duration
andTouchBlock:(MPNotificationSimpleAction)block
{
return [self notifyWithText:text
detail:detail
image:image
duration:duration
type:nil
andTouchBlock:block];
}
+ (MPNotificationView *) notifyWithText:(NSString*)text
detail:(NSString*)detail
image:(UIImage*)image
duration:(NSTimeInterval)duration
type:(NSString *)type
andTouchBlock:(MPNotificationSimpleAction)block
{
if (__notificationWindow == nil)
{
__notificationWindow = [[MPNotificationWindow alloc] initWithFrame:notificationRect()];
__notificationWindow.hidden = NO;
}
MPNotificationView * notification;
id nibNameOrClass = type ? _registeredTypes[type] : nil;
if ([nibNameOrClass isKindOfClass:[NSString class]])
{
notification = [[NSBundle mainBundle] loadNibNamed:nibNameOrClass
owner:nil
options:nil][0];
notification.frame = __notificationWindow.bounds;
}
else if (!nibNameOrClass)
{
notification = [[MPNotificationView alloc] initWithFrame:__notificationWindow.bounds];
}
else
{
notification = [[nibNameOrClass alloc] initWithFrame:__notificationWindow.bounds];
}
notification.textLabel.text = text;
notification.detailTextLabel.text = detail;
notification.imageView.image = image;
notification.duration = duration;
notification.tapBlock = block;
UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:notification
action:@selector(handleTap:)];
notification.tapGestureRecognizer = gr;
[notification addGestureRecognizer:gr];
[__notificationWindow.notificationQueue addObject:notification];
if (__notificationWindow.currentNotification == nil)
{
[self showNextNotification];
}
return notification;
}
+ (void)registerNibNameOrClass:(id)nibNameOrClass
forNotificationsOfType:(NSString *)type
{
if (!_registeredTypes)
_registeredTypes = [NSMutableDictionary dictionary];
_registeredTypes[type] = nibNameOrClass;
}
- (void) handleTap:(UITapGestureRecognizer *)gestureRecognizer
{
if (_tapBlock != nil)
{
_tapBlock(self);
}
if ([_delegate respondsToSelector:@selector(didTapOnNotificationView:)])
{
[_delegate didTapOnNotificationView:self];
}
[[NSNotificationCenter defaultCenter] postNotificationName:kMPNotificationViewTapReceivedNotification
object:self];
[MPNotificationView showNextNotification];
}
+ (void) showNextNotification
{
[NSObject cancelPreviousPerformRequestsWithTarget:[self class]
selector:@selector(showNextNotification)
object:nil];
UIView *viewToRotateOut = nil;
UIImage *screenshot = [self screenImageWithRect:__notificationWindow.frame];
if (__notificationWindow.currentNotification)
{
viewToRotateOut = __notificationWindow.currentNotification;
}
else
{
viewToRotateOut = [[UIImageView alloc] initWithFrame:__notificationWindow.bounds];
((UIImageView *)viewToRotateOut).image = screenshot;
[__notificationWindow addSubview:viewToRotateOut];
__notificationWindow.hidden = NO;
}
UIView *viewToRotateIn = nil;
if ([__notificationWindow.notificationQueue count] > 0)
{
viewToRotateIn = __notificationWindow.notificationQueue[0];
}
else
{
viewToRotateIn = [[UIImageView alloc] initWithFrame:__notificationWindow.bounds];
((UIImageView *)viewToRotateIn).image = screenshot;
}
viewToRotateIn.layer.anchorPointZ = 11.547f;
viewToRotateIn.layer.doubleSided = NO;
viewToRotateIn.layer.zPosition = 2;
CATransform3D viewInStartTransform = CATransform3DMakeRotation(RADIANS(-120), 1.0, 0.0, 0.0);
viewInStartTransform.m34 = -1.0 / 200.0;
viewToRotateOut.layer.anchorPointZ = 11.547f;
viewToRotateOut.layer.doubleSided = NO;
viewToRotateOut.layer.zPosition = 2;
CATransform3D viewOutEndTransform = CATransform3DMakeRotation(RADIANS(120), 1.0, 0.0, 0.0);
viewOutEndTransform.m34 = -1.0 / 200.0;
[__notificationWindow addSubview:viewToRotateIn];
__notificationWindow.backgroundColor = [UIColor blackColor];
viewToRotateIn.layer.transform = viewInStartTransform;
if ([viewToRotateIn isKindOfClass:[MPNotificationView class]] )
{
UIImageView *bgImage = [[UIImageView alloc] initWithFrame:__notificationWindow.bounds];
bgImage.image = screenshot;
[viewToRotateIn addSubview:bgImage];
[viewToRotateIn sendSubviewToBack:bgImage];
__notificationWindow.currentNotification = viewToRotateIn;
}
[UIView animateWithDuration:0.5
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
viewToRotateIn.layer.transform = CATransform3DIdentity;
viewToRotateOut.layer.transform = viewOutEndTransform;
}
completion:^(BOOL finished) {
[viewToRotateOut removeFromSuperview];
[__notificationWindow.notificationQueue removeObject:viewToRotateOut];
if ([viewToRotateIn isKindOfClass:[MPNotificationView class]])
{
MPNotificationView *notification = (MPNotificationView*)viewToRotateIn;
if (notification.duration > 0.0)
{
[self performSelector:@selector(showNextNotification)
withObject:nil
afterDelay:notification.duration];
}
__notificationWindow.currentNotification = notification;
[__notificationWindow.notificationQueue removeObject:notification];
}
else
{
[viewToRotateIn removeFromSuperview];
__notificationWindow.hidden = YES;
__notificationWindow.currentNotification = nil;
}
__notificationWindow.backgroundColor = [UIColor clearColor];
}];
}
+ (UIImage *) screenImageWithRect:(CGRect)rect
{
CALayer *layer = [[UIApplication sharedApplication] keyWindow].layer;
CGFloat scale = [UIScreen mainScreen].scale;
UIGraphicsBeginImageContextWithOptions(layer.frame.size, NO, scale);
[layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
rect = CGRectMake(rect.origin.x * scale, rect.origin.y * scale
, rect.size.width * scale, rect.size.height * scale);
CGImageRef imageRef = CGImageCreateWithImageInRect([screenshot CGImage], rect);
UIImage *croppedScreenshot = [UIImage imageWithCGImage:imageRef
scale:screenshot.scale
orientation:screenshot.imageOrientation];
CGImageRelease(imageRef);
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
UIImageOrientation imageOrientation = UIImageOrientationUp;
switch (orientation)
{
case UIInterfaceOrientationPortraitUpsideDown:
imageOrientation = UIImageOrientationDown;
break;
case UIInterfaceOrientationLandscapeLeft:
imageOrientation = UIImageOrientationRight;
break;
case UIInterfaceOrientationLandscapeRight:
imageOrientation = UIImageOrientationLeft;
break;
default:
break;
}
return [[UIImage alloc] initWithCGImage:croppedScreenshot.CGImage
scale:croppedScreenshot.scale
orientation:imageOrientation];
}
@end