๐Ÿ’œGridLinesShape

SwiftUI โŸฉ Drawing โŸฉ Shape โŸฉ Helper Shapes โŸฉ

โฌ†๏ธ ้œ€่ฆ๏ผš floating +โˆ’โจ‰รท int, path.line(), Rectangular

// 2022.02.10

import SwiftUI

/// vertical & horizontal grid lines in a rect.
/// ```
/// GridLinesShape(rows: 3, cols: 3)
/// GridLinesShape(rows: 3) //  3x10 grids
/// GridLinesShape()        // 10x10 grids
/// ```
struct GridLinesShape: Shape {
    var rows: Int = 10
    var cols: Int = 10
    func path(in rect: CGRect) -> Path {
        Path { path in
            // horizontal lines 
            for i in 1..<rows {
                let y: CGFloat = 1.0 / rows * i    // ๐ŸŒ€Int+
                path.line(rect[0,y], rect[1,y])    // ๐ŸŒ€Path+, ๐Ÿ…ฟ๏ธ Rectangular
            }
            // vertical lines
            for i in 1..<cols {
                let x: CGFloat = 1.0 / cols * i
                path.line(rect[x,0], rect[x,1])
            }
        }
    }
}

Last updated