49 lines
1.3 KiB
Swift
49 lines
1.3 KiB
Swift
//
|
|
// GridView.swift
|
|
// Tetris
|
|
//
|
|
// Created by Shadowfacts on 10/14/19.
|
|
// Copyright © 2019 Shadowfacts. All rights reserved.
|
|
//
|
|
|
|
import SwiftUI
|
|
|
|
struct GridView<Cell>: View where Cell: View {
|
|
|
|
let rows: Int
|
|
let columns: Int
|
|
let cellProvider: (Int, Int, CGFloat) -> Cell
|
|
|
|
init(rows: Int, columns: Int, @ViewBuilder cellProvider: @escaping (Int, Int, CGFloat) -> Cell) {
|
|
self.rows = rows
|
|
self.columns = columns
|
|
self.cellProvider = cellProvider
|
|
}
|
|
|
|
var body: some View {
|
|
GeometryReader { (geometry) in
|
|
VStack(spacing: 0) {
|
|
ForEach(0..<self.rows, id: \.self) { (row) in
|
|
HStack(spacing: 0) {
|
|
ForEach(0..<self.columns, id: \.self) { (col) in
|
|
self.cellProvider(col, row, self.cellSize(for: geometry))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func cellSize(for geometry: GeometryProxy) -> CGFloat {
|
|
min(geometry.size.width / CGFloat(columns), geometry.size.height / CGFloat(rows))
|
|
}
|
|
}
|
|
|
|
struct GridView_Previews: PreviewProvider {
|
|
static var previews: some View {
|
|
GridView(rows: 3, columns: 3) { (col, row, size) in
|
|
Rectangle().frame(width: size, height: size).foregroundColor(Color.red)
|
|
}
|
|
}
|
|
}
|