๐ผ๏ธPoint
Last updated
Last updated
SwiftUI โฉ Drawing โฉ Shape โฉ Helper Shapes โฉ
โฌ๏ธ ้่ฆ๏ผ Rectangular
import SwiftUI
/// draw a relative point in a rect.
/// ```
/// Point(A) // A: UnitPoint
/// Point(A, .black, size: 20, label: "A")
/// ```
struct Point: View {
let point: UnitPoint
var color: Color = .pink
var size : CGFloat = 24
var label: String = ""
var body: some View {
GeometryReader { geo in
Circle()
// point color
.fill(color)
// point label
.overlay(Text(label).font(.caption).bold())
// point size
.frame(width: size, height: size)
// โญ๏ธ absolute position
// โญ๏ธ frame changed! ("push-out" view)
.position(geo.size[point]) // ๐
ฟ๏ธ Rectangular
}
}
}
// convenience init
extension Point {
init(
_ point: UnitPoint,
_ color: Color = .pink,
size : CGFloat = 24,
label : String = ""
) {
self.init(point: point, color: color, size: size, label: label)
}
}
โฌ๏ธ ้่ฆ๏ผ GridLines, .white(), Line, Point
struct Point_Previews: PreviewProvider {
// relative points
static let A: UnitPoint = [0.3, 0.8]
static let B: UnitPoint = [0.2, 0.5]
static let C: UnitPoint = [0.7, 0.3]
// previews
static var previews: some View {
ZStack {
// ๐ผ Line (3 segments)
Line(A, B)
Line(B, C)
Line(C, A)
// ๐ผ Point (3 points)
Point(A, label: "A")
Point(B, .blue, label: "B")
Point(C, .green, size: 30)
Point(C, .black, size: 20, label: "C")
}
.compositingGroup()
.shadow(color: .black.opacity(0.8), radius: 4, y: 4)
// ๐ผ GridLines, ๐Color+
.background(GridLines(color: .white(0.3)))
.border(Color.black.opacity(0.8))
.frame(height: 150)
.padding(8)
.background(.white(0.5)) // ๐ShapeStyle+
.padding()
}
}
used in Pin.
used in Vehicle.CarBodyShape's previews.
used in Vehicle as wheels' shape.