-
Notifications
You must be signed in to change notification settings - Fork 7
/
PCPopoverController.m
223 lines (176 loc) · 6.66 KB
/
PCPopoverController.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
//
// PCPopoverController.m
// PCPopoverControllerTests
//
// Created by Patrick Perini on 5/16/12.
// Licensing information available in README.md
//
#import "PCPopoverController.h"
#pragma mark - Internal Constants
CGFloat const contentInset = 10.0;
CGFloat const capInset = 25.0;
CGFloat const arrowHeight = 15.0;
CGFloat const arrowBase = 24.0;
@interface PCPopoverControllerBackgroundView : UIPopoverBackgroundView
{
UIImageView *borderImageView;
UIImageView *arrowImageView;
}
+ (UIColor *)currentTintColor;
+ (void)setCurrentTintColor: (UIColor *)tintColor;
@end
@implementation PCPopoverControllerBackgroundView
#pragma mark - Internal Class Variables
static UIColor *currentTintColor;
#pragma mark - Initializers
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame: frame];
if (!self)
return nil;
UIGraphicsBeginImageContext(CGSizeMake(60, 60));
UIBezierPath *borderPath = [UIBezierPath bezierPathWithRoundedRect: CGRectMake(0, 0, 60, 60)
cornerRadius: 8];
[currentTintColor setFill];
[borderPath fill];
UIImage *borderImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIEdgeInsets capInsets = UIEdgeInsetsMake(capInset, capInset, capInset, capInset);
borderImageView = [[UIImageView alloc] initWithImage: [borderImage resizableImageWithCapInsets: capInsets]];
borderImageView.layer.shadowColor = [UIColor blackColor].CGColor;
borderImageView.layer.shadowOpacity = .4;
borderImageView.layer.shadowRadius = 2;
borderImageView.layer.shadowOffset = CGSizeMake(0, 2);
UIGraphicsBeginImageContext(CGSizeMake(25, 25));
UIBezierPath *arrowPath = [UIBezierPath bezierPath];
[arrowPath moveToPoint: CGPointMake(12.5, 0)];
[arrowPath addLineToPoint: CGPointMake(25, 25)];
[arrowPath addLineToPoint: CGPointMake(0, 25)];
[arrowPath addLineToPoint: CGPointMake(12.5, 0)];
[currentTintColor setFill];
[arrowPath fill];
UIImage *arrowImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
arrowImageView = [[UIImageView alloc] initWithImage: arrowImage];
arrowImageView.layer.shadowColor = [UIColor blackColor].CGColor;
arrowImageView.layer.shadowOpacity = .4;
arrowImageView.layer.shadowRadius = 2;
arrowImageView.layer.shadowOffset = CGSizeMake(0, 1);
arrowImageView.layer.masksToBounds = YES;
[self addSubview: borderImageView];
[self addSubview: arrowImageView];
return self;
}
#pragma mark - Class Accessors and Mutators
+ (UIColor *)currentTintColor
{
return currentTintColor;
}
+ (void)setCurrentTintColor:(UIColor *)tintColor
{
currentTintColor = tintColor;
}
#pragma mark - Class Handlers
+ (UIEdgeInsets)contentViewInsets
{
return UIEdgeInsetsMake(contentInset, contentInset, contentInset, contentInset);
}
+ (CGFloat)arrowHeight
{
return arrowHeight;
}
+ (CGFloat)arrowBase
{
return arrowBase;
}
#pragma mark - View Handlers
@synthesize arrowOffset;
@synthesize arrowDirection;
- (void)layoutSubviews
{
[super layoutSubviews];
CGFloat height = CGRectGetHeight(self.frame);
CGFloat width = CGRectGetWidth(self.frame);
CGFloat left = 0;
CGFloat top = 0;
CGFloat coordinate = 0;
CGAffineTransform rotation = CGAffineTransformIdentity;
switch (arrowDirection)
{
case UIPopoverArrowDirectionUp:
{
top += arrowHeight;
height -= arrowHeight;
coordinate = ((CGRectGetWidth(self.frame) / 2.0) + arrowOffset) - (arrowBase / 2.0);
arrowImageView.frame = CGRectMake(coordinate, 0, arrowBase, arrowHeight);
break;
}
case UIPopoverArrowDirectionDown:
{
height -= arrowHeight;
coordinate = ((CGRectGetWidth(self.frame) / 2.0) + arrowOffset) - (arrowBase / 2.0);
arrowImageView.frame = CGRectMake(coordinate, height, arrowBase, arrowHeight);
rotation = CGAffineTransformMakeRotation(M_PI);
break;
}
case UIPopoverArrowDirectionLeft:
{
left += arrowBase - ceil((arrowBase - arrowHeight) / 2.0);
width -= arrowBase;
coordinate = ((CGRectGetHeight(self.frame) / 2.0) + arrowOffset) - (arrowHeight / 2.0);
arrowImageView.frame = CGRectMake(0, coordinate, arrowBase, arrowHeight);
rotation = CGAffineTransformMakeRotation(-M_PI_2);
break;
}
case UIPopoverArrowDirectionRight:
{
left += ceil((arrowBase - arrowHeight) / 2.0);
width -= arrowBase;
coordinate = ((CGRectGetHeight(self.frame) / 2.0) + arrowOffset) - (arrowHeight / 2.0);
arrowImageView.frame = CGRectMake(width, coordinate, arrowBase, arrowHeight);
rotation = CGAffineTransformMakeRotation(M_PI_2);
break;
}
}
borderImageView.frame = CGRectMake(left, top, width, height);
[arrowImageView setTransform: rotation];
}
@end
@implementation PCPopoverController
#pragma mark - Properties
@synthesize tintColor;
#pragma mark - Initializers
- (id)initWithContentViewController:(UIViewController *)viewController
{
self = [self initWithContentViewController: viewController
andTintColor: [UIColor blackColor]];
return self;
}
- (id)initWithContentViewController:(UIViewController *)viewController andTintColor:(UIColor *)aTintColor
{
self = [super initWithContentViewController: viewController];
if (!self)
return nil;
[super setPopoverBackgroundViewClass: [PCPopoverControllerBackgroundView class]];
tintColor = aTintColor;
return self;
}
#pragma mark - Overriders
- (void)setPopoverBackgroundViewClass:(Class)popoverBackgroundViewClass {}
- (void)presentPopoverFromRect:(CGRect)rect inView:(UIView *)view permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections animated:(BOOL)animated
{
@synchronized(self)
{
[[PCPopoverControllerBackgroundView class] setCurrentTintColor: tintColor];
[super presentPopoverFromRect: rect inView: view permittedArrowDirections: arrowDirections animated: animated];
}
}
- (void)presentPopoverFromBarButtonItem:(UIBarButtonItem *)item permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections animated:(BOOL)animated
{
@synchronized(self)
{
[[PCPopoverControllerBackgroundView class] setCurrentTintColor: tintColor];
[super presentPopoverFromBarButtonItem: item permittedArrowDirections: arrowDirections animated: animated];
}
}
@end