Skip to content

Instantly share code, notes, and snippets.

@dmytro-anokhin
Created August 19, 2021 05:16
Show Gist options
  • Save dmytro-anokhin/e7b6b9920bf1a594d5692d2a5ad29d97 to your computer and use it in GitHub Desktop.
Save dmytro-anokhin/e7b6b9920bf1a594d5692d2a5ad29d97 to your computer and use it in GitHub Desktop.
Simple grid for SwiftUI
//
// GridView.swift
// GridView
//
// Created by Dmytro Anokhin on 19/08/2021.
//
import SwiftUI
struct GridView: View {
var length: CGFloat = 100.0
var body: some View {
GeometryReader { proxy in
ZStack {
path(size: proxy.size, length: length * 0.25)
.stroke(style: StrokeStyle(lineWidth: 0.5))
.foregroundColor(Color(.displayP3, white: 0.75, opacity: 1.0))
path(size: proxy.size, length: length)
.stroke(style: StrokeStyle(lineWidth: 0.25))
.foregroundColor(Color(.displayP3, white: 0.25, opacity: 1.0))
}
.background(Color.white)
// .drawingGroup()
}
}
func path(size: CGSize, length: CGFloat) -> Path {
Path { path in
var x = length
while x < size.width {
path.move(to: CGPoint(x: x, y: 0.0))
path.addLine(to: CGPoint(x: x, y: size.height))
x += length
}
var y = length
while y < size.height {
path.move(to: CGPoint(x: 0.0, y: y))
path.addLine(to: CGPoint(x: size.width, y: y))
y += length
}
}
}
}
struct GridView_Previews: PreviewProvider {
static var previews: some View {
GridView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment