forked from ebaker355/DPToastView
-
Notifications
You must be signed in to change notification settings - Fork 1
/
DPToastView.m
556 lines (497 loc) · 32.5 KB
/
DPToastView.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
//
// DPToastView.m
// DPToastViewDemo
//
// Created by Baker, Eric on 2/15/13.
// Copyright (c) 2013 DuneParkSoftware, LLC. All rights reserved.
//
#import <QuartzCore/QuartzCore.h>
#import "DPToastView.h"
static id _DP_PreviousToastView = nil;
@interface DPToastView ()
@property (strong, nonatomic) UIView *toastView;
@property (strong, nonatomic) NSMutableArray *windowConstraints;
@property (assign, nonatomic) BOOL cancelNotifications;
@end
@implementation DPToastView
@synthesize message;
@synthesize textAlignment;
@synthesize lineBreakMode;
@synthesize gravity;
@synthesize duration;
@synthesize textColor, backgroundColor, borderColor, shadowColor;
@synthesize font;
@synthesize borderWidth, cornerRadius, shadowOpacity, shadowRadius, fadeInDuration, fadeOutDuration;
@synthesize shadowOffset;
@synthesize innerEdgeInsets;
@synthesize yOffset;
@synthesize horizontalMargin;
@synthesize rightView;
@synthesize toastView;
@synthesize windowConstraints;
@synthesize cancelNotifications;
+ (id)makeToast:(id)message {
return [[self class] makeToast:message gravity:DPToastGravityBottom duration:DPToastDurationNormal];
}
+ (id)makeToast:(id)message gravity:(DPToastGravity)gravity {
return [[self class] makeToast:message gravity:gravity duration:DPToastDurationNormal];
}
+ (id)makeToast:(id)message duration:(NSTimeInterval)duration {
return [[self class] makeToast:message gravity:DPToastGravityBottom duration:duration];
}
+ (id)makeToast:(id)message gravity:(DPToastGravity)gravity duration:(NSTimeInterval)duration {
return [[[self class] alloc] initWithMessage:message gravity:gravity duration:duration];
}
+ (void)dismissToast {
if (_DP_PreviousToastView) {
[[_DP_PreviousToastView toastView] removeFromSuperview];
[_DP_PreviousToastView setCancelNotifications:YES];
[[NSNotificationCenter defaultCenter] removeObserver:_DP_PreviousToastView];
[[NSNotificationCenter defaultCenter] postNotificationName:DPToastViewDidDismissNotification object:_DP_PreviousToastView userInfo:@{ DPToastViewUserInfoKey : _DP_PreviousToastView, DPToastViewStringUserInfoKey : [_DP_PreviousToastView messageString] }];
_DP_PreviousToastView = nil;
}
}
- (id)initWithMessage:(id)theMessage gravity:(DPToastGravity)theGravity duration:(NSTimeInterval)theDuration {
if ((self = [super init])) {
[self setMessage:theMessage];
[self setTextAlignment:NSTextAlignmentCenter];
[self setLineBreakMode:NSLineBreakByWordWrapping];
[self setGravity:theGravity];
[self setDuration:theDuration];
[self setTextColor:[UIColor whiteColor]];
[self setFont:[UIFont systemFontOfSize:16.0]];
[self setBackgroundColor:[UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:0.8]];
[self setBorderColor:[[UIColor whiteColor] colorWithAlphaComponent:0.8]];
[self setBorderWidth:1.5];
[self setCornerRadius:4.0];
[self setShadowColor:[UIColor blackColor]];
[self setShadowOpacity:0.8];
[self setShadowRadius:5.0];
[self setShadowOffset:CGSizeZero];
[self setInnerEdgeInsets:UIEdgeInsetsMake(6, 10, 6, 10)];
[self setYOffset:(theGravity == DPToastGravityCenter ? 0 : 60)];
[self setFadeInDuration:0.15];
[self setFadeOutDuration:0.5];
[self setHorizontalMargin:16];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(toastWasDismissed:) name:DPToastViewDidDismissNotification object:self];
[self setCancelNotifications:NO];
}
return self;
}
- (void)show {
UIWindow *window = [[[UIApplication sharedApplication] windows] objectAtIndex:0];
[self showInView:window];
}
- (void)showInView:(UIView *)view {
if (nil == view) return;
if (nil != _DP_PreviousToastView) {
[DPToastView dismissToast];
[self setFadeInDuration:0.0];
}
[self buildToastViewForView:view];
if (nil == toastView) return;
[[NSNotificationCenter defaultCenter] postNotificationName:DPToastViewWillAppearNotification object:self userInfo:@{ DPToastViewUserInfoKey : self, DPToastViewStringUserInfoKey : [self messageString] }];
[toastView setAlpha:0.0];
_DP_PreviousToastView = self;
[UIView animateWithDuration:[self fadeInDuration]
delay:0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
[self->toastView setAlpha:1.0];
}
completion:^(BOOL finished) {
if (NO == [self cancelNotifications]) {
[[NSNotificationCenter defaultCenter] postNotificationName:DPToastViewDidAppearNotification object:self userInfo:@{ DPToastViewUserInfoKey : self, DPToastViewStringUserInfoKey : [self messageString] }];
}
if (finished) {
[self performSelector:@selector(postWillDisappearNotification:) withObject:self afterDelay:[self duration]];
[UIView animateWithDuration:[self fadeOutDuration]
delay:[self duration]
options:UIViewAnimationOptionCurveEaseIn
animations:^{
[self->toastView setAlpha:0.0];
}
completion:^(BOOL finished) {
if (finished) {
if (NO == [self cancelNotifications]) {
[[NSNotificationCenter defaultCenter] postNotificationName:DPToastViewDidDisappearNotification object:self userInfo:@{ DPToastViewUserInfoKey : self, DPToastViewStringUserInfoKey : [self messageString] }];
}
[DPToastView dismissToast];
}
}];
}
}];
}
- (NSString *)messageString {
if ([[self message] isKindOfClass:[NSString class]]) {
return (NSString *)[self message];
} else if ([[self message] isKindOfClass:[NSAttributedString class]]) {
return [(NSAttributedString *)[self message] string];
}
return nil;
}
- (void)toastWasDismissed:(NSNotification *)notification {
[self setCancelNotifications:YES];
}
- (void)postWillDisappearNotification:(id)sender {
if (NO == [self cancelNotifications]) {
[[NSNotificationCenter defaultCenter] postNotificationName:DPToastViewWillDisappearNotification object:self userInfo:@{ DPToastViewUserInfoKey : self, DPToastViewStringUserInfoKey : [self messageString] }];
}
}
- (void)statusBarOrientationChanged:(NSNotification *)notification {
[self defineConstraintsForToastInView:[self.toastView superview]];
}
- (UIView *)buildToastViewForView:(UIView *)parentView {
UILabel *label = nil;
if ([self message]) {
if ([[self message] isKindOfClass:[NSString class]]) {
if ([[(NSString *)message stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] > 0) {
label = [[UILabel alloc] init];
[label setText:(NSString *)message];
[label setTextColor:[self textColor]];
[label setTextAlignment:[self textAlignment]];
[label setLineBreakMode:[self lineBreakMode]];
[label setFont:[self font]];
}
} else if ([[self message] isKindOfClass:[NSAttributedString class]]) {
if ([[[(NSAttributedString *)message string] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] > 0) {
label = [[UILabel alloc] init];
[label setAttributedText:(NSAttributedString *)message];
}
}
}
if (nil == label) return nil;
[label setBackgroundColor:[UIColor clearColor]];
[label setNumberOfLines:0];
[label setUserInteractionEnabled:NO];
[self setToastView:[[UIView alloc] init]];
[toastView setBackgroundColor:[self backgroundColor]];
[toastView setUserInteractionEnabled:NO];
[toastView.layer setBorderColor:[[self borderColor] CGColor]];
[toastView.layer setBorderWidth:[self borderWidth]];
[toastView.layer setCornerRadius:[self cornerRadius]];
[toastView.layer setShadowColor:[[self shadowColor] CGColor]];
[toastView.layer setShadowOpacity:[self shadowOpacity]];
[toastView.layer setShadowRadius:[self shadowRadius]];
[toastView.layer setShadowOffset:[self shadowOffset]];
[toastView addSubview:label];
[parentView addSubview:toastView];
[parentView bringSubviewToFront:toastView];
[label setTranslatesAutoresizingMaskIntoConstraints:NO];
[toastView setTranslatesAutoresizingMaskIntoConstraints:NO];
[toastView addConstraint:[NSLayoutConstraint constraintWithItem:label
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:toastView
attribute:NSLayoutAttributeTop
multiplier:1
constant:innerEdgeInsets.top]];
[toastView addConstraint:[NSLayoutConstraint constraintWithItem:toastView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:label
attribute:NSLayoutAttributeBottom
multiplier:1
constant:innerEdgeInsets.bottom]];
[toastView addConstraint:[NSLayoutConstraint constraintWithItem:label
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:toastView
attribute:NSLayoutAttributeLeft
multiplier:1
constant:innerEdgeInsets.left]];
if (rightView == nil) {
[toastView addConstraint:[NSLayoutConstraint constraintWithItem:toastView
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual
toItem:label
attribute:NSLayoutAttributeRight
multiplier:1
constant:innerEdgeInsets.right]];
} else {
[rightView setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
[rightView setTranslatesAutoresizingMaskIntoConstraints:NO];
[toastView addSubview:rightView];
[label setTextAlignment:NSTextAlignmentLeft];
NSLayoutConstraint *rightViewMaxWidthConstraint = [NSLayoutConstraint constraintWithItem:rightView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1
constant:100];
rightViewMaxWidthConstraint.priority = UILayoutPriorityDefaultLow;
[toastView addConstraint:rightViewMaxWidthConstraint];
[toastView addConstraint:[NSLayoutConstraint constraintWithItem:rightView
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:label
attribute:NSLayoutAttributeCenterY
multiplier:1
constant:0]];
[toastView addConstraint:[NSLayoutConstraint constraintWithItem:toastView
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual
toItem:rightView
attribute:NSLayoutAttributeRight
multiplier:1
constant:innerEdgeInsets.right]];
[toastView addConstraint:[NSLayoutConstraint constraintWithItem:rightView
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:label
attribute:NSLayoutAttributeRight
multiplier:1
constant:16]];
[toastView addConstraint:[NSLayoutConstraint constraintWithItem:rightView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:toastView
attribute:NSLayoutAttributeTop
multiplier:1
constant:0]];
[toastView addConstraint:[NSLayoutConstraint constraintWithItem:toastView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:rightView
attribute:NSLayoutAttributeBottom
multiplier:1
constant:0]];
}
[self defineConstraintsForToastInView:parentView];
return toastView;
}
- (void)defineConstraintsForToastInView:(UIView *)parentView {
if (nil == windowConstraints) {
windowConstraints = [[NSMutableArray alloc] init];
if ([parentView isKindOfClass:[UIWindow class]]) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarOrientationChanged:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
}
} else {
[parentView removeConstraints:windowConstraints];
[parentView setNeedsUpdateConstraints];
[windowConstraints removeAllObjects];
}
[windowConstraints addObject:[NSLayoutConstraint constraintWithItem:toastView
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:parentView
attribute:NSLayoutAttributeLeft
multiplier:1.0
constant:self.horizontalMargin]];
[windowConstraints addObject:[NSLayoutConstraint constraintWithItem:parentView
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual
toItem:toastView
attribute:NSLayoutAttributeRight
multiplier:1.0
constant:self.horizontalMargin]];
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (NO == [parentView isKindOfClass:[UIWindow class]]) {
orientation = UIInterfaceOrientationPortrait;
} else if ([[[UIDevice currentDevice] systemVersion] compare:@"8.0" options:NSNumericSearch] != NSOrderedAscending) {
// UIWindow itself is rotated from iOS 8.0
orientation = UIInterfaceOrientationPortrait;
}
switch (orientation) {
case UIInterfaceOrientationUnknown:
case UIInterfaceOrientationPortrait: {
[toastView setTransform:CGAffineTransformMakeRotation(0)];
[windowConstraints addObject:[NSLayoutConstraint constraintWithItem:toastView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:parentView
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0]];
switch ([self gravity]) {
case DPToastGravityTop: {
CGFloat statusBarHeight = 0;
if ([parentView isKindOfClass:[UIWindow class]]) {
if (NO == [UIApplication sharedApplication].statusBarHidden) {
statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.height;
}
}
[windowConstraints addObject:[NSLayoutConstraint constraintWithItem:toastView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:parentView
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:ABS(self.yOffset) + statusBarHeight]];
}
break;
case DPToastGravityCenter: {
[windowConstraints addObject:[NSLayoutConstraint constraintWithItem:toastView
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:parentView
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:self.yOffset]];
}
break;
case DPToastGravityBottom: {
[windowConstraints addObject:[NSLayoutConstraint constraintWithItem:toastView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:parentView
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:(ABS(self.yOffset) * -1)]];
}
break;
}
}
break;
case UIInterfaceOrientationLandscapeLeft: {
[toastView setTransform:CGAffineTransformMakeRotation(-M_PI_2)];
[windowConstraints addObject:[NSLayoutConstraint constraintWithItem:toastView
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:parentView
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0.0]];
switch ([self gravity]) {
case DPToastGravityTop: {
CGFloat statusBarHeight = 0;
if ([parentView isKindOfClass:[UIWindow class]]) {
if (NO == [UIApplication sharedApplication].statusBarHidden) {
statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.width;
}
}
[windowConstraints addObject:[NSLayoutConstraint constraintWithItem:toastView
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:parentView
attribute:NSLayoutAttributeLeft
multiplier:1.0
constant:ABS(self.yOffset) + statusBarHeight]];
}
break;
case DPToastGravityCenter: {
[windowConstraints addObject:[NSLayoutConstraint constraintWithItem:toastView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:parentView
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:self.yOffset]];
}
break;
case DPToastGravityBottom: {
[windowConstraints addObject:[NSLayoutConstraint constraintWithItem:toastView
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual
toItem:parentView
attribute:NSLayoutAttributeRight
multiplier:1.0
constant:(ABS(self.yOffset) * -1)]];
}
break;
}
}
break;
case UIInterfaceOrientationLandscapeRight: {
[toastView setTransform:CGAffineTransformMakeRotation(M_PI_2)];
[windowConstraints addObject:[NSLayoutConstraint constraintWithItem:toastView
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:parentView
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0.0]];
switch ([self gravity]) {
case DPToastGravityTop: {
CGFloat statusBarHeight = 0;
if ([parentView isKindOfClass:[UIWindow class]]) {
if (NO == [UIApplication sharedApplication].statusBarHidden) {
statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.width;
}
}
[windowConstraints addObject:[NSLayoutConstraint constraintWithItem:toastView
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual
toItem:parentView
attribute:NSLayoutAttributeRight
multiplier:1.0
constant:-(ABS(self.yOffset) + statusBarHeight)]];
}
break;
case DPToastGravityCenter: {
[windowConstraints addObject:[NSLayoutConstraint constraintWithItem:toastView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:parentView
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:-self.yOffset]];
}
break;
case DPToastGravityBottom: {
[windowConstraints addObject:[NSLayoutConstraint constraintWithItem:toastView
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:parentView
attribute:NSLayoutAttributeLeft
multiplier:1.0
constant:ABS(self.yOffset)]];
}
break;
}
}
break;
case UIInterfaceOrientationPortraitUpsideDown: {
[toastView setTransform:CGAffineTransformMakeRotation(M_PI)];
[windowConstraints addObject:[NSLayoutConstraint constraintWithItem:toastView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:parentView
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0]];
switch ([self gravity]) {
case DPToastGravityTop: {
CGFloat statusBarHeight = 0;
if ([parentView isKindOfClass:[UIWindow class]]) {
if (NO == [UIApplication sharedApplication].statusBarHidden) {
statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.height;
}
}
[windowConstraints addObject:[NSLayoutConstraint constraintWithItem:toastView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:parentView
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:-(ABS(self.yOffset) + statusBarHeight)]];
}
break;
case DPToastGravityCenter: {
[windowConstraints addObject:[NSLayoutConstraint constraintWithItem:toastView
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:parentView
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:-(self.yOffset)]];
}
break;
case DPToastGravityBottom: {
[windowConstraints addObject:[NSLayoutConstraint constraintWithItem:toastView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:parentView
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:ABS(self.yOffset)]];
}
break;
}
}
break;
}
[parentView addConstraints:windowConstraints];
}
@end