💜LineShape
SwiftUI ⟩ Drawing ⟩ Shape ⟩ Helper Shapes ⟩

// 2022.02.10
import SwiftUI
/// a line (invisible shape) relative to a rect.
/// ```
/// LineShape([0,0], [1,1])
/// ```
struct LineShape: Shape {
    let p1: UnitPoint
    let p2: UnitPoint
    func path(in rect: CGRect) -> Path {
        Path { path in
            // 🌀Path+ line(), 🅿️ Rectangular
            path.line(rect[p1], rect[p2])
        }
    }
}
// convenience init
extension LineShape {
    init(_ p1: UnitPoint, _ p2: UnitPoint) {
        self.init(p1: p1, p2: p2)
    }
}
struct LineShape_Previews: PreviewProvider {
    static var previews: some View {
        HStack {
            Group {
                LineShape([0.5,0], [1,0.5]).stroke()
                LineShape([0.5,1], [1,0.5]).stroke()
                LineShape([0.5,0], [1,1]).stroke()
            }
            .frame(width: 100, height: 100)
            .foregroundColor(.pink)            // ⭐️ set primary color
            .border(Color.primary.opacity(0.4))
        }
        .padding()
    }
}Last updated
Was this helpful?