-
Notifications
You must be signed in to change notification settings - Fork 0
/
RoundedRectView.mm
executable file
·76 lines (56 loc) · 2.37 KB
/
RoundedRectView.mm
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
//
// RoundedRectView.mm
// BananaCamera
//
// Copyright 2011 Banana Camera Company. All rights reserved.
//
#import "RoundedRectView.h"
@implementation RoundedRectView
- (id)initWithFrame: (CGRect)frame
{
if((self = [super initWithFrame:frame]))
{
}
return self;
}
- (void) drawRect: (CGRect) rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect fillFrame = CGRectInset(self.bounds, 20.0, 20.0);
CGRect strokeFrame = CGRectInset(self.bounds, 20.0, 20.0);
CGPathRef fillPath = [self newPathForRoundedRect: fillFrame radius: 10 ];
CGPathRef strokePath = [self newPathForRoundedRect: strokeFrame radius: 10 ];
CGContextAddPath(ctx, fillPath);
CGContextSetRGBFillColor(ctx, 1.0, 1.0, 1.0, 1.0);
CGContextFillPath(ctx);
CGContextAddPath(ctx, strokePath);
CGContextSetRGBStrokeColor(ctx, 0.67, 0.67, 0.67, 1.0);
CGContextSetLineWidth(ctx, 1.0);
CGContextStrokePath(ctx);
CGPathRelease(strokePath);
CGPathRelease(fillPath);
}
- (CGPathRef) newPathForRoundedRect: (CGRect) rect radius: (CGFloat) radius
{
CGMutablePathRef retPath = CGPathCreateMutable();
CGRect innerRect = CGRectInset(rect, radius, radius);
CGFloat inside_right = innerRect.origin.x + innerRect.size.width;
CGFloat outside_right = rect.origin.x + rect.size.width;
CGFloat inside_bottom = innerRect.origin.y + innerRect.size.height;
CGFloat outside_bottom = rect.origin.y + rect.size.height;
CGFloat inside_top = innerRect.origin.y;
CGFloat outside_top = rect.origin.y;
CGFloat outside_left = rect.origin.x;
CGPathMoveToPoint(retPath, NULL, innerRect.origin.x, outside_top);
CGPathAddLineToPoint(retPath, NULL, inside_right, outside_top);
CGPathAddArcToPoint(retPath, NULL, outside_right, outside_top, outside_right, inside_top, radius);
CGPathAddLineToPoint(retPath, NULL, outside_right, inside_bottom);
CGPathAddArcToPoint(retPath, NULL, outside_right, outside_bottom, inside_right, outside_bottom, radius);
CGPathAddLineToPoint(retPath, NULL, innerRect.origin.x, outside_bottom);
CGPathAddArcToPoint(retPath, NULL, outside_left, outside_bottom, outside_left, inside_bottom, radius);
CGPathAddLineToPoint(retPath, NULL, outside_left, inside_top);
CGPathAddArcToPoint(retPath, NULL, outside_left, outside_top, innerRect.origin.x, outside_top, radius);
CGPathCloseSubpath(retPath);
return retPath;
}
@end