-
Notifications
You must be signed in to change notification settings - Fork 1
/
DXRadianLayerView.m
130 lines (123 loc) · 4.79 KB
/
DXRadianLayerView.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
//
// DXRadianLayerView.m
// UIVIEW弧形边框
//
// Created by arraybuffer on 2020/4/26.
// Copyright © 2020 airtim. All rights reserved.
//
#import "DXRadianLayerView.h"
@implementation DXRadianLayerView
- (void)setRadian:(CGFloat) radian
{
if(radian == 0) return;
CGFloat t_width = CGRectGetWidth(self.frame); // 宽
CGFloat t_height = CGRectGetHeight(self.frame); // 高
CGFloat height = fabs(radian); // 圆弧高度
CGFloat x = 0;
CGFloat y = 0;
// 计算圆弧的最大高度
CGFloat _maxRadian = 0;
switch (self.direction) {
case DXRadianDirectionBottom:
case DXRadianDirectionTop:
_maxRadian = MIN(t_height, t_width / 2);
break;
case DXRadianDirectionLeft:
case DXRadianDirectionRight:
_maxRadian = MIN(t_height / 2, t_width);
break;
default:
break;
}
if(height > _maxRadian){
NSLog(@"圆弧半径过大, 跳过设置。");
return;
}
// 计算半径
CGFloat radius = 0;
switch (self.direction) {
case DXRadianDirectionBottom:
case DXRadianDirectionTop:
{
CGFloat c = sqrt(pow(t_width / 2, 2) + pow(height, 2));
CGFloat sin_bc = height / c;
radius = c / ( sin_bc * 2);
}
break;
case DXRadianDirectionLeft:
case DXRadianDirectionRight:
{
CGFloat c = sqrt(pow(t_height / 2, 2) + pow(height, 2));
CGFloat sin_bc = height / c;
radius = c / ( sin_bc * 2);
}
break;
default:
break;
}
// 画圆
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
[shapeLayer setFillColor:[[UIColor whiteColor] CGColor]];
CGMutablePathRef path = CGPathCreateMutable();
switch (self.direction) {
case DXRadianDirectionBottom:
{
if(radian > 0){
CGPathMoveToPoint(path,NULL, t_width,t_height - height);
CGPathAddArc(path,NULL, t_width / 2, t_height - radius, radius, asin((radius - height ) / radius), M_PI - asin((radius - height ) / radius), NO);
}else{
CGPathMoveToPoint(path,NULL, t_width,t_height);
CGPathAddArc(path,NULL, t_width / 2, t_height + radius - height, radius, 2 * M_PI - asin((radius - height ) / radius), M_PI + asin((radius - height ) / radius), YES);
}
CGPathAddLineToPoint(path,NULL, x, y);
CGPathAddLineToPoint(path,NULL, t_width, y);
}
break;
case DXRadianDirectionTop:
{
if(radian > 0){
CGPathMoveToPoint(path,NULL, t_width, height);
CGPathAddArc(path,NULL, t_width / 2, radius, radius, 2 * M_PI - asin((radius - height ) / radius), M_PI + asin((radius - height ) / radius), YES);
}else{
CGPathMoveToPoint(path,NULL, t_width, y);
CGPathAddArc(path,NULL, t_width / 2, height - radius, radius, asin((radius - height ) / radius), M_PI - asin((radius - height ) / radius), NO);
}
CGPathAddLineToPoint(path,NULL, x, t_height);
CGPathAddLineToPoint(path,NULL, t_width, t_height);
}
break;
case DXRadianDirectionLeft:
{
if(radian > 0){
CGPathMoveToPoint(path,NULL, height, y);
CGPathAddArc(path,NULL, radius, t_height / 2, radius, M_PI + asin((radius - height ) / radius), M_PI - asin((radius - height ) / radius), YES);
}else{
CGPathMoveToPoint(path,NULL, x, y);
CGPathAddArc(path,NULL, height - radius, t_height / 2, radius, 2 * M_PI - asin((radius - height ) / radius), asin((radius - height ) / radius), NO);
}
CGPathAddLineToPoint(path,NULL, t_width, t_height);
CGPathAddLineToPoint(path,NULL, t_width, y);
}
break;
case DXRadianDirectionRight:
{
if(radian > 0){
CGPathMoveToPoint(path,NULL, t_width - height, y);
CGPathAddArc(path,NULL, t_width - radius, t_height / 2, radius, 1.5 * M_PI + asin((radius - height ) / radius), M_PI / 2 + asin((radius - height ) / radius), NO);
}else{
CGPathMoveToPoint(path,NULL, t_width, y);
CGPathAddArc(path,NULL, t_width + radius - height, t_height / 2, radius, M_PI + asin((radius - height ) / radius), M_PI - asin((radius - height ) / radius), YES);
}
CGPathAddLineToPoint(path,NULL, x, t_height);
CGPathAddLineToPoint(path,NULL, x, y);
}
break;
default:
break;
}
CGPathCloseSubpath(path);
[shapeLayer setPath:path];
CFRelease(path);
self.layer.mask = shapeLayer;
}
@end