I am very new to Swift. Am trying to draw a grid (like a graph page) on a UIView in Swift. The code is running but I don't see any lines. I don't see what I did wrong.
In the Storyboard I added a View onto the ViewController.
In code I created a GridView.swift and added it to the View above. Here is the code for GridView class. The function draw is being called and the code running without any errors. Its just doing what I thought it would do.
Its not even drawing one line (I remove the for loop and just try to draw one line). This seems so basic that it should work.
import Foundation
import UIKit
class GridView: UIView
{
fileprivate var gridWidthMultiple: CGFloat
{
return 10
}
fileprivate var gridHeightMultiple : CGFloat
{
return 20
}
fileprivate var gridWidth: CGFloat
{
return bounds.width/CGFloat(gridWidthMultiple)
}
fileprivate var gridHeight: CGFloat
{
return bounds.height/CGFloat(gridHeightMultiple)
}
fileprivate var gridCenter: CGPoint {
return CGPoint(x: bounds.midX, y: bounds.midY)
}
fileprivate func drawGrid()
{
let path = UIBezierPath()
path.lineWidth = 5.0
for index in 1...Int(gridWidthMultiple) - 1
{
let start = CGPoint(x: CGFloat(index) * gridWidth, y: 0)
let end = CGPoint(x: CGFloat(index) * gridWidth, y:bounds.height)
path.move(to: start)
path.addLine(to: end)
}
}
override func draw(_ rect: CGRect)
{
drawGrid()
}
}