/
Scribblable.swift
150 lines (113 loc) · 3.47 KB
/
Scribblable.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
//
// Scribblable.swift
// SmoothScribble
//
// Created by Simon Gladman on 05/11/2015.
// Copyright © 2015 Simon Gladman. All rights reserved.
//
import UIKit
// MARK: Scribblable protocol
protocol Scribblable
{
func beginScribble(point: CGPoint)
func appendScribble(point: CGPoint)
func endScribble()
func clearScribble()
}
// MARK: ScribbleView base class
class ScribbleView: UIView
{
let backgroundLayer = CAShapeLayer()
let drawingLayer = CAShapeLayer()
let titleLabel = UILabel()
required init(title: String)
{
super.init(frame: CGRectZero)
backgroundLayer.strokeColor = UIColor.darkGrayColor().CGColor
backgroundLayer.fillColor = nil
backgroundLayer.lineWidth = 2
drawingLayer.strokeColor = UIColor.blackColor().CGColor
drawingLayer.fillColor = nil
drawingLayer.lineWidth = 2
layer.addSublayer(backgroundLayer)
layer.addSublayer(drawingLayer)
titleLabel.text = title
titleLabel.textAlignment = NSTextAlignment.Center
titleLabel.textColor = UIColor.blueColor()
addSubview(titleLabel)
layer.borderColor = UIColor.blueColor().CGColor
layer.borderWidth = 1
layer.masksToBounds = true
}
required init?(coder aDecoder: NSCoder)
{
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews()
{
titleLabel.frame = CGRect(x: 0,
y: frame.height - titleLabel.intrinsicContentSize().height - 2,
width: frame.width,
height: titleLabel.intrinsicContentSize().height)
}
}
// MARK: Simple line drawing from point to point
class SimpleScribbleView: ScribbleView, Scribblable
{
let simplePath = UIBezierPath()
func beginScribble(point: CGPoint)
{
simplePath.removeAllPoints()
simplePath.moveToPoint(point)
}
func appendScribble(point: CGPoint)
{
simplePath.addLineToPoint(point)
drawingLayer.path = simplePath.CGPath
}
func endScribble()
{
if let backgroundPath = backgroundLayer.path
{
simplePath.appendPath(UIBezierPath(CGPath: backgroundPath))
}
backgroundLayer.path = simplePath.CGPath
simplePath.removeAllPoints()
drawingLayer.path = simplePath.CGPath
}
func clearScribble()
{
backgroundLayer.path = nil
}
}
// MARK: Hermite interpolation implementation of ScribbleView
class HermiteScribbleView: ScribbleView, Scribblable
{
let hermitePath = UIBezierPath()
var interpolationPoints = [CGPoint]()
func beginScribble(point: CGPoint)
{
interpolationPoints = [point]
}
func appendScribble(point: CGPoint)
{
interpolationPoints.append(point)
hermitePath.removeAllPoints()
hermitePath.interpolatePointsWithHermite(interpolationPoints)
drawingLayer.path = hermitePath.CGPath
}
func endScribble()
{
if let backgroundPath = backgroundLayer.path
{
hermitePath.appendPath(UIBezierPath(CGPath: backgroundPath))
}
backgroundLayer.path = hermitePath.CGPath
hermitePath.removeAllPoints()
drawingLayer.path = hermitePath.CGPath
}
func clearScribble()
{
backgroundLayer.path = nil
}
}