This repository has been archived by the owner on Oct 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathDDPopoverBackgroundView.m
executable file
·453 lines (327 loc) · 12 KB
/
DDPopoverBackgroundView.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
//
// DDPopoverBackgroundView.m
// /~https://github.com/ddebin/DDPopoverBackgroundView
//
//
// ARC Helper
//
// Version 2.2
//
// Created by Nick Lockwood on 05/01/2012.
// Copyright 2012 Charcoal Design
//
// Distributed under the permissive zlib license
// Get the latest version from here:
//
// https://gist.github.com/1563325
//
#import <Availability.h>
#undef ah_retain
#undef ah_dealloc
#undef ah_autorelease
#undef ah_dealloc
#if __has_feature(objc_arc)
#define ah_retain self
#define ah_release self
#define ah_autorelease self
#define ah_dealloc self
#else
#define ah_retain retain
#define ah_release release
#define ah_autorelease autorelease
#define ah_dealloc dealloc
#endif
// ARC Helper ends
#import <QuartzCore/QuartzCore.h>
#import "DDPopoverBackgroundView.h"
#define DEFAULT_ARROW_BASE 35.0f
static CGFloat s_ArrowBase = DEFAULT_ARROW_BASE;
#define DEFAULT_ARROW_HEIGHT 19.0f
static CGFloat s_ArrowHeight = DEFAULT_ARROW_HEIGHT;
#define BKG_IMAGE_SIZE 40.0f
#define BKG_IMAGE_CORNER_RADIUS 8.0f
static CGFloat s_BackgroundImageCornerRadius = BKG_IMAGE_CORNER_RADIUS;
#define BKG_IMAGE_CAPINSET (s_BackgroundImageCornerRadius * 2.0f)
#define TOP_CONTENT_INSET s_ContentInset
#define LEFT_CONTENT_INSET s_ContentInset
#define BOTTOM_CONTENT_INSET s_ContentInset
#define RIGHT_CONTENT_INSET s_ContentInset
#define DEFAULT_CONTENT_INSET 9.0f
static CGFloat s_ContentInset = DEFAULT_CONTENT_INSET;
#define DEFAULT_TINT_COLOR [UIColor blackColor]
static UIColor *s_TintColor = nil;
#define DEFAULT_SHADOW_ENABLED YES
static BOOL s_ShadowEnabled = DEFAULT_SHADOW_ENABLED;
static UIImage *s_DefaultTopArrowImage = nil;
static UIImage *s_DefaultLeftArrowImage = nil;
static UIImage *s_DefaultRightArrowImage = nil;
static UIImage *s_DefaultBottomArrowImage = nil;
static UIImage *s_DefaultBackgroundImage = nil;
#pragma mark - Implementation
@implementation DDPopoverBackgroundView
@synthesize arrowOffset, arrowDirection;
#pragma mark - Overriden class methods
/**
Return the width of the arrow triangle at its base.
*/
+ (CGFloat)arrowBase
{
return s_ArrowBase;
}
/**
Return the height of the arrow (measured in points) from its base to its tip.
*/
+ (CGFloat)arrowHeight
{
return s_ArrowHeight;
}
/**
Return the insets for the content portion of the popover.
*/
+ (UIEdgeInsets)contentViewInsets
{
return UIEdgeInsetsMake(TOP_CONTENT_INSET, LEFT_CONTENT_INSET, BOTTOM_CONTENT_INSET, RIGHT_CONTENT_INSET);
}
#pragma mark - Custom setters for updating layout
/*
Whenever arrow changes direction or position, layout subviews will be called
in order to update arrow and background frames
*/
- (void)setArrowOffset:(CGFloat)_arrowOffset
{
arrowOffset = _arrowOffset;
[self setNeedsLayout];
}
- (void)setArrowDirection:(UIPopoverArrowDirection)_arrowDirection
{
arrowDirection = _arrowDirection;
[self setNeedsLayout];
}
#pragma mark - Global statics setters
+ (void)setBackgroundImageCornerRadius:(CGFloat)cornerRadius
{
s_BackgroundImageCornerRadius = cornerRadius;
}
+ (void)setContentInset:(CGFloat)contentInset
{
s_ContentInset = contentInset;
}
+ (void)setTintColor:(UIColor *)tintColor
{
[s_TintColor ah_release];
s_TintColor = [tintColor ah_retain];
}
+ (void)setShadowEnabled:(BOOL)shadowEnabled
{
s_ShadowEnabled = shadowEnabled;
}
+ (void)setArrowBase:(CGFloat)arrowBase
{
s_ArrowBase = arrowBase;
}
+ (void)setArrowHeight:(CGFloat)arrowHeight
{
s_ArrowHeight = arrowHeight;
}
+ (void)setBackgroundImage:(UIImage *)background top:(UIImage *)top right:(UIImage *)right bottom:(UIImage *)bottom left:(UIImage *)left
{
[s_DefaultBackgroundImage ah_release];
s_DefaultBackgroundImage = [background ah_retain];
[s_DefaultTopArrowImage ah_release];
s_DefaultTopArrowImage = [top ah_retain];
[s_DefaultRightArrowImage ah_release];
s_DefaultRightArrowImage = [right ah_retain];
[s_DefaultBottomArrowImage ah_release];
s_DefaultBottomArrowImage = [bottom ah_retain];
[s_DefaultLeftArrowImage ah_release];
s_DefaultLeftArrowImage = [left ah_retain];
}
#pragma mark - Initialization
- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame])
{
if ((s_DefaultBackgroundImage == nil) || (s_DefaultTopArrowImage == nil) || (s_DefaultRightArrowImage == nil) || (s_DefaultBottomArrowImage == nil) || (s_DefaultLeftArrowImage == nil))
{
if (s_TintColor == nil) s_TintColor = [DEFAULT_TINT_COLOR ah_retain];
[DDPopoverBackgroundView buildArrowImagesWithTintColor:s_TintColor];
}
[popoverBackgroundImageView ah_release];
popoverBackgroundImageView = [[UIImageView alloc] initWithImage:s_DefaultBackgroundImage];
[self addSubview:popoverBackgroundImageView];
[arrowImageView ah_release];
arrowImageView = [[UIImageView alloc] init];
[self addSubview:arrowImageView];
if (s_ShadowEnabled)
{
popoverBackgroundImageView.layer.shadowColor = [UIColor blackColor].CGColor;
popoverBackgroundImageView.layer.shadowOpacity = 0.4f;
popoverBackgroundImageView.layer.shadowRadius = 2.0f;
popoverBackgroundImageView.layer.shadowOffset = CGSizeMake(0.0f, 1.5f);
arrowImageView.layer.shadowColor = [UIColor blackColor].CGColor;
arrowImageView.layer.shadowOpacity = 0.4f;
arrowImageView.layer.shadowRadius = 2.0f;
arrowImageView.layer.shadowOffset = CGSizeMake(0.0f, 1.5f);
arrowImageView.layer.masksToBounds = YES;
}
}
return self;
}
#if !__has_feature(objc_arc)
- (void)dealloc
{
[arrowImageView release];
[popoverBackgroundImageView release];
[super dealloc];
}
#endif
#pragma mark - Arrow images build
+ (void)rebuildArrowImages
{
[DDPopoverBackgroundView buildArrowImagesWithTintColor:s_TintColor];
}
+ (void)buildArrowImagesWithTintColor:(UIColor *)tintColor
{
UIBezierPath *arrowPath;
// top arrow
UIGraphicsBeginImageContextWithOptions(CGSizeMake(s_ArrowBase, s_ArrowHeight), NO, 0.0f);
arrowPath = [UIBezierPath bezierPath];
[arrowPath moveToPoint: CGPointMake(s_ArrowBase/2.0f, 0.0f)];
[arrowPath addLineToPoint:CGPointMake(s_ArrowBase, s_ArrowHeight)];
[arrowPath addLineToPoint:CGPointMake(0.0f, s_ArrowHeight)];
[arrowPath addLineToPoint:CGPointMake(s_ArrowBase/2.0f, 0.0f)];
[tintColor setFill];
[arrowPath fill];
[s_DefaultTopArrowImage ah_release];
s_DefaultTopArrowImage = [UIGraphicsGetImageFromCurrentImageContext() ah_retain];
UIGraphicsEndImageContext();
// bottom arrow
UIGraphicsBeginImageContextWithOptions(CGSizeMake(s_ArrowBase, s_ArrowHeight), NO, 0.0f);
arrowPath = [UIBezierPath bezierPath];
[arrowPath moveToPoint: CGPointMake(0.0f, 0.0f)];
[arrowPath addLineToPoint:CGPointMake(s_ArrowBase, 0.0f)];
[arrowPath addLineToPoint:CGPointMake(s_ArrowBase/2.0f, s_ArrowHeight)];
[arrowPath addLineToPoint:CGPointMake(0.0f, 0.0f)];
[tintColor setFill];
[arrowPath fill];
[s_DefaultBottomArrowImage ah_release];
s_DefaultBottomArrowImage = [UIGraphicsGetImageFromCurrentImageContext() ah_retain];
UIGraphicsEndImageContext();
// left arrow
UIGraphicsBeginImageContextWithOptions(CGSizeMake(s_ArrowHeight, s_ArrowBase), NO, 0.0f);
arrowPath = [UIBezierPath bezierPath];
[arrowPath moveToPoint: CGPointMake(s_ArrowHeight, 0.0f)];
[arrowPath addLineToPoint:CGPointMake(s_ArrowHeight, s_ArrowBase)];
[arrowPath addLineToPoint:CGPointMake(0.0f, s_ArrowBase/2.0f)];
[arrowPath addLineToPoint:CGPointMake(s_ArrowHeight, 0.0f)];
[tintColor setFill];
[arrowPath fill];
[s_DefaultLeftArrowImage ah_release];
s_DefaultLeftArrowImage = [UIGraphicsGetImageFromCurrentImageContext() ah_retain];
UIGraphicsEndImageContext();
// right arrow
UIGraphicsBeginImageContextWithOptions(CGSizeMake(s_ArrowHeight, s_ArrowBase), NO, 0.0f);
arrowPath = [UIBezierPath bezierPath];
[arrowPath moveToPoint: CGPointMake(0.0f, 0.0f)];
[arrowPath addLineToPoint:CGPointMake(s_ArrowHeight, s_ArrowBase/2.0f)];
[arrowPath addLineToPoint:CGPointMake(0.0f, s_ArrowBase)];
[arrowPath addLineToPoint:CGPointMake(0.0f, 0.0f)];
[tintColor setFill];
[arrowPath fill];
[s_DefaultRightArrowImage ah_release];
s_DefaultRightArrowImage = [UIGraphicsGetImageFromCurrentImageContext() ah_retain];
UIGraphicsEndImageContext();
// background
UIGraphicsBeginImageContextWithOptions(CGSizeMake(BKG_IMAGE_SIZE, BKG_IMAGE_SIZE), NO, 0.0f);
UIBezierPath *borderPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0.0f, 0.0f, BKG_IMAGE_SIZE, BKG_IMAGE_SIZE)
cornerRadius:s_BackgroundImageCornerRadius];
[tintColor setFill];
[borderPath fill];
UIEdgeInsets capInsets = UIEdgeInsetsMake(BKG_IMAGE_CAPINSET, BKG_IMAGE_CAPINSET, BKG_IMAGE_CAPINSET, BKG_IMAGE_CAPINSET);
[s_DefaultBackgroundImage ah_release];
s_DefaultBackgroundImage = [[UIGraphicsGetImageFromCurrentImageContext() resizableImageWithCapInsets:capInsets] ah_retain];
UIGraphicsEndImageContext();
}
#pragma mark - Layout subviews
- (void)layoutSubviews
{
CGFloat popoverImageOriginX = 0.0f;
CGFloat popoverImageOriginY = 0.0f;
CGFloat popoverImageWidth = self.bounds.size.width;
CGFloat popoverImageHeight = self.bounds.size.height;
CGFloat arrowImageOriginX = 0.0f;
CGFloat arrowImageOriginY = 0.0f;
CGFloat arrowImageWidth = s_ArrowBase;
CGFloat arrowImageHeight = s_ArrowHeight;
switch (self.arrowDirection)
{
case UIPopoverArrowDirectionUp:
popoverImageOriginY = s_ArrowHeight;
popoverImageHeight = self.bounds.size.height - s_ArrowHeight;
// Calculating arrow x position using arrow offset, arrow width and popover width
arrowImageOriginX = roundf((self.bounds.size.width - s_ArrowBase) / 2.0f + self.arrowOffset);
// If arrow image exceeds rounded corner arrow image x postion is adjusted
if ((arrowImageOriginX + s_ArrowBase) > (self.bounds.size.width - s_BackgroundImageCornerRadius))
{
arrowImageOriginX -= s_BackgroundImageCornerRadius;
}
if (arrowImageOriginX < s_BackgroundImageCornerRadius)
{
arrowImageOriginX += s_BackgroundImageCornerRadius;
}
// Setting arrow image for current arrow direction
arrowImageView.image = s_DefaultTopArrowImage;
break;
case UIPopoverArrowDirectionDown:
popoverImageHeight = self.bounds.size.height - s_ArrowHeight;
arrowImageOriginX = roundf((self.bounds.size.width - s_ArrowBase) / 2.0f + self.arrowOffset);
if ((arrowImageOriginX + s_ArrowBase) > (self.bounds.size.width - s_BackgroundImageCornerRadius))
{
arrowImageOriginX -= s_BackgroundImageCornerRadius;
}
if (arrowImageOriginX < s_BackgroundImageCornerRadius)
{
arrowImageOriginX += s_BackgroundImageCornerRadius;
}
arrowImageOriginY = popoverImageHeight;
arrowImageView.image = s_DefaultBottomArrowImage;
break;
case UIPopoverArrowDirectionLeft:
popoverImageOriginX = s_ArrowHeight;
popoverImageWidth = self.bounds.size.width - s_ArrowHeight;
arrowImageOriginY = roundf((self.bounds.size.height - s_ArrowBase) / 2.0f + self.arrowOffset);
if ((arrowImageOriginY + s_ArrowBase) > (self.bounds.size.height - s_BackgroundImageCornerRadius))
{
arrowImageOriginY -= s_BackgroundImageCornerRadius;
}
if (arrowImageOriginY < s_BackgroundImageCornerRadius)
{
arrowImageOriginY += s_BackgroundImageCornerRadius;
}
arrowImageWidth = s_ArrowHeight;
arrowImageHeight = s_ArrowBase;
arrowImageView.image = s_DefaultLeftArrowImage;
break;
case UIPopoverArrowDirectionRight:
popoverImageWidth = self.bounds.size.width - s_ArrowHeight;
arrowImageOriginX = popoverImageWidth;
arrowImageOriginY = roundf((self.bounds.size.height - s_ArrowBase) / 2.0f + self.arrowOffset);
if ((arrowImageOriginY + s_ArrowBase) > (self.bounds.size.height - s_BackgroundImageCornerRadius))
{
arrowImageOriginY -= s_BackgroundImageCornerRadius;
}
if (arrowImageOriginY < s_BackgroundImageCornerRadius)
{
arrowImageOriginY += s_BackgroundImageCornerRadius;
}
arrowImageWidth = s_ArrowHeight;
arrowImageHeight = s_ArrowBase;
arrowImageView.image = s_DefaultRightArrowImage;
break;
default:
break;
}
popoverBackgroundImageView.frame = CGRectMake(popoverImageOriginX, popoverImageOriginY, popoverImageWidth, popoverImageHeight);
arrowImageView.frame = CGRectMake(arrowImageOriginX, arrowImageOriginY, arrowImageWidth, arrowImageHeight);
}
@end