Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // LoginViewController.swift
- // First screen
- //
- // Created by karolis on 21/09/2019.
- // Copyright © 2019 karolis. All rights reserved.
- //
- import UIKit
- class DrawingController: UIView {
- override func draw(_ rect: CGRect) {
- super.draw(rect)
- guard let context = UIGraphicsGetCurrentContext() else { return }
- context.setStrokeColor(UIColor.red.cgColor)
- context.setLineWidth(10)
- context.setLineCap(.butt)
- lines.forEach { (line) in
- for (i, p) in line.enumerated() {
- if i == 0 {
- context.move(to: p)
- } else {
- context.addLine(to: p)
- }
- }
- }
- context.strokePath()
- }
- var lines = [[CGPoint]]()
- override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
- lines.append([CGPoint]())
- }
- override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
- guard let point = touches.first?.location(in: nil) else { return }
- guard var lastLine = lines.popLast() else { return }
- lastLine.append(point)
- lines.append(lastLine)
- setNeedsDisplay()
- }
- }
- class ViewController: UIViewController {
- let canvas = DrawingController()
- override func viewDidLoad() {
- super.viewDidLoad()
- view.addSubview(canvas)
- canvas.backgroundColor = .white
- canvas.frame = view.frame
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement