-
Notifications
You must be signed in to change notification settings - Fork 1
/
ViewController.swift
186 lines (134 loc) · 5.77 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//
// ViewController.swift
// CoreGraphics
//
// Created by Julian Moorhouse on 26/08/2019.
// Copyright © 2019 Mindwarp Consultancy Ltd. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
@IBOutlet var imageView: UIImageView!
var currentDrawType = 0
override func viewDidLoad() {
super.viewDidLoad()
drawRectangle()
}
@IBAction func redrawTapped(_ sender: Any) {
currentDrawType += 1
if currentDrawType > 5 {
currentDrawType = 0
}
switch currentDrawType {
case 0:
drawRectangle()
case 1:
drawCircle()
case 2:
drawCheckerBoard()
case 3:
drawRotatedSquares()
case 4:
drawLines()
case 5:
drawImagesAndText()
default:
break
}
}
func drawRectangle() {
let renderer = UIGraphicsImageRenderer(size: CGSize(width: 512, height: 512))
let image = renderer.image { ctx in
let rectangle = CGRect(x: 0, y: 0, width: 512, height: 512)
ctx.cgContext.setFillColor(UIColor.red.cgColor)
ctx.cgContext.setStrokeColor(UIColor.black.cgColor)
ctx.cgContext.setLineWidth(10) // 5 points inside and 5 outside
ctx.cgContext.addRect(rectangle) // does actually draw it
ctx.cgContext.drawPath(using: .fillStroke) // this draws it
}
imageView.image = image
}
func drawCircle() {
let renderer = UIGraphicsImageRenderer(size: CGSize(width: 512, height: 512))
let image = renderer.image { ctx in
let rectangle = CGRect(x: 0, y: 0, width: 512, height: 512).insetBy(dx: 5, dy: 5)
ctx.cgContext.setFillColor(UIColor.red.cgColor)
ctx.cgContext.setStrokeColor(UIColor.black.cgColor)
ctx.cgContext.setLineWidth(10) // 5 points inside and 5 outside
ctx.cgContext.addEllipse(in: rectangle) // does actually draw it
ctx.cgContext.drawPath(using: .fillStroke) // this draws it
}
imageView.image = image
}
func drawCheckerBoard() {
let renderer = UIGraphicsImageRenderer(size: CGSize(width: 512, height: 512))
let image = renderer.image { ctx in
ctx.cgContext.setFillColor(UIColor.black.cgColor)
for row in 0 ..< 8 {
for col in 0 ..< 8 {
if (row + col) % 2 == 0 {
ctx.cgContext.fill(CGRect(x: col * 64, y: row * 64, width: 64, height: 64))
}
}
}
}
imageView.image = image
}
func drawRotatedSquares() {
let renderer = UIGraphicsImageRenderer(size: CGSize(width: 512, height: 512))
let image = renderer.image { ctx in
ctx.cgContext.setFillColor(UIColor.black.cgColor)
// rotate around the center rather than the top left corner
ctx.cgContext.translateBy(x: 256, y: 256)
// how much we want to rotate and how many times
let rotations = 16
let amount = Double.pi / Double(rotations)
for _ in 0 ..< rotations {
ctx.cgContext.rotate(by: CGFloat(amount))
// back and left by 128 because we're drawing from the center
ctx.cgContext.addRect(CGRect(x: -128, y: -128, width: 256, height: 256))
}
ctx.cgContext.setStrokeColor(UIColor.black.cgColor)
ctx.cgContext.strokePath()
}
imageView.image = image
}
func drawLines() {
let renderer = UIGraphicsImageRenderer(size: CGSize(width: 512, height: 512))
let image = renderer.image { ctx in
// rotate around the center rather than the top left corner
ctx.cgContext.translateBy(x: 256, y: 256)
var first = true
var length: CGFloat = 256
for _ in 0 ..< 256 {
ctx.cgContext.rotate(by: .pi / 2) // 90 degress
if first {
ctx.cgContext.move(to: CGPoint(x: length, y: 50))
first = false
} else {
ctx.cgContext.addLine(to: CGPoint(x: length, y: 50))
}
length *= 0.99
}
ctx.cgContext.setStrokeColor(UIColor.black.cgColor)
ctx.cgContext.strokePath()
}
imageView.image = image
}
func drawImagesAndText() {
let renderer = UIGraphicsImageRenderer(size: CGSize(width: 512, height: 512))
let image = renderer.image { ctx in
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
let attrs: [NSAttributedString.Key: Any] = [
.font: UIFont.systemFont(ofSize: 36),
.paragraphStyle: paragraphStyle
]
let string = "The best-laid schemes o'\nmice an' me gang aft agley"
let attributedString = NSAttributedString(string: string, attributes: attrs)
attributedString.draw(with: CGRect(x: 32, y: 32, width: 448, height: 448), options: .usesLineFragmentOrigin, context: nil)
let mouse = UIImage(named: "mouse")
mouse?.draw(at: CGPoint(x: 300, y: 150))
}
imageView.image = image
}
}