forked from msavela/corner-radius
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CornerRadius.m
115 lines (90 loc) · 3.33 KB
/
CornerRadius.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
//
// CornerRadius.m
//
#import "CornerRadius.h"
#define DEFAULT_RADIUS 3.0
#define DEFAULT_COLOR [UIColor blackColor]
@implementation CornerRadius
- (void)initStuff_CornerRadius
{
self.backgroundColor = [UIColor clearColor];
self.userInteractionEnabled = NO;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateFrame) name:UIDeviceOrientationDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillChangeStatusBarFrame:) name:UIApplicationWillChangeStatusBarFrameNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateFrame) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];
}
- (id)initWithRadius:(NSInteger)radius color:(UIColor *)color
{
if(self = [super initWithFrame:[self appFrame]])
{
self.cornersOn = YES;
self.radius = radius;
self.color = color;
self.contentMode = UIViewContentModeRedraw;
[self initStuff_CornerRadius];
}
return self;
}
- (void)applicationWillChangeStatusBarFrame:(NSNotification *)notif
{
[UIView animateWithDuration:0.35 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^ {
[self updateFrame];
} completion:^(BOOL finished) {
[self updateFrame];
}];
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [self initWithRadius:DEFAULT_RADIUS color:DEFAULT_COLOR];
return self;
}
- (id)initWithFrame:(CGRect)frame
{
self = [self initWithRadius:DEFAULT_RADIUS color:DEFAULT_COLOR];
return self;
}
- (void)setCornersOn:(BOOL)cornersOn
{
BOOL oldCornersOn = _cornersOn;
_cornersOn = cornersOn;
if(oldCornersOn != self.cornersOn)
{
[self setNeedsDisplay];
}
}
- (CGRect)appFrame
{
return [UIScreen mainScreen].applicationFrame;
}
- (void)updateFrame
{
self.frame = [self appFrame];
}
- (void)drawRect:(CGRect)rect
{
if(self.cornersOn)
{
CGContextRef context = UIGraphicsGetCurrentContext();
[self.color set];
// Bottom left corner
CGContextMoveToPoint(context, rect.origin.x, rect.origin.y + rect.size.height);
CGContextAddArc(context, rect.origin.x + self.radius, rect.origin.y + rect.size.height - self.radius, self.radius, M_PI, M_PI / 2, 1);
// Bottom right corner
CGContextMoveToPoint(context, rect.origin.x + rect.size.width, rect.origin.y + rect.size.height);
CGContextAddArc(context, rect.origin.x + rect.size.width - self.radius, rect.origin.y + rect.size.height - self.radius, self.radius, M_PI / 2, 0.0f, 1);
// Top right corner
CGContextMoveToPoint(context, rect.origin.x + rect.size.width, rect.origin.y);
CGContextAddArc(context, rect.origin.x + rect.size.width - self.radius, rect.origin.y + self.radius, self.radius, 0.0f, -M_PI / 2, 1);
// Top left corner
CGContextMoveToPoint(context, rect.origin.x, rect.origin.y);
CGContextAddArc(context, rect.origin.x + self.radius, rect.origin.y + self.radius, self.radius, -M_PI / 2, M_PI, 1);
CGContextFillPath(context);
}
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillChangeStatusBarFrameNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidChangeStatusBarFrameNotification object:nil];
}
@end