Created
July 10, 2020 04:47
-
-
Save swiftui-lab/c77af5f33f68da86de8b1e84391a6a9d to your computer and use it in GitHub Desktop.
A shape of a triangle inside a rectangle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Triangle: View { | |
let direction: Direction | |
let color: Color | |
init(_ direction: Direction = .down, _ color: Color = .black) { | |
self.direction = direction | |
self.color = color | |
} | |
var body: some View { | |
TriangleShape() | |
.fill(color) | |
.border(Color.black) | |
.rotationEffect(Angle.degrees(direction.rawValue)) | |
} | |
struct TriangleShape: Shape { | |
func path(in rect: CGRect) -> Path { | |
var path = Path() | |
path.move(to: .zero) | |
path.addLine(to: CGPoint(x: rect.maxX, y: 0)) | |
path.addLine(to: CGPoint(x: rect.midX, y: rect.midY)) | |
path.closeSubpath() | |
return path | |
} | |
} | |
enum Direction: Double { | |
case down = 0 | |
case left = 90 | |
case up = 180 | |
case right = 270 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment