-
Notifications
You must be signed in to change notification settings - Fork 9
/
Shapes.swift
49 lines (42 loc) · 1.11 KB
/
Shapes.swift
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
// MARK: - VLine
public struct VLine: Shape {
public init() {}
public func path(in rect: CGRect) -> SwiftUI.Path {
SwiftUI.Path { path in
path.move(to: .init(x: rect.midX, y: rect.minY))
path.addLine(to: .init(x: rect.midX, y: rect.maxY))
}
}
}
// MARK: - RoundedCorners
/// A more advanced rounded corners shape, allowing to round specific corners.
public struct RoundedCorners: Shape {
let corners: UIRectCorner
let radius: CGFloat
public init(corners: UIRectCorner = .allCorners, radius: CGFloat) {
self.corners = corners
self.radius = radius
}
public func path(in rect: CGRect) -> SwiftUI.Path {
.init(
UIBezierPath(
roundedRect: rect,
byRoundingCorners: corners,
cornerRadii: .init(width: radius, height: radius)
).cgPath
)
}
}
extension View {
public func roundedCorners(_ corners: UIRectCorner = .allCorners, radius: CGFloat) -> some View {
self.clipShape(RoundedCorners(corners: corners, radius: radius))
}
}
extension UIRectCorner {
public static var top: UIRectCorner {
[.topLeft, .topRight]
}
public static var bottom: UIRectCorner {
[.bottomLeft, .bottomRight]
}
}