-
Notifications
You must be signed in to change notification settings - Fork 0
/
MRGrowly.m
153 lines (127 loc) · 5.05 KB
/
MRGrowly.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
//
// MRGrowly.h
//
// Created by Benjamin de Jager on 4/18/11.
// Copyright 2011 M0rph3v5. All rights reserved.
//
#import "MRGrowly.h"
#import <QuartzCore/QuartzCore.h>
static MRGrowly *instance = nil;
@implementation MRGrowly
@synthesize targetView,
targetCorner,
isDeviceOrientationLandscape;
+(id)instance {
if(instance == nil) {
instance = [[super allocWithZone:NULL] init];
}
return instance;
}
+(id)allocWithZone:(NSZone*)zone {
return [[self instance] retain];
}
-(id)init {
if ((self = [super init])) {
growlies = [[NSMutableArray alloc] init];
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if (UIDeviceOrientationIsLandscape(orientation)) {
isDeviceOrientationLandscape = YES;
} else if (UIDeviceOrientationIsPortrait(orientation)) {
isDeviceOrientationLandscape = NO;
}
}
return self;
}
-(void)showNotificationWithMessage:(NSString*)message andDuration:(CGFloat)seconds {
CGFloat lastHeight;
if (targetCorner == MRGrowlyCornerBottomLeft || targetCorner == MRGrowlyCornerBottomRight) {
if (isDeviceOrientationLandscape) {
lastHeight = targetView.frame.size.width;
} else {
lastHeight = targetView.frame.size.height;
}
} else if (targetCorner == MRGrowlyCornerTopLeft || targetCorner == MRGrowlyCornerTopRight) {
lastHeight = -25;
}
CGFloat marginLeft;
CGFloat marginTop;
if (!targetView) {
[NSException raise:@"MRGrowlyTargetViewIssueNil"
format:@"A valid target view has not been set."];
}
switch (targetCorner) {
case MRGrowlyCornerTopLeft:
marginLeft = 20;
marginTop = 20;
break;
case MRGrowlyCornerTopRight:
if (isDeviceOrientationLandscape) {
marginLeft = targetView.frame.size.height - 220;
} else {
marginLeft = targetView.frame.size.width - 220;
}
marginTop = 20;
break;
case MRGrowlyCornerBottomLeft:
marginLeft = 20;
if (isDeviceOrientationLandscape) {
marginTop = targetView.frame.size.width - 60;
} else {
marginTop = targetView.frame.size.height - 60;
}
break;
case MRGrowlyCornerBottomRight:
if (isDeviceOrientationLandscape) {
marginLeft = targetView.frame.size.height - 220;
marginTop = targetView.frame.size.width - 60;
} else {
marginLeft = targetView.frame.size.width - 220;
marginTop = targetView.frame.size.height - 60;
}
break;
default:
[NSException raise:@"MRGrowlyCornerIssueNil"
format:@"A valid target corner has not been set."];
break;
}
if ([growlies count] > 0) {
UIView *lastView = [growlies lastObject];
lastHeight = lastView.frame.origin.y;
}
UIView *growlyView = [[UIView alloc] init];
if (targetCorner == MRGrowlyCornerBottomLeft || targetCorner == MRGrowlyCornerBottomRight) {
[growlyView setFrame:CGRectMake(marginLeft, lastHeight - 45, 200, 40)];
} else if (targetCorner == MRGrowlyCornerTopLeft || targetCorner == MRGrowlyCornerTopRight) {
[growlyView setFrame:CGRectMake(marginLeft, marginTop + (25 + lastHeight), 200, 40)];
}
[growlyView setAlpha:0.0f];
[growlyView setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.3]];
[growlyView.layer setCornerRadius:5];
[growlyView.layer setShadowOffset:CGSizeMake(0, 0)];
[growlyView.layer setShadowRadius:5];
[growlyView.layer setShadowOpacity:0.5];
[growlyView.layer setMasksToBounds:NO];
[growlyView.layer setShadowPath:[UIBezierPath bezierPathWithRect:growlyView.bounds].CGPath];
[growlies addObject:growlyView];
UILabel *messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 180, 20)];
[messageLabel setBackgroundColor:[UIColor clearColor]];
[messageLabel setText:message];
[messageLabel setTextColor:[UIColor whiteColor]];
[growlyView addSubview:messageLabel];
[messageLabel release];
[targetView addSubview:growlyView];
[UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
[growlyView setAlpha:1.0f];
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.5 delay:seconds options:UIViewAnimationOptionAllowUserInteraction animations:^{
[growlyView setAlpha:0.0f];
} completion:^(BOOL finished) {
if (finished) {
[growlies removeObject:growlyView];
[growlyView removeFromSuperview];
[growlyView release];
}
}];
}];
}
@end