forked from dehli/TouchDraw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ViewController.swift
95 lines (73 loc) · 2.28 KB
/
ViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//
// ViewController.swift
// TouchDrawDemo
//
// Created by Christian Paul Dehli on 10/4/15.
//
import UIKit
import TouchDraw
class ViewController: UIViewController, TouchDrawViewDelegate {
@IBOutlet var drawView: TouchDrawView!
@IBOutlet weak var undoButton: UIBarButtonItem!
@IBOutlet weak var redoButton: UIBarButtonItem!
@IBOutlet weak var clearButton: UIBarButtonItem!
@IBOutlet weak var eraserButton: UIBarButtonItem!
private static let deltaWidth = CGFloat(2.0)
override func viewDidLoad() {
super.viewDidLoad()
drawView.delegate = self
drawView.setWidth(ViewController.deltaWidth)
undoButton.isEnabled = false
redoButton.isEnabled = false
clearButton.isEnabled = false
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func eraserClicked(_ sender: Any) {
drawView.setColor(nil)
eraserButton.isEnabled = false
}
@IBAction func clear(_ sender: AnyObject) {
drawView.clearDrawing()
}
// Slider can vary from 1 to 10
@IBAction func sliderChanged(_ sender: UISlider) {
let newWidth = CGFloat(sender.value) * ViewController.deltaWidth
drawView.setWidth(newWidth)
}
@IBAction func randomColor(_ sender: AnyObject) {
let r = CGFloat(arc4random() % 255) / 255
let g = CGFloat(arc4random() % 255) / 255
let b = CGFloat(arc4random() % 255) / 255
let color = UIColor(red: r, green: g, blue: b, alpha: 1.0)
drawView.setColor(color)
eraserButton.isEnabled = true
}
@IBAction func undo(_ sender: AnyObject) {
drawView.undo()
}
@IBAction func redo(_ sender: AnyObject) {
drawView.redo()
}
// MARK: - TouchDrawViewDelegate
func undoEnabled() {
self.undoButton.isEnabled = true
}
func undoDisabled() {
self.undoButton.isEnabled = false
}
func redoEnabled() {
self.redoButton.isEnabled = true
}
func redoDisabled() {
self.redoButton.isEnabled = false
}
func clearEnabled() {
self.clearButton.isEnabled = true
}
func clearDisabled() {
self.clearButton.isEnabled = false
}
}