๐Vehicle.CarBodyShape
Last updated
Last updated
โฌ๏ธ ้่ฆ๏ผ Vector2D, Rectangular
// 2022.02.10
import SwiftUI
extension Vehicle {
/// `Vehicle.CarBodyShape`
struct CarBodyShape: Shape {
// relative points in rect
let A : UnitPoint = [0 , 1 ] // ๐
ฟ๏ธ Vector2D
let B : UnitPoint = [1 , 0.8]
let C1: UnitPoint = [0.1, 0.4] // control points
let C2: UnitPoint = [0.2, 0.5]
let C : UnitPoint = [0.8, 1]
let C3: UnitPoint = [0.9, 1] // control points
let C4: UnitPoint = [1 , 1]
func path(in rect: CGRect) -> Path {
Path { path in
// move to A
path.move(to: rect[A]) // ๐
ฟ๏ธ Rectangular
// add curve to B
path.addCurve(to: rect[B], control1: rect[C1], control2: rect[C2])
// add curve to C
path.addCurve(to: rect[C], control1: rect[C3], control2: rect[C4])
// add line back to A
path.closeSubpath()
}
}
}
}
โฌ๏ธ ้่ฆ๏ผ Pin, Point, GridLines
// โญ๏ธ previews ๅซไป้บผๅๅญๅฅฝๅๆฒ้ไฟโ
struct CarBodyShape_Previews: PreviewProvider {
static var previews: some View {
let b = Vehicle.CarBodyShape()
b
// โญ๏ธ set primary color for shape
.foregroundColor(.green.opacity(0.8))
.frame(height: 150)
.overlay {
// ๐ผ Pin (4 control points)
Pin(b.C1, b.A)
Pin(b.C2, b.B)
Pin(b.C4, b.C, .blue)
Pin(b.C3, b.B, .blue)
// ๐ผ Point (3 key points)
Point(b.A, .gray, label: "A")
Point(b.B, .gray, label: "B")
Point(b.C, .gray, label: "C")
}
.background(GridLines()) // ๐ผ GridLines
.border(Color.blue.opacity(0.3))
.padding(40)
}
}