XCode 11.3.1
https://qiita.com/Sa2Knight/items/4debc1f66db8cd958803 This code is summarized in a for in statement.
After placing the ImageView on the Storyboard, I'll set the class to GridView. (First file created) Also, ** Since the grid line to be displayed is white, change the background color to something other than white, Change the color of the grid lines. ** ** From here, we will write the code in the Grid.swift file created first.
//
// Grid.swift
import UIKit
class GridView: UIView {
//splitCount should be even
//Vertical line:Number of splitCount- 1
//horizontal line:Number of splitCount/ 2 - 1
let splitCount = 10
override func draw(_ rect: CGRect) {
let path = UIBezierPath()
path.lineWidth = 1.5
UIColor.white.setStroke()
for x in 0...splitCount {
for y in 0...splitCount {
if x != y, x == 0, y < splitCount {
path.move(to: getPoint(rect, x: CGFloat(x), y: CGFloat(y)))
path.addLine(to: getPoint(rect, x: CGFloat(splitCount), y: CGFloat(y)))
path.stroke()
} else if x < splitCount, x % 2 == 0, x != 0, y == 0 {
path.move(to: getPoint(rect, x: CGFloat(x), y: CGFloat(y)))
path.addLine(to: getPoint(rect, x: CGFloat(x), y: CGFloat(splitCount)))
path.stroke()
}
}
}
}
/*Get the coordinates of the specified partition on the View*/
private func getPoint(_ rect: CGRect, x: CGFloat, y: CGFloat) -> CGPoint {
let width = rect.width / CGFloat(splitCount)
let height = rect.height / CGFloat(splitCount)
return CGPoint(x: width * x, y: height * y)
}
}
Just change the value of the constant splitCount You can change the number of grid lines.
Recommended Posts