Un Path define el borde externo de una figura. Se construye como una combinación de líneas y curvas. El cursor se mueve de un punto a otro. Para ello se usan los siguientes métodos:
Path.init()Path.init(_:). Crea un path vacío y luego ejecuta un closure con él.move(to:). Como inicialmente el cursor arranca en0, 0, entonces hay que moverlo hasta la posición que se desee con este método, antes de trazar la primera línea.addArc(center:radius:startAngle:endAngle:clockwise:transform:)
La figura resultante se puede rellenar o dejar como perímetro
struct ContentView: View {
var body: some View {
Path { path in
// Esquina superior izquierda
path.move(to: .init(x: 80, y: 150))
// Esquina superior derecha.
// Desplazamiento en x
path.addLine(to: .init(x: 200, y: 150))
// Esquina inferior izquierda.
// Debajo del primer vértice.
path.addLine(to: .init(x: 80, y: 250))
path.closeSubpath()
}
.stroke(.red, lineWidth: 5)
}
}
struct ContentView: View {
var body: some View {
Path { path in
path.move(to: .init(x: 80, y: 150))
path.addLine(to: .init(x: 280, y: 150))
path.addArc(center: .init(x: 280, y: 160), radius: 10, startAngle: .degrees(270), endAngle: .degrees(90), clockwise: false)
path.addLine(to: .init(x: 150, y: 170))
path.addLine(to: .init(x: 150, y: 190))
path.closeSubpath()
}
.stroke(.red, lineWidth: 5)
}
}
En el siguiente caso, al agregar la elipse, closeSubpath no hace nada.
swift
struct ContentView: View {
var body: some View {
Path { path in
path.move(to: .init(x: 80, y: 150))
path.addLine(to: .init(x: 280, y: 150))
path.addEllipse(in: .init(x: 290, y: 140, width: 20, height: 20))
path.addLine(to: .init(x: 310, y: 230))
path.closeSubpath()
}
.stroke(.red, lineWidth: 5)
}
}
---
## Bibliografía
- [Gauchat, J. D. (2024). SwiftUI for Masterminds: How to take advantage of Swift and SwiftUI to create insanely great apps for iPhones, iPads, and Macs (5a ed.). John D Gauchat.](https://a.co/d/0egeOYJp)


Top comments (0)