Skip to content

Instantly share code, notes, and snippets.

@michaelevensen
Created January 18, 2023 21:19
Show Gist options
  • Save michaelevensen/63fdc0750485d8f10eb3a7523931b88b to your computer and use it in GitHub Desktop.
Save michaelevensen/63fdc0750485d8f10eb3a7523931b88b to your computer and use it in GitHub Desktop.
struct GridView: View {
var lineWidth: CGFloat = 1.5
var color = Color(
.displayP3,
red: 225/255,
green: 225/255,
blue: 225/255,
opacity: 1.0
)
var spacing: CGSize = CGSize(
width: 48,
height: 48
)
var body: some View {
GeometryReader { geometry in
Path { path in
// Draw vertical grid lines
for offsetX in stride(from: -lineWidth, to: geometry.size.width, by: self.spacing.width + lineWidth) {
path.move(to: CGPoint(x: offsetX, y: 0))
path.addLine(to: CGPoint(x: offsetX, y: geometry.size.height))
}
// Draw horizontal grid lines
for offsetY in stride(from: 0, to: geometry.size.height, by: self.spacing.height) {
path.move(to: CGPoint(x: 0, y: offsetY))
path.addLine(to: CGPoint(x: geometry.size.height, y: offsetY))
}
}
.stroke(color, lineWidth: lineWidth)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment