DEV Community

GoyesDev
GoyesDev

Posted on

[SUI] Path

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:

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)
  }
}
Enter fullscreen mode Exit fullscreen mode


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)
  }
}
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)