-
Notifications
You must be signed in to change notification settings - Fork 318
Expand file tree
/
Copy pathWEPopoverController.m
More file actions
executable file
·337 lines (262 loc) · 10.2 KB
/
Copy pathWEPopoverController.m
File metadata and controls
executable file
·337 lines (262 loc) · 10.2 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
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
//
// WEPopoverController.m
// WEPopover
//
// Created by Werner Altewischer on 02/09/10.
// Copyright 2010 Werner IT Consultancy. All rights reserved.
//
#import "WEPopoverController.h"
#import "WEPopoverParentView.h"
#import "UIBarButtonItem+WEPopover.h"
#define FADE_DURATION 0.25
#define DIM_ALPHA 0.25
@interface WEPopoverController ()
@property (nonatomic, retain) UIView *dimView;
@end
@interface WEPopoverController(Private)
- (UIView *)keyView;
- (void)updateBackgroundPassthroughViews;
- (void)setView:(UIView *)v;
- (CGRect)displayAreaForView:(UIView *)theView;
- (WEPopoverContainerViewProperties *)defaultContainerViewProperties;
- (void)dismissPopoverAnimated:(BOOL)animated userInitiated:(BOOL)userInitiated;
@end
@implementation WEPopoverController
@synthesize contentViewController;
@synthesize popoverContentSize;
@synthesize popoverVisible;
@synthesize popoverArrowDirection;
@synthesize delegate;
@synthesize view;
@synthesize containerViewProperties;
@synthesize context;
@synthesize passthroughViews;
static NSUInteger customKeyViewIndex;
+ (void)setCustomKeyViewIndex:(NSUInteger)keyViewIndex
{
customKeyViewIndex = keyViewIndex;
}
- (id)init {
if ((self = [super init])) {
}
return self;
}
- (id)initWithContentViewController:(UIViewController *)viewController {
if ((self = [self init])) {
self.contentViewController = viewController;
}
return self;
}
- (void)dealloc {
[self dismissPopoverAnimated:NO];
[contentViewController release];
[containerViewProperties release];
[passthroughViews release];
self.context = nil;
self.dimView = nil;
[super ah_dealloc];
}
- (void)setContentViewController:(UIViewController *)vc {
if (vc != contentViewController) {
[contentViewController release];
contentViewController = [vc ah_retain];
popoverContentSize = CGSizeZero;
}
}
//Overridden setter to copy the passthroughViews to the background view if it exists already
- (void)setPassthroughViews:(NSArray *)array {
[passthroughViews release];
passthroughViews = nil;
if (array) {
passthroughViews = [[NSArray alloc] initWithArray:array];
}
[self updateBackgroundPassthroughViews];
}
- (void)fadeAnimationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)theContext {
if ([animationID isEqual:@"FadeIn"]) {
self.view.userInteractionEnabled = YES;
popoverVisible = YES;
[contentViewController viewDidAppear:YES];
} else {
popoverVisible = NO;
[contentViewController viewDidDisappear:YES];
[self.view removeFromSuperview];
self.view = nil;
[backgroundView removeFromSuperview];
[backgroundView release];
backgroundView = nil;
BOOL userInitiatedDismissal = [(__bridge NSNumber *)theContext boolValue];
if (userInitiatedDismissal) {
//Only send message to delegate in case the user initiated this event, which is if he touched outside the view
[delegate popoverControllerDidDismissPopover:self];
}
}
}
- (void)dismissPopoverAnimated:(BOOL)animated {
[self dismissPopoverAnimated:animated userInitiated:NO];
}
- (void)presentPopoverFromBarButtonItem:(UIBarButtonItem *)item
permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections
animated:(BOOL)animated {
UIView *v = [self keyView];
CGRect rect = [item frameInView:v];
return [self presentPopoverFromRect:rect inView:v permittedArrowDirections:arrowDirections animated:animated];
}
- (void)presentPopoverFromRect:(CGRect)rect
inView:(UIView *)theView
permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections
animated:(BOOL)animated {
[self dismissPopoverAnimated:NO];
//First force a load view for the contentViewController so the popoverContentSize is properly initialized
contentViewController.view = contentViewController.view;
if (CGSizeEqualToSize(popoverContentSize, CGSizeZero)) {
popoverContentSize = contentViewController.contentSizeForViewInPopover;
}
CGRect displayArea = [self displayAreaForView:theView];
WEPopoverContainerViewProperties *props = self.containerViewProperties ? self.containerViewProperties : [self defaultContainerViewProperties];
WEPopoverContainerView *containerView = [[[WEPopoverContainerView alloc] initWithSize:self.popoverContentSize anchorRect:rect displayArea:displayArea permittedArrowDirections:arrowDirections properties:props] autorelease];
popoverArrowDirection = containerView.arrowDirection;
UIView *keyView = self.keyView;
backgroundView = [[WETouchableView alloc] initWithFrame:keyView.bounds];
backgroundView.contentMode = UIViewContentModeScaleToFill;
backgroundView.autoresizingMask = ( UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleHeight |
UIViewAutoresizingFlexibleBottomMargin);
backgroundView.backgroundColor = [UIColor clearColor];
backgroundView.delegate = self;
[keyView addSubview:backgroundView];
containerView.frame = [theView convertRect:containerView.frame toView:backgroundView];
[backgroundView addSubview:containerView];
containerView.contentView = contentViewController.view;
containerView.autoresizingMask = ( UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin);
self.view = containerView;
[self updateBackgroundPassthroughViews];
[contentViewController viewWillAppear:animated];
[self.view becomeFirstResponder];
[_dimView removeFromSuperview];
self.dimView = [[[UIView alloc] initWithFrame:keyView.bounds] autorelease];
_dimView.backgroundColor = [UIColor blackColor];
[keyView insertSubview:self.dimView belowSubview:backgroundView];
if (animated) {
self.view.alpha = 0.0;
_dimView.alpha = 0.0;
[UIView beginAnimations:@"FadeIn" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(fadeAnimationDidStop:finished:context:)];
[UIView setAnimationDuration:FADE_DURATION];
self.view.alpha = 1.0;
_dimView.alpha = DIM_ALPHA;
[UIView commitAnimations];
} else {
popoverVisible = YES;
_dimView.alpha = DIM_ALPHA;
[contentViewController viewDidAppear:animated];
}
}
- (void)repositionPopoverFromRect:(CGRect)rect
inView:(UIView *)theView
permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections {
CGRect displayArea = [self displayAreaForView:theView];
WEPopoverContainerView *containerView = (WEPopoverContainerView *)self.view;
[containerView updatePositionWithAnchorRect:rect
displayArea:displayArea
permittedArrowDirections:arrowDirections];
popoverArrowDirection = containerView.arrowDirection;
containerView.frame = [theView convertRect:containerView.frame toView:backgroundView];
}
#pragma mark -
#pragma mark WETouchableViewDelegate implementation
- (void)viewWasTouched:(WETouchableView *)view {
if (popoverVisible) {
if (!delegate || [delegate popoverControllerShouldDismissPopover:self]) {
[self dismissPopoverAnimated:YES userInitiated:YES];
}
}
}
@end
@implementation WEPopoverController(Private)
- (UIView *)keyView {
UIWindow *w = [[UIApplication sharedApplication] keyWindow];
if (w.subviews.count > 0) {
return [w.subviews objectAtIndex:customKeyViewIndex];
} else {
return w;
}
}
- (void)setView:(UIView *)v {
if (view != v) {
[view release];
view = [v ah_retain];
}
}
- (void)updateBackgroundPassthroughViews {
backgroundView.passthroughViews = passthroughViews;
}
- (void)dismissPopoverAnimated:(BOOL)animated userInitiated:(BOOL)userInitiated {
if (self.view) {
[contentViewController viewWillDisappear:animated];
popoverVisible = NO;
[self.view resignFirstResponder];
if (animated) {
self.view.userInteractionEnabled = NO;
[UIView beginAnimations:@"FadeOut" context:(__bridge void *)([NSNumber numberWithBool:userInitiated])];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(fadeAnimationDidStop:finished:context:)];
[UIView setAnimationDuration:FADE_DURATION];
self.view.alpha = 0.0;
_dimView.alpha = 0.0;
[UIView commitAnimations];
} else {
[contentViewController viewDidDisappear:animated];
[self.view removeFromSuperview];
self.view = nil;
[_dimView removeFromSuperview];
self.dimView = nil;
[backgroundView removeFromSuperview];
[backgroundView release];
backgroundView = nil;
}
}
}
- (CGRect)displayAreaForView:(UIView *)theView {
CGRect displayArea = CGRectZero;
if ([theView conformsToProtocol:@protocol(WEPopoverParentView)] && [theView respondsToSelector:@selector(displayAreaForPopover)]) {
displayArea = [(id <WEPopoverParentView>)theView displayAreaForPopover];
} else {
displayArea = [[[UIApplication sharedApplication] keyWindow] convertRect:[[UIScreen mainScreen] applicationFrame] toView:theView];
}
return displayArea;
}
//Enable to use the simple popover style
- (WEPopoverContainerViewProperties *)defaultContainerViewProperties {
WEPopoverContainerViewProperties *ret = [[[WEPopoverContainerViewProperties alloc] init] autorelease];
NSString *bgImageName = nil;
CGFloat bgMargin = 0.0;
CGFloat bgCapSize = 0.0;
CGFloat contentMargin = 4.0;
bgImageName = @"popoverBg.png";
// These constants are determined by the popoverBg.png image file and are image dependent
bgMargin = 13; // margin width of 13 pixels on all sides popoverBg.png (62 pixels wide - 36 pixel background) / 2 == 26 / 2 == 13
bgCapSize = 31; // ImageSize/2 == 62 / 2 == 31 pixels
ret.leftBgMargin = bgMargin;
ret.rightBgMargin = bgMargin;
ret.topBgMargin = bgMargin;
ret.bottomBgMargin = bgMargin;
ret.leftBgCapSize = bgCapSize;
ret.topBgCapSize = bgCapSize;
ret.bgImageName = bgImageName;
ret.leftContentMargin = contentMargin;
ret.rightContentMargin = contentMargin - 1; // Need to shift one pixel for border to look correct
ret.topContentMargin = contentMargin - 1;
ret.bottomContentMargin = contentMargin;
ret.upArrowImageName = @"popoverArrowUp.png";
ret.downArrowImageName = @"popoverArrowDown.png";
ret.leftArrowImageName = @"popoverArrowLeft.png";
ret.rightArrowImageName = @"popoverArrowRight.png";
return ret;
}
@end