Skip to content

Instantly share code, notes, and snippets.

@KanshuYokoo
Created April 2, 2020 10:00
Show Gist options
  • Save KanshuYokoo/ac4ca194575bc07ef4a58c8bcbde0eb5 to your computer and use it in GitHub Desktop.
Save KanshuYokoo/ac4ca194575bc07ef4a58c8bcbde0eb5 to your computer and use it in GitHub Desktop.
SwiftUI Shape example, Triangle
import SwiftUI
struct TriangleView: View {
static let gradientStart = Color(red: 239.0 / 255, green: 120.0 / 255, blue: 221.0 / 255)
static let gradientEnd = Color(red: 239.0 / 255, green: 172.0 / 255, blue: 120.0 / 255)
var body: some View {
Triangle()
.fill(LinearGradient(
gradient: .init(colors: [Self.gradientStart, Self.gradientEnd]),
startPoint: .init(x: 0.5, y: 0),
endPoint: .init(x: 0.5, y: 1
)
))
.frame(width: 300, height: 300)
}
}
struct Triangle: Shape {
func path(in rect: CGRect) -> Path {
var path = Path()
path.move(to: CGPoint(x: rect.midX, y: rect.minY))
path.addLine(to: CGPoint(x: rect.minX, y: rect.maxY))
path.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY))
path.addLine(to: CGPoint(x: rect.midX, y: rect.minY))
return path
}
}
struct Triangle_Previews: PreviewProvider {
static var previews: some View {
TriangleView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment