🖼️Pin
SwiftUI ⟩ Drawing ⟩ Shape ⟩ Helper Shapes ⟩

// 2022.02.10
import SwiftUI
/// a (visible) pin in a rect.
/// ```
/// let H: UnitPoint = [0.2, 0.5]
/// let T: UnitPoint = [0.7, 0.7]
///
/// Pin(H, T)
/// Pin(H, T, .blue)
/// Pin(H, T, label: "A")
/// Pin(H, T, size: 40)
/// ```
struct Pin: View {
    
    let head: UnitPoint
    let tail: UnitPoint
    
    var color: Color = .pink    // pin head color
    var size: CGFloat = 24      // pin head size
    var label: String = ""
    
    var body: some View {
        ZStack {
            // 🖼 Line (pin tail)
            Line(head, tail)
            // 🖼 Point (pin head)
            Point(head, color, size: size, label: label)
        }
    }
}
// convenience init
extension Pin {
    init(
        _ head: UnitPoint, 
        _ tail: UnitPoint, 
        _ color: Color = .pink, 
        size: CGFloat = 24, 
        label: String = ""
    ) {
        self.init(head: head, tail:tail, color: color, size: size, label: label)
    }
}⬆️ 需要: Frame
struct Pin_Previews: PreviewProvider {
    
    // (relative) points
    static let H1: UnitPoint = [0.2, 0.2]    // Rectangular
    static let H2: UnitPoint = [0.8, 0.2]
    static let T: UnitPoint = [0.4, 0.9]
    
    static var previews: some View {
        HStack {
            Group {
                ZStack {
                    Pin(H1, T)
                    Pin(H2, T, .blue)
                }
                Pin(H1, T, label: "A")
                Pin(H1, T, .green, label: "B")
                Pin(H2, T, .purple, size: 30, label: "C")
            }
            .frame(width: 100, height: 100)
            .border(.white(0.4))
        }
    }
}- used in Vehicle.CarBodyShape's previews. 
Last updated
Was this helpful?