|
23 | 23 | import UIKit |
24 | 24 |
|
25 | 25 | class SPQRFrameLayer: CAShapeLayer { |
26 | | - private let cLength: CGFloat |
27 | | - private let cRadius: CGFloat |
28 | 26 |
|
29 | 27 | // MARK: - Init |
30 | 28 |
|
31 | | - init( |
32 | | - length: CGFloat = 16.0, |
33 | | - radius: CGFloat = 16.0, |
34 | | - lineWidth: CGFloat = 5.0, |
35 | | - lineColor: UIColor = .systemYellow |
36 | | - ) { |
37 | | - self.cLength = length |
38 | | - self.cRadius = radius |
39 | | - |
| 29 | + override init() { |
40 | 30 | super.init() |
41 | | - |
42 | | - self.strokeColor = lineColor.cgColor |
43 | | - self.fillColor = UIColor.clear.cgColor |
44 | | - |
45 | | - self.lineWidth = lineWidth |
| 31 | + strokeColor = UIColor.systemYellow.cgColor |
| 32 | + lineWidth = 5 |
| 33 | + fillColor = UIColor.clear.cgColor |
46 | 34 | } |
47 | 35 |
|
48 | 36 | required init?(coder: NSCoder) { |
49 | 37 | fatalError("init(coder:) has not been implemented") |
50 | 38 | } |
51 | 39 |
|
52 | | - override func action(forKey event: String) -> CAAction? { |
53 | | - if event == "path" { |
54 | | - let animation: CABasicAnimation = .init(keyPath: event) |
55 | | - |
56 | | - animation.duration = 0.3 |
57 | | - animation.timingFunction = CATransaction.animationTimingFunction() |
58 | | - |
59 | | - return animation |
60 | | - } |
61 | | - |
62 | | - return super.action(forKey: event) |
63 | | - } |
64 | | - |
65 | 40 | // MARK: - Actions |
66 | 41 |
|
67 | | - func update(using points: [CGPoint]) { |
68 | | - let corners = buildCorners(for: points) |
69 | | - |
70 | | - let framePath: UIBezierPath = .init() |
71 | | - |
72 | | - for corner in corners { |
73 | | - guard let cStartPoint = corner.startPoint(using: corners), |
74 | | - let cPreCurvePoint = corner.preCurvePoint(using: corners), |
75 | | - let cPostCurvePoint = corner.postCurvePoint(using: corners), |
76 | | - let cEndPoint = corner.endPoint(using: corners) |
77 | | - else { return } |
78 | | - |
79 | | - framePath.move(to: cStartPoint) |
80 | | - framePath.addLine(to: cPreCurvePoint) |
81 | | - framePath.addQuadCurve(to: cPostCurvePoint, controlPoint: corner.point) |
82 | | - framePath.addLine(to: cEndPoint) |
83 | | - } |
84 | | - |
85 | | - path = framePath.cgPath |
| 42 | + func detected(with newBezierPath: UIBezierPath) { |
| 43 | + path = newBezierPath.cgPath |
86 | 44 | } |
87 | 45 |
|
88 | | - func dissapear() { |
89 | | - path = nil |
| 46 | + func update(with newBezierPath: UIBezierPath) { |
| 47 | + path = newBezierPath.cgPath |
90 | 48 | } |
91 | 49 |
|
92 | | - private func buildCorners(for points: [CGPoint]) -> [SPQRCorner] { |
93 | | - var corners: [SPQRCorner] = .init() |
94 | | - |
95 | | - for corner in SPQRCorner.Kind.allCases { |
96 | | - corners.append( |
97 | | - .init( |
98 | | - kind: corner, |
99 | | - point: points[corner.rawValue], |
100 | | - length: cLength, |
101 | | - radius: cRadius |
102 | | - ) |
103 | | - ) |
104 | | - } |
105 | | - |
106 | | - return corners |
| 50 | + func dissapear() { |
| 51 | + path = nil |
107 | 52 | } |
108 | 53 | } |
0 commit comments