-
Notifications
You must be signed in to change notification settings - Fork 318
Expand file tree
/
Copy pathWEPopoverContainerView.m
More file actions
executable file
·371 lines (291 loc) · 14.5 KB
/
Copy pathWEPopoverContainerView.m
File metadata and controls
executable file
·371 lines (291 loc) · 14.5 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
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
//
// WEPopoverContainerViewProperties.m
// WEPopover
//
// Created by Werner Altewischer on 02/09/10.
// Copyright 2010 Werner IT Consultancy. All rights reserved.
//
#import "WEPopoverContainerView.h"
@implementation WEPopoverContainerViewProperties
@synthesize bgImageName, upArrowImageName, downArrowImageName, leftArrowImageName, rightArrowImageName, topBgMargin, bottomBgMargin, leftBgMargin, rightBgMargin, topBgCapSize, leftBgCapSize;
@synthesize leftContentMargin, rightContentMargin, topContentMargin, bottomContentMargin, arrowMargin;
- (void)dealloc {
self.bgImageName = nil;
self.upArrowImageName = nil;
self.downArrowImageName = nil;
self.leftArrowImageName = nil;
self.rightArrowImageName = nil;
[super dealloc];
}
@end
@interface WEPopoverContainerView(Private)
- (void)determineGeometryForSize:(CGSize)theSize anchorRect:(CGRect)anchorRect displayArea:(CGRect)displayArea permittedArrowDirections:(UIPopoverArrowDirection)permittedArrowDirections;
- (CGRect)contentRect;
- (CGSize)contentSize;
- (void)setProperties:(WEPopoverContainerViewProperties *)props;
- (void)initFrame;
@end
@implementation WEPopoverContainerView
@synthesize arrowDirection, contentView;
- (id)initWithSize:(CGSize)theSize
anchorRect:(CGRect)anchorRect
displayArea:(CGRect)displayArea
permittedArrowDirections:(UIPopoverArrowDirection)permittedArrowDirections
properties:(WEPopoverContainerViewProperties *)theProperties {
if ((self = [super initWithFrame:CGRectZero])) {
[self setProperties:theProperties];
correctedSize = CGSizeMake(theSize.width + properties.leftBgMargin + properties.rightBgMargin + properties.leftContentMargin + properties.rightContentMargin,
theSize.height + properties.topBgMargin + properties.bottomBgMargin + properties.topContentMargin + properties.bottomContentMargin);
[self determineGeometryForSize:correctedSize anchorRect:anchorRect displayArea:displayArea permittedArrowDirections:permittedArrowDirections];
[self initFrame];
self.backgroundColor = [UIColor clearColor];
UIImage *theImage = [UIImage imageNamed:properties.bgImageName];
bgImage = [[theImage stretchableImageWithLeftCapWidth:properties.leftBgCapSize topCapHeight:properties.topBgCapSize] retain];
self.clipsToBounds = YES;
self.userInteractionEnabled = YES;
}
return self;
}
- (void)dealloc {
[properties release];
[contentView release];
[bgImage release];
[arrowImage release];
[super dealloc];
}
- (void)drawRect:(CGRect)rect {
[bgImage drawInRect:bgRect blendMode:kCGBlendModeNormal alpha:1.0];
[arrowImage drawInRect:arrowRect blendMode:kCGBlendModeNormal alpha:1.0];
}
- (void)updatePositionWithSize:(CGSize)theSize
anchorRect:(CGRect)anchorRect
displayArea:(CGRect)displayArea
permittedArrowDirections:(UIPopoverArrowDirection)permittedArrowDirections {
correctedSize = CGSizeMake(theSize.width + properties.leftBgMargin + properties.rightBgMargin + properties.leftContentMargin + properties.rightContentMargin,
theSize.height + properties.topBgMargin + properties.bottomBgMargin + properties.topContentMargin + properties.bottomContentMargin);
[self determineGeometryForSize:correctedSize anchorRect:anchorRect displayArea:displayArea permittedArrowDirections:permittedArrowDirections];
[self initFrame];
[self setNeedsDisplay];
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
return CGRectContainsPoint(self.contentRect, point);
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
}
- (void)setContentView:(UIView *)v {
if (v != contentView) {
[contentView release];
contentView = [v retain];
contentView.frame = self.contentRect;
[self addSubview:contentView];
}
}
@end
@implementation WEPopoverContainerView(Private)
- (void)initFrame {
CGRect theFrame = CGRectOffset(CGRectUnion(bgRect, arrowRect), offset.x, offset.y);
//If arrow rect origin is < 0 the frame above is extended to include it so we should offset the other rects
arrowOffset = CGPointMake(MAX(0, -arrowRect.origin.x), MAX(0, -arrowRect.origin.y));
bgRect = CGRectOffset(bgRect, arrowOffset.x, arrowOffset.y);
arrowRect = CGRectOffset(arrowRect, arrowOffset.x, arrowOffset.y);
self.frame = CGRectIntegral(theFrame);
}
- (CGSize)contentSize {
return self.contentRect.size;
}
- (CGRect)contentRect {
CGRect rect = CGRectMake(properties.leftBgMargin + properties.leftContentMargin + arrowOffset.x,
properties.topBgMargin + properties.topContentMargin + arrowOffset.y,
bgRect.size.width - properties.leftBgMargin - properties.rightBgMargin - properties.leftContentMargin - properties.rightContentMargin,
bgRect.size.height - properties.topBgMargin - properties.bottomBgMargin - properties.topContentMargin - properties.bottomContentMargin);
return rect;
}
- (void)setProperties:(WEPopoverContainerViewProperties *)props {
if (properties != props) {
[properties release];
properties = [props retain];
}
}
- (void)determineGeometryForSize:(CGSize)theSize anchorRect:(CGRect)anchorRect displayArea:(CGRect)displayArea permittedArrowDirections:(UIPopoverArrowDirection)supportedArrowDirections {
//Determine the frame, it should not go outside the display area
UIPopoverArrowDirection theArrowDirection = UIPopoverArrowDirectionUp;
offset = CGPointZero;
bgRect = CGRectZero;
arrowRect = CGRectZero;
arrowDirection = UIPopoverArrowDirectionUnknown;
CGFloat biggestSurface = 0.0f;
CGFloat currentMinMargin = 0.0f;
UIImage *upArrowImage = [UIImage imageNamed:properties.upArrowImageName];
UIImage *downArrowImage = [UIImage imageNamed:properties.downArrowImageName];
UIImage *leftArrowImage = [UIImage imageNamed:properties.leftArrowImageName];
UIImage *rightArrowImage = [UIImage imageNamed:properties.rightArrowImageName];
while (theArrowDirection <= UIPopoverArrowDirectionRight) {
if ((supportedArrowDirections & theArrowDirection)) {
CGRect theBgRect = CGRectMake(0, 0, theSize.width, theSize.height);
CGRect theArrowRect = CGRectZero;
CGPoint theOffset = CGPointZero;
CGFloat xArrowOffset = 0.0;
CGFloat yArrowOffset = 0.0;
CGPoint anchorPoint = CGPointZero;
switch (theArrowDirection) {
case UIPopoverArrowDirectionUp:
anchorPoint = CGPointMake(CGRectGetMidX(anchorRect) - displayArea.origin.x, CGRectGetMaxY(anchorRect) - displayArea.origin.y);
xArrowOffset = theSize.width / 2 - upArrowImage.size.width / 2;
yArrowOffset = properties.topBgMargin - upArrowImage.size.height;
theOffset = CGPointMake(anchorPoint.x - xArrowOffset - upArrowImage.size.width / 2, anchorPoint.y - yArrowOffset);
if (theOffset.x < 0) {
xArrowOffset += theOffset.x;
theOffset.x = 0;
} else if (theOffset.x + theSize.width > displayArea.size.width) {
xArrowOffset += (theOffset.x + theSize.width - displayArea.size.width);
theOffset.x = displayArea.size.width - theSize.width;
}
//Cap the arrow offset
xArrowOffset = MAX(xArrowOffset, properties.leftBgMargin + properties.arrowMargin);
xArrowOffset = MIN(xArrowOffset, theSize.width - properties.rightBgMargin - properties.arrowMargin - upArrowImage.size.width);
//x offset should be round to avoid blurring
xArrowOffset = roundf(xArrowOffset);
theArrowRect = CGRectMake(xArrowOffset, yArrowOffset, upArrowImage.size.width, upArrowImage.size.height);
break;
case UIPopoverArrowDirectionDown:
anchorPoint = CGPointMake(CGRectGetMidX(anchorRect) - displayArea.origin.x, CGRectGetMinY(anchorRect) - displayArea.origin.y);
xArrowOffset = theSize.width / 2 - downArrowImage.size.width / 2;
yArrowOffset = theSize.height - properties.bottomBgMargin;
theOffset = CGPointMake(anchorPoint.x - xArrowOffset - downArrowImage.size.width / 2, anchorPoint.y - yArrowOffset - downArrowImage.size.height);
if (theOffset.x < 0) {
xArrowOffset += theOffset.x;
theOffset.x = 0;
} else if (theOffset.x + theSize.width > displayArea.size.width) {
xArrowOffset += (theOffset.x + theSize.width - displayArea.size.width);
theOffset.x = displayArea.size.width - theSize.width;
}
//Cap the arrow offset
xArrowOffset = MAX(xArrowOffset, properties.leftBgMargin + properties.arrowMargin);
xArrowOffset = MIN(xArrowOffset, theSize.width - properties.rightBgMargin - properties.arrowMargin - downArrowImage.size.width);
//x offset should be round to avoid blurring
xArrowOffset = roundf(xArrowOffset);
theArrowRect = CGRectMake(xArrowOffset , yArrowOffset, downArrowImage.size.width, downArrowImage.size.height);
break;
case UIPopoverArrowDirectionLeft:
anchorPoint = CGPointMake(CGRectGetMaxX(anchorRect) - displayArea.origin.x, CGRectGetMidY(anchorRect) - displayArea.origin.y);
xArrowOffset = properties.leftBgMargin - leftArrowImage.size.width;
yArrowOffset = theSize.height / 2 - leftArrowImage.size.height / 2;
theOffset = CGPointMake(anchorPoint.x - xArrowOffset, anchorPoint.y - yArrowOffset - leftArrowImage.size.height / 2);
if (theOffset.y < 0) {
yArrowOffset += theOffset.y;
theOffset.y = 0;
} else if (theOffset.y + theSize.height > displayArea.size.height) {
yArrowOffset += (theOffset.y + theSize.height - displayArea.size.height);
theOffset.y = displayArea.size.height - theSize.height;
}
//Cap the arrow offset
yArrowOffset = MAX(yArrowOffset, properties.topBgMargin + properties.arrowMargin);
yArrowOffset = MIN(yArrowOffset, theSize.height - properties.bottomBgMargin - properties.arrowMargin - leftArrowImage.size.height);
//y offset should be round to avoid blurring
yArrowOffset = roundf(yArrowOffset);
theArrowRect = CGRectMake(xArrowOffset, yArrowOffset, leftArrowImage.size.width, leftArrowImage.size.height);
break;
case UIPopoverArrowDirectionRight:
anchorPoint = CGPointMake(CGRectGetMinX(anchorRect) - displayArea.origin.x, CGRectGetMidY(anchorRect) - displayArea.origin.y);
xArrowOffset = theSize.width - properties.rightBgMargin;
yArrowOffset = theSize.height / 2 - rightArrowImage.size.width / 2;
theOffset = CGPointMake(anchorPoint.x - xArrowOffset - rightArrowImage.size.width, anchorPoint.y - yArrowOffset - rightArrowImage.size.height / 2);
if (theOffset.y < 0) {
yArrowOffset += theOffset.y;
theOffset.y = 0;
} else if (theOffset.y + theSize.height > displayArea.size.height) {
yArrowOffset += (theOffset.y + theSize.height - displayArea.size.height);
theOffset.y = displayArea.size.height - theSize.height;
}
//Cap the arrow offset
yArrowOffset = MAX(yArrowOffset, properties.topBgMargin + properties.arrowMargin);
yArrowOffset = MIN(yArrowOffset, theSize.height - properties.bottomBgMargin - properties.arrowMargin - rightArrowImage.size.height);
//y offset should be round to avoid blurring
yArrowOffset = roundf(yArrowOffset);
theArrowRect = CGRectMake(xArrowOffset, yArrowOffset, rightArrowImage.size.width, rightArrowImage.size.height);
break;
default:
break;
}
CGRect bgFrame = CGRectOffset(theBgRect, theOffset.x, theOffset.y);
CGFloat minMarginLeft = CGRectGetMinX(bgFrame);
CGFloat minMarginRight = CGRectGetWidth(displayArea) - CGRectGetMaxX(bgFrame);
CGFloat minMarginTop = CGRectGetMinY(bgFrame);
CGFloat minMarginBottom = CGRectGetHeight(displayArea) - CGRectGetMaxY(bgFrame);
if (minMarginLeft < 0) {
// Popover is too wide and clipped on the left; decrease width
// and move it to the right
theOffset.x -= minMarginLeft;
theBgRect.size.width += minMarginLeft;
minMarginLeft = 0;
if (theArrowDirection == UIPopoverArrowDirectionRight) {
theArrowRect.origin.x = CGRectGetMaxX(theBgRect) - properties.rightBgMargin;
}
}
if (minMarginRight < 0) {
// Popover is too wide and clipped on the right; decrease width.
theBgRect.size.width += minMarginRight;
minMarginRight = 0;
if (theArrowDirection == UIPopoverArrowDirectionLeft) {
theArrowRect.origin.x = CGRectGetMinX(theBgRect) - leftArrowImage.size.width + properties.leftBgMargin;
}
}
if (minMarginTop < 0) {
// Popover is too high and clipped at the top; decrease height
// and move it down
theOffset.y -= minMarginTop;
theBgRect.size.height += minMarginTop;
minMarginTop = 0;
if (theArrowDirection == UIPopoverArrowDirectionDown) {
theArrowRect.origin.y = CGRectGetMaxY(theBgRect) - properties.bottomBgMargin;
}
}
if (minMarginBottom < 0) {
// Popover is too high and clipped at the bottom; decrease height.
theBgRect.size.height += minMarginBottom;
minMarginBottom = 0;
if (theArrowDirection == UIPopoverArrowDirectionUp) {
theArrowRect.origin.y = CGRectGetMinY(theBgRect) - upArrowImage.size.height + properties.topBgMargin;
}
}
bgFrame = CGRectOffset(theBgRect, theOffset.x, theOffset.y);
CGFloat minMargin = MIN(minMarginLeft, minMarginRight);
minMargin = MIN(minMargin, minMarginTop);
minMargin = MIN(minMargin, minMarginBottom);
// Calculate intersection and surface
CGFloat surface = theBgRect.size.width * theBgRect.size.height;
if (surface >= biggestSurface && minMargin >= currentMinMargin) {
biggestSurface = surface;
offset = CGPointMake(theOffset.x + displayArea.origin.x, theOffset.y + displayArea.origin.y);
arrowRect = theArrowRect;
bgRect = theBgRect;
arrowDirection = theArrowDirection;
currentMinMargin = minMargin;
}
}
theArrowDirection <<= 1;
}
switch (arrowDirection) {
case UIPopoverArrowDirectionUp:
arrowImage = [upArrowImage retain];
break;
case UIPopoverArrowDirectionDown:
arrowImage = [downArrowImage retain];
break;
case UIPopoverArrowDirectionLeft:
arrowImage = [leftArrowImage retain];
break;
case UIPopoverArrowDirectionRight:
arrowImage = [rightArrowImage retain];
break;
default:
break;
}
}
@end