[Swift] I want to draw grid lines (squares)

version

XCode 11.3.1

reference

https://qiita.com/Sa2Knight/items/4debc1f66db8cd958803 This code is summarized in a for in statement.

Completed form

スクリーンショット 2020-02-14 17.19.01.png Grid lines are displayed in UIImageView like this.

File preparation

スクリーンショット 2020-02-14 17.37.54.png

Storyboard preparation

スクリーンショット 2020-02-14 17.30.42.png

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.

code

//
//  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

[Swift] I want to draw grid lines (squares)
[Swift] I want to do something like C's sprinftf
I want to convert characters ...
I want to use FormObject well
I want to convert InputStream to String
I want to docker-compose up Next.js!
I want to create a chat screen for the Swift chat app!
I want to remove the top margin in Grouped UITableView (swift)
I want to develop a web application!
I want to write a nice build.gradle
I want to eliminate duplicate error messages
I want to make an ios.android app
I want to display background-ground-image on heroku.
I want to use DBViewer with Eclipse 2018-12! !!
I want to RSpec even at Jest!
I want to write a unit test!
I want to install PHP 7.2 on Ubuntu 20.04.
I want to stop Java updates altogether
I want to use @Autowired in Servlet
I want to target static fields to @Autowired
I want to do team development remotely
I want to test Action Cable with RSpec test
I want to sort by tab delimited by ruby
I want to output the day of the week
Run R from Java I want to run rJava
I want to send an email in Java.
I want to graduate from npm install properly [2020]
I want to use arrow notation in Ruby
[Ruby] I want to do a method jump!
I want to use java8 forEach with index
I want to var_dump the contents of the intent
I want to pass APP_HOME to logback in Gradle
I want to simply write a repeating string
I want to design a structured exception handling
rsync4j --I want to touch rsync in Java.
I want to play with Firestore from Rails
[Xcode] I want to manage images in folders
I want to be eventually even in kotlin
I want to write quickly from java to sqlite
I want to truncate after the decimal point
I want to reduce simple mistakes. To command yourself.
I want to perform aggregation processing with spring-batch
[Rails] I want to load CSS with webpacker
I want to delete files managed by Git
I want to get the value in Ruby
I want to use Combine in UIKit as well.
I want to call a method of another class
I want to do something like "cls" in Java
[Java] I want to calculate the difference from the date
I want to use NetBeans on Mac → I can use it!
I want to embed any TraceId in the log
Pointcut Expression I want to specify more than one
I want to use fish shell in Laradock too! !!
I want to use ES2015 in Java too! → (´ ・ ω ・ `)
I learned stream (I want to convert List to Map <Integer, List>)
I want to judge the range using the monthly degree
I want to use a little icon in Rails
I want to know the answer of the rock-paper-scissors app
I want to display the name of the poster of the comment
I want to dark mode with the SWT app
I want to monitor a specific file with WatchService