-
Notifications
You must be signed in to change notification settings - Fork 74
/
Shape.swift
371 lines (324 loc) · 12.9 KB
/
Shape.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// Copyright © 2014 C4
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions: The above copyright
// notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
import QuartzCore
import UIKit
/// Shape is a concrete subclass of View that draws a bezier path in its coordinate space.
open class Shape: View {
internal class ShapeView: UIView {
var shapeLayer: ShapeLayer {
return self.layer as! ShapeLayer // swiftlint:disable:this force_cast
}
override class var layerClass: AnyClass {
return ShapeLayer.self
}
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
guard let path = shapeLayer.path else {
return nil
}
let fillRule = shapeLayer.fillRule == kCAFillRuleNonZero ? CGPathFillRule.evenOdd : CGPathFillRule.winding
if path.contains(point, using: fillRule, transform: CGAffineTransform.identity) {
return self
}
return nil
}
}
/// Shape's contents are drawn on a ShapeLayer.
open var shapeLayer: ShapeLayer {
return self.shapeView.shapeLayer
}
internal var shapeView: ShapeView {
return self.view as! ShapeView // swiftlint:disable:this force_cast
}
/// Initializes an empty Shape.
public override init() {
super.init()
self.view = ShapeView(frame: CGRect(frame))
strokeColor = C4Purple
fillColor = C4Blue
lineWidth = 1
lineCap = .Round
lineJoin = .Round
let image = UIImage.createWithColor(UIColor.clear, size: CGSize(width: 1, height: 1)).cgImage
shapeLayer.contents = image
}
/// Initializes a new Shape from a specified Path.
/// - parameter path: A Path around which the new shape is created with the frame of the new shape fitting the path on
/// screen.
public convenience init(_ path: Path) {
self.init()
self.path = path
shapeLayer.path = path.CGPath
updatePath()
adjustToFitPath()
}
public override init(frame: Rect) {
super.init()
self.view = ShapeView(frame: CGRect(frame))
strokeColor = C4Purple
fillColor = C4Blue
lineWidth = 1
lineCap = .Round
lineJoin = .Round
let image = UIImage.createWithColor(UIColor.clear, size: CGSize(width: 1, height: 1)).cgImage
shapeLayer.contents = image
}
/// Initializes a new Shape from the properties of another Shape. Essentially, this copies the provided shape.
/// - parameter shape: A Shape around which the new shape is created.
public convenience init(copy original: Shape) {
//If there is a scale transform we need to undo that
let t = original.view.transform.inverted()
let x = sqrt(t.a * t.a + t.c * t.c)
let y = sqrt(t.b * t.b + t.d * t.d)
let s = CGAffineTransform(scaleX: x, y: y)
self.init(frame: Rect(original.view.frame.applying(s)))
let disable = ShapeLayer.disableActions
ShapeLayer.disableActions = true
self.path = original.path
shapeLayer.path = path?.CGPath
self.lineWidth = original.lineWidth
self.lineDashPhase = original.lineDashPhase
self.lineCap = original.lineCap
self.lineJoin = original.lineJoin
self.lineDashPattern = original.lineDashPattern
self.fillColor = original.fillColor
self.strokeColor = original.strokeColor
self.strokeStart = original.strokeStart
self.strokeEnd = original.strokeEnd
self.miterLimit = original.miterLimit
updatePath()
adjustToFitPath()
copyViewStyle(original)
ShapeLayer.disableActions = disable
}
///An optional variable representing a gradient. If this is non-nil, then the shape will appear to be filled with a gradient.
public var gradientFill: Gradient? {
didSet {
guard let gradientFill = gradientFill else {
fillColor = clear
return
}
let gim = gradientFill.render()?.cgImage
//inverts coordinate for graphics context rendering
var b = bounds
b.origin.y = self.height - b.origin.y
UIGraphicsBeginImageContextWithOptions(CGSize(b.size), false, UIScreen.main.scale)
let context = UIGraphicsGetCurrentContext()
context?.draw(gim!, in: CGRect(b), byTiling: true)
let uiimage = UIGraphicsGetImageFromCurrentImageContext()
let uicolor = UIColor(patternImage: uiimage!)
fillColor = Color(uicolor)
UIGraphicsEndImageContext()
}
}
/// The path defining the shape to be rendered. If the path extends outside the layer bounds it will not automatically
/// be clipped to the layer. Defaults to nil. Animatable.
public var path: Path? {
didSet {
shapeLayer.path = path?.CGPath
}
}
internal func updatePath() {}
/// Adjusts the shape's frame to the bounding bounding box of its specified path.
public func adjustToFitPath() {
if shapeLayer.path == nil {
return
}
view.bounds = (shapeLayer.path?.boundingBoxOfPath)!
view.frame = view.bounds
}
/// Returns the receiver's frame. Animatable.
public override var frame: Rect {
get {
return Rect(view.frame)
}
set {
view.frame = CGRect(newValue)
updatePath()
}
}
/// The line width used when stroking the path. Defaults to 1.0. Animatable.
@IBInspectable
public var lineWidth: Double {
get { return Double(shapeLayer.lineWidth) }
set(width) { shapeLayer.lineWidth = CGFloat(width) }
}
/// The color to stroke the path, or nil for no fill. Defaults to opaque black. Animatable.
public var strokeColor: Color? {
get {
return shapeLayer.strokeColor.map({ Color($0) })
}
set(color) {
shapeLayer.strokeColor = color?.cgColor
}
}
/// The color to fill the path, or nil for no fill. Defaults to opaque black. Animatable.
public var fillColor: Color? {
get {
return shapeLayer.fillColor.map({ Color($0) })
}
set(color) {
shapeLayer.fillColor = color?.cgColor
}
}
/// The fill rule used when filling the path. Defaults to `NonZero`.
public var fillRule: FillRule {
get {
switch shapeLayer.fillRule {
case kCAFillRuleNonZero:
return .NonZero
case kCAFillRuleEvenOdd:
return .EvenOdd
default:
return .NonZero
}
}
set(fillRule) {
switch fillRule {
case .NonZero:
shapeLayer.fillRule = kCAFillRuleNonZero
case .EvenOdd:
shapeLayer.fillRule = kCAFillRuleEvenOdd
}
}
}
/// The current rotation value of the view. Animatable.
/// - returns: A Double value representing the cumulative rotation of the view, measured in Radians.
open override var rotation: Double {
get {
if let number = shapeLayer.value(forKeyPath: Layer.rotationKey) as? NSNumber {
return number.doubleValue
}
return 0.0
}
set {
shapeLayer.setValue(newValue, forKeyPath: Layer.rotationKey)
}
}
/// This value defines the start of the path used to draw the stroked outline. The value must be in the range [0,1]
/// with zero representing the start of the path and one the end. Values in between zero and one are interpolated
/// linearly along the path length. Defaults to zero. Animatable.
public var strokeStart: Double {
get { return Double(shapeLayer.strokeStart) }
set(start) { shapeLayer.strokeStart = CGFloat(start) }
}
/// This value defines the end of the path used to draw the stroked outline. The value must be in the range [0,1]
/// with zero representing the start of the path and one the end. Values in between zero and one are interpolated
/// linearly along the path length. Defaults to 1.0. Animatable.
public var strokeEnd: Double {
get { return Double(shapeLayer.strokeEnd) }
set(end) { shapeLayer.strokeEnd = CGFloat(end) }
}
/// The miter limit used when stroking the path. Defaults to ten. Animatable.
@IBInspectable
public var miterLimit: Double {
get { return Double(shapeLayer.miterLimit) }
set(miterLimit) { shapeLayer.miterLimit = CGFloat(miterLimit) }
}
/// The cap style used when stroking the path. Defaults to `Butt`.
public var lineCap: LineCap {
get {
switch shapeLayer.lineCap {
case kCALineCapRound:
return .Round
case kCALineCapSquare:
return .Square
default:
return .Butt
}
}
set(lineCap) {
switch lineCap {
case .Butt:
shapeLayer.lineCap = kCALineCapButt
case .Round:
shapeLayer.lineCap = kCALineCapRound
case .Square:
shapeLayer.lineCap = kCALineCapSquare
}
}
}
/// The join style used when stroking the path. Defaults to `Miter`.
public var lineJoin: LineJoin {
get {
switch shapeLayer.lineJoin {
case kCALineJoinRound:
return .Round
case kCALineJoinBevel:
return .Bevel
default:
return .Miter
}
}
set(lineJoin) {
switch lineJoin {
case .Miter:
shapeLayer.lineJoin = kCALineJoinMiter
case .Round:
shapeLayer.lineJoin = kCALineJoinRound
case .Bevel:
shapeLayer.lineJoin = kCALineJoinBevel
}
}
}
/// The phase of the dashing pattern applied when creating the stroke. Defaults to zero. Animatable.
public var lineDashPhase: Double {
get { return Double(shapeLayer.lineDashPhase) }
set(phase) { shapeLayer.lineDashPhase = CGFloat(phase) }
}
/// The dash pattern applied when creating the stroked version of the path. Defaults to nil.
public var lineDashPattern: [NSNumber]? {
get { return shapeLayer.lineDashPattern as [NSNumber]? }
set(pattern) { shapeLayer.lineDashPattern = pattern }
}
/// The size of the receiver including the width of its stroke.
/// - returns: The bounding box that surrounds the shape and its line.
public func intrinsicContentSize() -> CGSize {
if let path = path {
let boundingBox = path.boundingBox()
return CGSize(width: boundingBox.max.x + lineWidth/2, height: boundingBox.max.y + lineWidth/2)
} else {
return CGSize()
}
}
/// Returns true if there is no path.
public func isEmpty() -> Bool {
return path?.isEmpty() ?? true
}
/// The join style for joints on the shape's path.
public enum LineJoin {
/// Specifies a miter line shape of the joints between connected segments of a stroked path.
case Miter
/// Specifies a round line shape of the joints between connected segments of a stroked path.
case Round
/// Specifies a bevel line shape of the joints between connected segments of a stroked path.
case Bevel
}
/// The cap style for the ends of the shape's path.
public enum LineCap {
/// Specifies a butt line cap style for endpoints for an open path when stroked.
case Butt
/// Specifies a round line cap style for endpoints for an open path when stroked.
case Round
/// Specifies a square line cap style for endpoints for an open path when stroked.
case Square
}
public override func hitTest(_ point: Point) -> Bool {
return path?.containsPoint(point) ?? false
}
}