17 |
17 |
18 |
18 |
19 - (id)initWithFrame:(CGRect)frame |
19 - (id)initWithFrame:(CGRect)frame |
20 { |
20 { |
21 if ((self = [super initWithFrame:frame])) { |
21 if ((self = [super initWithFrame:frame])) { |
22 self.contentMode = UIViewContentModeRedraw; |
22 self.contentMode = UIViewContentModeRedraw; |
23 self.userInteractionEnabled = NO; |
23 self.userInteractionEnabled = NO; |
24 self.opaque = NO; |
24 self.opaque = NO; |
25 self.backgroundColor = [UIColor clearColor]; |
25 self.backgroundColor = [UIColor clearColor]; |
26 cornerRadius = 0.0; // actual value is set by the splitViewController. |
26 cornerRadius = 0.0; // actual value is set by the splitViewController. |
27 cornersPosition = MGCornersPositionLeadingVertical; |
27 cornersPosition = MGCornersPositionLeadingVertical; |
28 } |
28 } |
29 |
29 |
30 return self; |
30 return self; |
31 } |
31 } |
32 |
32 |
33 |
33 |
34 - (void)dealloc |
34 - (void)dealloc |
35 { |
35 { |
36 self.cornerBackgroundColor = nil; |
36 self.cornerBackgroundColor = nil; |
37 |
37 |
38 [super dealloc]; |
38 [super dealloc]; |
39 } |
39 } |
40 |
40 |
41 |
41 |
42 #pragma mark - |
42 #pragma mark - |
43 #pragma mark Drawing |
43 #pragma mark Drawing |
44 |
44 |
45 |
45 |
46 - (void)drawRect:(CGRect)rect |
46 - (void)drawRect:(CGRect)rect |
47 { |
47 { |
48 // Draw two appropriate corners, with cornerBackgroundColor behind them. |
48 // Draw two appropriate corners, with cornerBackgroundColor behind them. |
49 if (cornerRadius > 0) { |
49 if (cornerRadius > 0) { |
50 if (NO) { // just for debugging. |
50 if (NO) { // just for debugging. |
51 [[UIColor redColor] set]; |
51 [[UIColor redColor] set]; |
52 UIRectFill(self.bounds); |
52 UIRectFill(self.bounds); |
53 } |
53 } |
54 |
54 |
55 float maxX = CGRectGetMaxX(self.bounds); |
55 float maxX = CGRectGetMaxX(self.bounds); |
56 float maxY = CGRectGetMaxY(self.bounds); |
56 float maxY = CGRectGetMaxY(self.bounds); |
57 UIBezierPath *path = [UIBezierPath bezierPath]; |
57 UIBezierPath *path = [UIBezierPath bezierPath]; |
58 CGPoint pt = CGPointZero; |
58 CGPoint pt = CGPointZero; |
59 switch (cornersPosition) { |
59 switch (cornersPosition) { |
60 case MGCornersPositionLeadingVertical: // top of screen for a left/right split |
60 case MGCornersPositionLeadingVertical: // top of screen for a left/right split |
61 [path moveToPoint:pt]; |
61 [path moveToPoint:pt]; |
62 pt.y += cornerRadius; |
62 pt.y += cornerRadius; |
63 [path appendPath:[UIBezierPath bezierPathWithArcCenter:pt radius:cornerRadius startAngle:degreesToRadians(90) endAngle:0 clockwise:YES]]; |
63 [path appendPath:[UIBezierPath bezierPathWithArcCenter:pt radius:cornerRadius startAngle:degreesToRadians(90) endAngle:0 clockwise:YES]]; |
64 pt.x += cornerRadius; |
64 pt.x += cornerRadius; |
65 pt.y -= cornerRadius; |
65 pt.y -= cornerRadius; |
66 [path addLineToPoint:pt]; |
66 [path addLineToPoint:pt]; |
67 [path addLineToPoint:CGPointZero]; |
67 [path addLineToPoint:CGPointZero]; |
68 [path closePath]; |
68 [path closePath]; |
69 |
69 |
70 pt.x = maxX - cornerRadius; |
70 pt.x = maxX - cornerRadius; |
71 pt.y = 0; |
71 pt.y = 0; |
72 [path moveToPoint:pt]; |
72 [path moveToPoint:pt]; |
73 pt.y = maxY; |
73 pt.y = maxY; |
74 [path addLineToPoint:pt]; |
74 [path addLineToPoint:pt]; |
75 pt.x += cornerRadius; |
75 pt.x += cornerRadius; |
76 [path appendPath:[UIBezierPath bezierPathWithArcCenter:pt radius:cornerRadius startAngle:degreesToRadians(180) endAngle:degreesToRadians(90) clockwise:YES]]; |
76 [path appendPath:[UIBezierPath bezierPathWithArcCenter:pt radius:cornerRadius startAngle:degreesToRadians(180) endAngle:degreesToRadians(90) clockwise:YES]]; |
77 pt.y -= cornerRadius; |
77 pt.y -= cornerRadius; |
78 [path addLineToPoint:pt]; |
78 [path addLineToPoint:pt]; |
79 pt.x -= cornerRadius; |
79 pt.x -= cornerRadius; |
80 [path addLineToPoint:pt]; |
80 [path addLineToPoint:pt]; |
81 [path closePath]; |
81 [path closePath]; |
82 |
82 |
83 break; |
83 break; |
84 |
84 |
85 case MGCornersPositionTrailingVertical: // bottom of screen for a left/right split |
85 case MGCornersPositionTrailingVertical: // bottom of screen for a left/right split |
86 pt.y = maxY; |
86 pt.y = maxY; |
87 [path moveToPoint:pt]; |
87 [path moveToPoint:pt]; |
88 pt.y -= cornerRadius; |
88 pt.y -= cornerRadius; |
89 [path appendPath:[UIBezierPath bezierPathWithArcCenter:pt radius:cornerRadius startAngle:degreesToRadians(270) endAngle:degreesToRadians(360) clockwise:NO]]; |
89 [path appendPath:[UIBezierPath bezierPathWithArcCenter:pt radius:cornerRadius startAngle:degreesToRadians(270) endAngle:degreesToRadians(360) clockwise:NO]]; |
90 pt.x += cornerRadius; |
90 pt.x += cornerRadius; |
91 pt.y += cornerRadius; |
91 pt.y += cornerRadius; |
92 [path addLineToPoint:pt]; |
92 [path addLineToPoint:pt]; |
93 pt.x -= cornerRadius; |
93 pt.x -= cornerRadius; |
94 [path addLineToPoint:pt]; |
94 [path addLineToPoint:pt]; |
95 [path closePath]; |
95 [path closePath]; |
96 |
96 |
97 pt.x = maxX - cornerRadius; |
97 pt.x = maxX - cornerRadius; |
98 pt.y = maxY; |
98 pt.y = maxY; |
99 [path moveToPoint:pt]; |
99 [path moveToPoint:pt]; |
100 pt.y -= cornerRadius; |
100 pt.y -= cornerRadius; |
101 [path addLineToPoint:pt]; |
101 [path addLineToPoint:pt]; |
102 pt.x += cornerRadius; |
102 pt.x += cornerRadius; |
103 [path appendPath:[UIBezierPath bezierPathWithArcCenter:pt radius:cornerRadius startAngle:degreesToRadians(180) endAngle:degreesToRadians(270) clockwise:NO]]; |
103 [path appendPath:[UIBezierPath bezierPathWithArcCenter:pt radius:cornerRadius startAngle:degreesToRadians(180) endAngle:degreesToRadians(270) clockwise:NO]]; |
104 pt.y += cornerRadius; |
104 pt.y += cornerRadius; |
105 [path addLineToPoint:pt]; |
105 [path addLineToPoint:pt]; |
106 pt.x -= cornerRadius; |
106 pt.x -= cornerRadius; |
107 [path addLineToPoint:pt]; |
107 [path addLineToPoint:pt]; |
108 [path closePath]; |
108 [path closePath]; |
109 |
109 |
110 break; |
110 break; |
111 |
111 |
112 case MGCornersPositionLeadingHorizontal: // left of screen for a top/bottom split |
112 case MGCornersPositionLeadingHorizontal: // left of screen for a top/bottom split |
113 pt.x = 0; |
113 pt.x = 0; |
114 pt.y = cornerRadius; |
114 pt.y = cornerRadius; |
115 [path moveToPoint:pt]; |
115 [path moveToPoint:pt]; |
116 pt.y -= cornerRadius; |
116 pt.y -= cornerRadius; |
117 [path addLineToPoint:pt]; |
117 [path addLineToPoint:pt]; |
118 pt.x += cornerRadius; |
118 pt.x += cornerRadius; |
119 [path appendPath:[UIBezierPath bezierPathWithArcCenter:pt radius:cornerRadius startAngle:degreesToRadians(180) endAngle:degreesToRadians(270) clockwise:NO]]; |
119 [path appendPath:[UIBezierPath bezierPathWithArcCenter:pt radius:cornerRadius startAngle:degreesToRadians(180) endAngle:degreesToRadians(270) clockwise:NO]]; |
120 pt.y += cornerRadius; |
120 pt.y += cornerRadius; |
121 [path addLineToPoint:pt]; |
121 [path addLineToPoint:pt]; |
122 pt.x -= cornerRadius; |
122 pt.x -= cornerRadius; |
123 [path addLineToPoint:pt]; |
123 [path addLineToPoint:pt]; |
124 [path closePath]; |
124 [path closePath]; |
125 |
125 |
126 pt.x = 0; |
126 pt.x = 0; |
127 pt.y = maxY - cornerRadius; |
127 pt.y = maxY - cornerRadius; |
128 [path moveToPoint:pt]; |
128 [path moveToPoint:pt]; |
129 pt.y = maxY; |
129 pt.y = maxY; |
130 [path addLineToPoint:pt]; |
130 [path addLineToPoint:pt]; |
131 pt.x += cornerRadius; |
131 pt.x += cornerRadius; |
132 [path appendPath:[UIBezierPath bezierPathWithArcCenter:pt radius:cornerRadius startAngle:degreesToRadians(180) endAngle:degreesToRadians(90) clockwise:YES]]; |
132 [path appendPath:[UIBezierPath bezierPathWithArcCenter:pt radius:cornerRadius startAngle:degreesToRadians(180) endAngle:degreesToRadians(90) clockwise:YES]]; |
133 pt.y -= cornerRadius; |
133 pt.y -= cornerRadius; |
134 [path addLineToPoint:pt]; |
134 [path addLineToPoint:pt]; |
135 pt.x -= cornerRadius; |
135 pt.x -= cornerRadius; |
136 [path addLineToPoint:pt]; |
136 [path addLineToPoint:pt]; |
137 [path closePath]; |
137 [path closePath]; |
138 |
138 |
139 break; |
139 break; |
140 |
140 |
141 case MGCornersPositionTrailingHorizontal: // right of screen for a top/bottom split |
141 case MGCornersPositionTrailingHorizontal: // right of screen for a top/bottom split |
142 pt.y = cornerRadius; |
142 pt.y = cornerRadius; |
143 [path moveToPoint:pt]; |
143 [path moveToPoint:pt]; |
144 pt.y -= cornerRadius; |
144 pt.y -= cornerRadius; |
145 [path appendPath:[UIBezierPath bezierPathWithArcCenter:pt radius:cornerRadius startAngle:degreesToRadians(270) endAngle:degreesToRadians(360) clockwise:NO]]; |
145 [path appendPath:[UIBezierPath bezierPathWithArcCenter:pt radius:cornerRadius startAngle:degreesToRadians(270) endAngle:degreesToRadians(360) clockwise:NO]]; |
146 pt.x += cornerRadius; |
146 pt.x += cornerRadius; |
147 pt.y += cornerRadius; |
147 pt.y += cornerRadius; |
148 [path addLineToPoint:pt]; |
148 [path addLineToPoint:pt]; |
149 pt.x -= cornerRadius; |
149 pt.x -= cornerRadius; |
150 [path addLineToPoint:pt]; |
150 [path addLineToPoint:pt]; |
151 [path closePath]; |
151 [path closePath]; |
152 |
152 |
153 pt.y = maxY - cornerRadius; |
153 pt.y = maxY - cornerRadius; |
154 [path moveToPoint:pt]; |
154 [path moveToPoint:pt]; |
155 pt.y += cornerRadius; |
155 pt.y += cornerRadius; |
156 [path appendPath:[UIBezierPath bezierPathWithArcCenter:pt radius:cornerRadius startAngle:degreesToRadians(90) endAngle:0 clockwise:YES]]; |
156 [path appendPath:[UIBezierPath bezierPathWithArcCenter:pt radius:cornerRadius startAngle:degreesToRadians(90) endAngle:0 clockwise:YES]]; |
157 pt.x += cornerRadius; |
157 pt.x += cornerRadius; |
158 pt.y -= cornerRadius; |
158 pt.y -= cornerRadius; |
159 [path addLineToPoint:pt]; |
159 [path addLineToPoint:pt]; |
160 pt.x -= cornerRadius; |
160 pt.x -= cornerRadius; |
161 [path addLineToPoint:pt]; |
161 [path addLineToPoint:pt]; |
162 [path closePath]; |
162 [path closePath]; |
163 |
163 |
164 break; |
164 break; |
165 |
165 |
166 default: |
166 default: |
167 break; |
167 break; |
168 } |
168 } |
169 |
169 |
170 [self.cornerBackgroundColor set]; |
170 [self.cornerBackgroundColor set]; |
171 [path fill]; |
171 [path fill]; |
172 } |
172 } |
173 } |
173 } |
174 |
174 |
175 |
175 |
176 #pragma mark - |
176 #pragma mark - |
177 #pragma mark Accessors and properties |
177 #pragma mark Accessors and properties |
178 |
178 |
179 |
179 |
180 - (void)setCornerRadius:(float)newRadius |
180 - (void)setCornerRadius:(float)newRadius |
181 { |
181 { |
182 if (newRadius != cornerRadius) { |
182 if (newRadius != cornerRadius) { |
183 cornerRadius = newRadius; |
183 cornerRadius = newRadius; |
184 [self setNeedsDisplay]; |
184 [self setNeedsDisplay]; |
185 } |
185 } |
186 } |
186 } |
187 |
187 |
188 |
188 |
189 - (void)setSplitViewController:(MGSplitViewController *)theController |
189 - (void)setSplitViewController:(MGSplitViewController *)theController |
190 { |
190 { |
191 if (theController != splitViewController) { |
191 if (theController != splitViewController) { |
192 splitViewController = theController; |
192 splitViewController = theController; |
193 [self setNeedsDisplay]; |
193 [self setNeedsDisplay]; |
194 } |
194 } |
195 } |
195 } |
196 |
196 |
197 |
197 |
198 - (void)setCornersPosition:(MGCornersPosition)posn |
198 - (void)setCornersPosition:(MGCornersPosition)posn |
199 { |
199 { |
200 if (cornersPosition != posn) { |
200 if (cornersPosition != posn) { |
201 cornersPosition = posn; |
201 cornersPosition = posn; |
202 [self setNeedsDisplay]; |
202 [self setNeedsDisplay]; |
203 } |
203 } |
204 } |
204 } |
205 |
205 |
206 |
206 |
207 - (void)setCornerBackgroundColor:(UIColor *)color |
207 - (void)setCornerBackgroundColor:(UIColor *)color |
208 { |
208 { |
209 if (color != cornerBackgroundColor) { |
209 if (color != cornerBackgroundColor) { |
210 [cornerBackgroundColor release]; |
210 [cornerBackgroundColor release]; |
211 cornerBackgroundColor = [color retain]; |
211 cornerBackgroundColor = [color retain]; |
212 [self setNeedsDisplay]; |
212 [self setNeedsDisplay]; |
213 } |
213 } |
214 } |
214 } |
215 |
215 |
216 |
216 |
217 @synthesize cornerRadius; |
217 @synthesize cornerRadius; |
218 @synthesize splitViewController; |
218 @synthesize splitViewController; |