forked from jessegrosjean/NOTTaskPaperForIOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHUDBackgroundView.m
More file actions
executable file
·292 lines (237 loc) · 7.77 KB
/
Copy pathHUDBackgroundView.m
File metadata and controls
executable file
·292 lines (237 loc) · 7.77 KB
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
//
// HUDView.m
// PlainText
//
// Created by Jesse Grosjean on 4/28/11.
//
#import "HUDBackgroundView.h"
#import "ApplicationViewController.h"
#import "ApplicationController.h"
#import "ApplicationView.h"
@interface HUDBorderView : UIView {
}
@end
@implementation HUDBorderView
- (void)refreshFromDefaults {
[self setNeedsDisplay];
}
- (id)init {
self = [super init];
if (self) {
self.opaque = NO;
CALayer *layer = self.layer;
if ([layer respondsToSelector:@selector(setShadowOffset:)]) {
layer.shadowOffset = CGSizeMake(0, 10);
layer.shadowRadius = 15;
layer.shadowOpacity = 0.25;
}
[self refreshFromDefaults];
}
return self;
}
#pragma mark Drawing
- (void)setFrame:(CGRect)frame {
[super setFrame:frame];
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect) rect {
[[UIColor clearColor] set];
UIRectFill(rect);
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect bounds = self.bounds;
[[APP_VIEW_CONTROLLER inkColorByPercent:0.03] set];
CGContextAddRoundedRect(context, bounds, 10);
CGContextFillPath(context);
[[APP_VIEW_CONTROLLER inkColorByPercent:0.8] set];
bounds.origin.x += 0.5;
bounds.origin.y += 0.5;
bounds.size.width -= 1.0;
bounds.size.height -= 1.0;
CGContextAddRoundedRect(context, bounds, 10);
CGContextSetLineWidth(context, 1);
CGContextDrawPath(context, kCGPathStroke);
}
@end
@implementation HUDBackgroundView
- (void)refreshFromDefaults {
//self.backgroundColor = [[APP_VIEW_CONTROLLER paperColor] colorWithAlphaComponent:0.5];
[self setNeedsLayout];
}
- (id)init {
self = [super init];
if (self) {
[self refreshFromDefaults];
self.opaque = NO;
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.borderInset = CGSizeMake(-10, -10);
//self.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.5];
hudBorderView = [[[HUDBorderView alloc] init] autorelease];
[self addSubview:hudBorderView];
[self addSubview:self.hudView];
}
return self;
}
- (void)dealloc {
[anchorView release];
[super dealloc];
}
@synthesize anchorRelativePosition;
@synthesize offsetPosition;
@synthesize borderInset;
@synthesize anchorView;
@synthesize hudView;
- (UIView *)hudView {
return nil;
}
- (void)show {
[self refreshSelfAndSubviewsFromDefaults];
[[UIMenuController sharedMenuController] setMenuVisible:NO animated:YES];
showing++;
[self setNeedsLayout];
[self setNeedsDisplay];
self.alpha = 0.0;
[(ApplicationView *)[APP_VIEW_CONTROLLER view] setHUDBackgroundView:self];
[self layoutSubviews];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.1];
self.alpha = 1.0;
[UIView commitAnimations];
self.hudView.userInteractionEnabled = YES;
[self.hudView becomeFirstResponder];
if ([self.hudView respondsToSelector:@selector(flashScrollIndicators)]) {
[self.hudView performSelector:@selector(flashScrollIndicators)];
}
}
- (void)close {
showing--;
self.hudView.userInteractionEnabled = NO;
[[NSRunLoop currentRunLoop] cancelPerformSelectorsWithTarget:self.hudView];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(closeAnimationDidStop:finished:context:)];
[UIView setAnimationDuration:0.1];
self.alpha = 0.0;
offsetPosition = CGPointMake(0, 0);
[UIView commitAnimations];
}
- (void)closeIfShowing {
if (showing > 0) {
[self close];
}
}
- (void)didClose {
[(ApplicationView *)[APP_VIEW_CONTROLLER view] setHUDBackgroundView:nil];
}
- (void)closeAnimationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
//if (showing == 0) {
[self didClose];
//}
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self close];
}
- (void)setNeedsLayout {
[super setNeedsLayout];
}
- (void)layoutSubviews {
if (!anchorView) {
anchorView = [[APP_VIEW_CONTROLLER view] retain];
}
CGRect anchoreBounds = [self convertRect:[anchorView convertRect:anchorView.bounds toView:nil] fromView:nil];
if (anchorView == [APP_VIEW_CONTROLLER view]) {
CGFloat keyboardHeight = [APP_VIEW_CONTROLLER keyboardHeight];
anchoreBounds.size.height -= keyboardHeight;
//if (keyboardHeight > 0) {
// bounds.size.height -= (keyboardHeight - [APP_VIEW_CONTROLLER adsHeight]);
// if (!IS_IPAD && !toolbar.hidden) {
// bounds.size.height += toolbar.frame.size.height;
// }
// }
}
anchoreBounds.origin.x += offsetPosition.x;
anchoreBounds.origin.y += offsetPosition.y;
hudViewFrame.origin = CGPointMake(0, 0);
hudViewFrame = CGRectInset(hudViewFrame, 0, -5);
hudViewFrame.origin = anchoreBounds.origin;
switch (anchorRelativePosition) {
case PositionUp:
hudViewFrame.origin.x -= CGRectGetMidX(hudViewFrame) - CGRectGetMidX(anchoreBounds);
hudViewFrame.origin.y -= hudViewFrame.size.height;
break;
case PositionDown:
hudViewFrame.origin.x -= CGRectGetMidX(hudViewFrame) - CGRectGetMidX(anchoreBounds);
hudViewFrame.origin.y += anchoreBounds.size.height;
break;
case PositionLeft:
break;
case PositionRight:
break;
case PositionUpLeft:
hudViewFrame.origin.y -= hudViewFrame.size.height;
hudViewFrame.origin.x -= hudViewFrame.size.width - anchoreBounds.size.width;
break;
case PositionUpRight:
hudViewFrame.origin.y -= hudViewFrame.size.height;
break;
case PositionDownLeft:
hudViewFrame.origin.x -= hudViewFrame.size.width - anchoreBounds.size.width;
hudViewFrame.origin.y += anchoreBounds.size.height;
break;
case PositionDownRight:
hudViewFrame.origin.y += anchoreBounds.size.height;
break;
case PositionCentered:
hudViewFrame.origin.x -= CGRectGetMidX(hudViewFrame) - CGRectGetMidX(anchoreBounds);
hudViewFrame.origin.y -= CGRectGetMidY(hudViewFrame) - CGRectGetMidY(anchoreBounds);
}
CGRect bounds = self.bounds;
UIWindow *selfWindow = self.window;
for (UIWindow *eachWindow in [[UIApplication sharedApplication] windows]) {
if (eachWindow != selfWindow) {
if (!eachWindow.hidden) {
for (UIView *eachView in eachWindow.subviews) {
if (!eachView.hidden) {
CGRect eachRect = CGRectInset([eachView convertRect:eachView.bounds toView:self], -5, -5);
if (CGRectGetMinY(eachRect) < CGRectGetMaxY(hudViewFrame)) {
hudViewFrame.size.height -= CGRectGetMaxY(hudViewFrame) - CGRectGetMinY(eachRect);
}
}
}
}
}
}
hudViewFrame = CGRectIntersection(hudViewFrame, CGRectInset(bounds, 5, 5));
if (![[UIApplication sharedApplication] isStatusBarHidden]) {
if (hudViewFrame.origin.y < 20) {
hudViewFrame.size.height -= 20 - hudViewFrame.origin.y;
hudViewFrame.origin.y = 20;
}
}
hudViewFrame = CGRectInset(hudViewFrame, 0, 5);
hudViewFrame = CGRectIntegral(hudViewFrame);
if (!CGRectEqualToRect(hudViewFrame, self.hudView.frame)) {
self.hudView.frame = hudViewFrame;
}
hudBorderView.frame = CGRectInset(self.hudView.frame, borderInset.width, borderInset.height);
}
#pragma mark -
#pragma mark Keyboard delegate
- (void)didMoveToSuperview {
[super didMoveToSuperview];
if ([self superview]) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:KeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:KeyboardWillHideNotification object:nil];
} else {
[[NSNotificationCenter defaultCenter] removeObserver:self name:KeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:KeyboardWillHideNotification object:nil];
}
}
- (void)keyboardWillShow:(NSNotification *)aNotification {
[self setNeedsLayout];
}
- (void)keyboardWillHide:(NSNotification *)aNotification {
[self setNeedsLayout];
}
@end