Skip to content

Instantly share code, notes, and snippets.

@swiftui-lab
Created July 10, 2020 04:47
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swiftui-lab/c77af5f33f68da86de8b1e84391a6a9d to your computer and use it in GitHub Desktop.
Save swiftui-lab/c77af5f33f68da86de8b1e84391a6a9d to your computer and use it in GitHub Desktop.
A shape of a triangle inside a rectangle
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