/
MGSplitCornersView.m
241 lines (192 loc) · 5.85 KB
/
MGSplitCornersView.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
//
// MGSplitCornersView.m
// MGSplitView
//
// Created by Matt Gemmell on 28/07/2010.
// Copyright 2010 Instinctive Code.
//
#import "MGSplitCornersView.h"
@implementation MGSplitCornersView
#pragma mark -
#pragma mark Setup and teardown
- (id)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame])) {
self.contentMode = UIViewContentModeRedraw;
self.userInteractionEnabled = NO;
self.opaque = NO;
self.backgroundColor = [UIColor clearColor];
cornerRadius = 0.0; // actual value is set by the splitViewController.
cornersPosition = MGCornersPositionLeadingVertical;
}
return self;
}
- (void)dealloc
{
self.cornerBackgroundColor = nil;
[super dealloc];
}
#pragma mark -
#pragma mark Geometry helpers
double deg2Rad(double degrees)
{
// Converts degrees to radians.
return degrees * (M_PI / 180.0);
}
double rad2Deg(double radians)
{
// Converts radians to degrees.
return radians * (180 / M_PI);
}
#pragma mark -
#pragma mark Drawing
- (void)drawRect:(CGRect)rect
{
// Draw two appropriate corners, with cornerBackgroundColor behind them.
if (cornerRadius > 0) {
if (NO) { // just for debugging.
[[UIColor redColor] set];
UIRectFill(self.bounds);
}
float maxX = CGRectGetMaxX(self.bounds);
float maxY = CGRectGetMaxY(self.bounds);
UIBezierPath *path = [UIBezierPath bezierPath];
CGPoint pt = CGPointZero;
switch (cornersPosition) {
case MGCornersPositionLeadingVertical: // top of screen for a left/right split
[path moveToPoint:pt];
pt.y += cornerRadius;
[path appendPath:[UIBezierPath bezierPathWithArcCenter:pt radius:cornerRadius startAngle:deg2Rad(90) endAngle:0 clockwise:YES]];
pt.x += cornerRadius;
pt.y -= cornerRadius;
[path addLineToPoint:pt];
[path addLineToPoint:CGPointZero];
[path closePath];
pt.x = maxX - cornerRadius;
pt.y = 0;
[path moveToPoint:pt];
pt.y = maxY;
[path addLineToPoint:pt];
pt.x += cornerRadius;
[path appendPath:[UIBezierPath bezierPathWithArcCenter:pt radius:cornerRadius startAngle:deg2Rad(180) endAngle:deg2Rad(90) clockwise:YES]];
pt.y -= cornerRadius;
[path addLineToPoint:pt];
pt.x -= cornerRadius;
[path addLineToPoint:pt];
[path closePath];
break;
case MGCornersPositionTrailingVertical: // bottom of screen for a left/right split
pt.y = maxY;
[path moveToPoint:pt];
pt.y -= cornerRadius;
[path appendPath:[UIBezierPath bezierPathWithArcCenter:pt radius:cornerRadius startAngle:deg2Rad(270) endAngle:deg2Rad(360) clockwise:NO]];
pt.x += cornerRadius;
pt.y += cornerRadius;
[path addLineToPoint:pt];
pt.x -= cornerRadius;
[path addLineToPoint:pt];
[path closePath];
pt.x = maxX - cornerRadius;
pt.y = maxY;
[path moveToPoint:pt];
pt.y -= cornerRadius;
[path addLineToPoint:pt];
pt.x += cornerRadius;
[path appendPath:[UIBezierPath bezierPathWithArcCenter:pt radius:cornerRadius startAngle:deg2Rad(180) endAngle:deg2Rad(270) clockwise:NO]];
pt.y += cornerRadius;
[path addLineToPoint:pt];
pt.x -= cornerRadius;
[path addLineToPoint:pt];
[path closePath];
break;
case MGCornersPositionLeadingHorizontal: // left of screen for a top/bottom split
pt.x = 0;
pt.y = cornerRadius;
[path moveToPoint:pt];
pt.y -= cornerRadius;
[path addLineToPoint:pt];
pt.x += cornerRadius;
[path appendPath:[UIBezierPath bezierPathWithArcCenter:pt radius:cornerRadius startAngle:deg2Rad(180) endAngle:deg2Rad(270) clockwise:NO]];
pt.y += cornerRadius;
[path addLineToPoint:pt];
pt.x -= cornerRadius;
[path addLineToPoint:pt];
[path closePath];
pt.x = 0;
pt.y = maxY - cornerRadius;
[path moveToPoint:pt];
pt.y = maxY;
[path addLineToPoint:pt];
pt.x += cornerRadius;
[path appendPath:[UIBezierPath bezierPathWithArcCenter:pt radius:cornerRadius startAngle:deg2Rad(180) endAngle:deg2Rad(90) clockwise:YES]];
pt.y -= cornerRadius;
[path addLineToPoint:pt];
pt.x -= cornerRadius;
[path addLineToPoint:pt];
[path closePath];
break;
case MGCornersPositionTrailingHorizontal: // right of screen for a top/bottom split
pt.y = cornerRadius;
[path moveToPoint:pt];
pt.y -= cornerRadius;
[path appendPath:[UIBezierPath bezierPathWithArcCenter:pt radius:cornerRadius startAngle:deg2Rad(270) endAngle:deg2Rad(360) clockwise:NO]];
pt.x += cornerRadius;
pt.y += cornerRadius;
[path addLineToPoint:pt];
pt.x -= cornerRadius;
[path addLineToPoint:pt];
[path closePath];
pt.y = maxY - cornerRadius;
[path moveToPoint:pt];
pt.y += cornerRadius;
[path appendPath:[UIBezierPath bezierPathWithArcCenter:pt radius:cornerRadius startAngle:deg2Rad(90) endAngle:0 clockwise:YES]];
pt.x += cornerRadius;
pt.y -= cornerRadius;
[path addLineToPoint:pt];
pt.x -= cornerRadius;
[path addLineToPoint:pt];
[path closePath];
break;
default:
break;
}
[self.cornerBackgroundColor set];
[path fill];
}
}
#pragma mark -
#pragma mark Accessors and properties
- (void)setCornerRadius:(float)newRadius
{
if (newRadius != cornerRadius) {
cornerRadius = newRadius;
[self setNeedsDisplay];
}
}
- (void)setSplitViewController:(MGSplitViewController *)theController
{
if (theController != splitViewController) {
splitViewController = theController;
[self setNeedsDisplay];
}
}
- (void)setCornersPosition:(MGCornersPosition)posn
{
if (cornersPosition != posn) {
cornersPosition = posn;
[self setNeedsDisplay];
}
}
- (void)setCornerBackgroundColor:(UIColor *)color
{
if (color != cornerBackgroundColor) {
[cornerBackgroundColor release];
cornerBackgroundColor = [color retain];
[self setNeedsDisplay];
}
}
@synthesize cornerRadius;
@synthesize splitViewController;
@synthesize cornersPosition;
@synthesize cornerBackgroundColor;
@end