Created
October 5, 2018 04:20
-
-
Save rockarts/f63cdca3db43cca7ac3cb27dd65d0ddd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class ViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
let draw = DrawView(frame: self.view.bounds) | |
view.addSubview(draw) | |
} | |
} | |
class DrawView: UIView { | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
override func draw( _ rect: CGRect) { | |
let step = 20 | |
let x = Int(bounds.origin.x) | |
let y = Int(bounds.origin.y) | |
let width = Int(bounds.width) | |
let height = Int(bounds.height) | |
for xvalue in stride(from: x, through: width, by: step) { | |
for yvalue in stride(from: y, through: height, by: step) { | |
drawLine(x: xvalue,y: yvalue, width: step, height: step) | |
} | |
} | |
} | |
func drawLine(x:Int, y:Int, width:Int, height:Int) { | |
let leftToRight:Bool = Bool.random() | |
let path = UIBezierPath() | |
if(leftToRight) { | |
path.move(to: CGPoint(x: x, y: y)) | |
path.addLine(to: CGPoint(x: x + width, y: y + height)) | |
} else { | |
path.move(to: CGPoint(x: x + width, y: y)) | |
path.addLine(to: CGPoint(x: x, y: y + height)) | |
} | |
path.close() | |
UIColor.red.set() | |
path.stroke() | |
path.fill() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment