-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
UserCourseView.swift
193 lines (167 loc) · 9.28 KB
/
UserCourseView.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
import UIKit
import Turf
import MapLibre
let PuckSize: CGFloat = 45
let ArrowSize = PuckSize * 0.6
/**
A view that represents the user’s location and course on a `NavigationMapView`.
*/
@objc(MBUserCourseView)
public protocol UserCourseView where Self: UIView {
@objc optional var location: CLLocation { get set }
@objc optional var direction: CLLocationDegrees { get set }
@objc optional var pitch: CLLocationDegrees { get set }
/**
Updates the view to reflect the given location and other camera properties.
*/
@objc optional func update(location: CLLocation, pitch: CGFloat, direction: CLLocationDegrees, animated: Bool, tracksUserCourse: Bool)
}
extension UIView {
func applyDefaultUserPuckTransformation(location: CLLocation, pitch: CGFloat, direction: CLLocationDegrees, animated: Bool, tracksUserCourse: Bool) {
let duration: TimeInterval = animated ? 1 : 0
UIView.animate(withDuration: duration, delay: 0, options: [.beginFromCurrentState, .curveLinear], animations: {
let angle = tracksUserCourse ? 0 : CLLocationDegrees(direction - location.course)
self.layer.setAffineTransform(CGAffineTransform.identity.rotated(by: -CGFloat(angle.toRadians())))
var transform = CATransform3DRotate(CATransform3DIdentity, CGFloat(CLLocationDegrees(pitch).toRadians()), 1.0, 0, 0)
transform = CATransform3DScale(transform, tracksUserCourse ? 1 : 0.5, tracksUserCourse ? 1 : 0.5, 1)
transform.m34 = -1.0 / 1000 // (-1 / distance to projection plane)
self.layer.sublayerTransform = transform
}, completion: nil)
}
}
/**
A view representing the user’s location on screen.
*/
@objc(MBUserPuckCourseView)
public class UserPuckCourseView: UIView, UserCourseView {
/**
Transforms the location of the user puck.
*/
public func update(location: CLLocation, pitch: CGFloat, direction: CLLocationDegrees, animated: Bool, tracksUserCourse: Bool) {
let duration: TimeInterval = animated ? 1 : 0
UIView.animate(withDuration: duration, delay: 0, options: [.beginFromCurrentState, .curveLinear], animations: {
let angle = tracksUserCourse ? 0 : CLLocationDegrees(direction - location.course)
self.puckView.layer.setAffineTransform(CGAffineTransform.identity.rotated(by: -CGFloat(angle.toRadians())))
var transform = CATransform3DRotate(CATransform3DIdentity, CGFloat(CLLocationDegrees(pitch).toRadians()), 1.0, 0, 0)
transform = CATransform3DScale(transform, tracksUserCourse ? 1 : 0.5, tracksUserCourse ? 1 : 0.5, 1)
transform.m34 = -1.0 / 1000 // (-1 / distance to projection plane)
self.layer.sublayerTransform = transform
}, completion: nil)
}
// Sets the color on the user puck
@objc public dynamic var puckColor: UIColor = #colorLiteral(red: 0.149, green: 0.239, blue: 0.341, alpha: 1) {
didSet {
puckView.puckColor = puckColor
}
}
// Sets the fill color on the circle around the user puck
@objc public dynamic var fillColor: UIColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1) {
didSet {
puckView.fillColor = fillColor
}
}
// Sets the shadow color around the user puck
@objc public dynamic var shadowColor: UIColor = #colorLiteral(red: 0.149, green: 0.239, blue: 0.341, alpha: 0.16) {
didSet {
puckView.shadowColor = shadowColor
}
}
var puckView: UserPuckStyleKitView!
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
func commonInit() {
puckView = UserPuckStyleKitView(frame: bounds)
backgroundColor = .clear
puckView.backgroundColor = .clear
addSubview(puckView)
}
}
class UserPuckStyleKitView: UIView {
var fillColor: UIColor = UIColor(red: 1.000, green: 1.000, blue: 1.000, alpha: 1.000) {
didSet {
setNeedsDisplay()
}
}
var puckColor: UIColor = UIColor(red: 0.149, green: 0.239, blue: 0.341, alpha: 1.000) {
didSet {
setNeedsDisplay()
}
}
var shadowColor: UIColor = UIColor(red: 0.149, green: 0.239, blue: 0.341, alpha: 0.160) {
didSet {
setNeedsDisplay()
}
}
override func draw(_ rect: CGRect) {
super.draw(rect)
drawNavigation_puck(fillColor: fillColor, puckColor: puckColor, shadowColor: shadowColor, circleColor: fillColor)
}
func drawNavigation_puck(fillColor: UIColor = UIColor(red: 1.000, green: 1.000, blue: 1.000, alpha: 1.000), puckColor: UIColor = UIColor(red: 0.149, green: 0.239, blue: 0.341, alpha: 1.000), shadowColor: UIColor = UIColor(red: 0.149, green: 0.239, blue: 0.341, alpha: 0.160), circleColor: UIColor = UIColor(red: 1.000, green: 1.000, blue: 1.000, alpha: 1.000)) {
//// Canvas 2
//// navigation_pluck
//// Oval 7
//// path0_fill Drawing
let path0_fillPath = UIBezierPath(ovalIn: CGRect(x: 9, y: 9, width: 57, height: 57))
fillColor.setFill()
path0_fillPath.fill()
//// Group 4
//// path1_stroke_2x Drawing
let path1_stroke_2xPath = UIBezierPath()
path1_stroke_2xPath.move(to: CGPoint(x: 37.5, y: 75))
path1_stroke_2xPath.addCurve(to: CGPoint(x: 75, y: 37.5), controlPoint1: CGPoint(x: 58.21, y: 75), controlPoint2: CGPoint(x: 75, y: 58.21))
path1_stroke_2xPath.addLine(to: CGPoint(x: 57, y: 37.5))
path1_stroke_2xPath.addCurve(to: CGPoint(x: 37.5, y: 57), controlPoint1: CGPoint(x: 57, y: 48.27), controlPoint2: CGPoint(x: 48.27, y: 57))
path1_stroke_2xPath.addLine(to: CGPoint(x: 37.5, y: 75))
path1_stroke_2xPath.close()
path1_stroke_2xPath.move(to: CGPoint(x: 75, y: 37.5))
path1_stroke_2xPath.addCurve(to: CGPoint(x: 37.5, y: 0), controlPoint1: CGPoint(x: 75, y: 16.79), controlPoint2: CGPoint(x: 58.21, y: 0))
path1_stroke_2xPath.addLine(to: CGPoint(x: 37.5, y: 18))
path1_stroke_2xPath.addCurve(to: CGPoint(x: 57, y: 37.5), controlPoint1: CGPoint(x: 48.27, y: 18), controlPoint2: CGPoint(x: 57, y: 26.73))
path1_stroke_2xPath.addLine(to: CGPoint(x: 75, y: 37.5))
path1_stroke_2xPath.close()
path1_stroke_2xPath.move(to: CGPoint(x: 37.5, y: 0))
path1_stroke_2xPath.addCurve(to: CGPoint(x: 0, y: 37.5), controlPoint1: CGPoint(x: 16.79, y: 0), controlPoint2: CGPoint(x: 0, y: 16.79))
path1_stroke_2xPath.addLine(to: CGPoint(x: 18, y: 37.5))
path1_stroke_2xPath.addCurve(to: CGPoint(x: 37.5, y: 18), controlPoint1: CGPoint(x: 18, y: 26.73), controlPoint2: CGPoint(x: 26.73, y: 18))
path1_stroke_2xPath.addLine(to: CGPoint(x: 37.5, y: 0))
path1_stroke_2xPath.close()
path1_stroke_2xPath.move(to: CGPoint(x: 0, y: 37.5))
path1_stroke_2xPath.addCurve(to: CGPoint(x: 37.5, y: 75), controlPoint1: CGPoint(x: 0, y: 58.21), controlPoint2: CGPoint(x: 16.79, y: 75))
path1_stroke_2xPath.addLine(to: CGPoint(x: 37.5, y: 57))
path1_stroke_2xPath.addCurve(to: CGPoint(x: 18, y: 37.5), controlPoint1: CGPoint(x: 26.73, y: 57), controlPoint2: CGPoint(x: 18, y: 48.27))
path1_stroke_2xPath.addLine(to: CGPoint(x: 0, y: 37.5))
path1_stroke_2xPath.close()
shadowColor.setFill()
path1_stroke_2xPath.fill()
//// path0_fill 2 Drawing
let path0_fill2Path = UIBezierPath(ovalIn: CGRect(x: 9, y: 9, width: 57, height: 57))
circleColor.setFill()
path0_fill2Path.fill()
//// Page 1
//// Fill 1
//// path3_fill Drawing
let path3_fillPath = UIBezierPath()
path3_fillPath.move(to: CGPoint(x: 39.2, y: 28.46))
path3_fillPath.addCurve(to: CGPoint(x: 38.02, y: 27.69), controlPoint1: CGPoint(x: 39, y: 27.99), controlPoint2: CGPoint(x: 38.54, y: 27.68))
path3_fillPath.addCurve(to: CGPoint(x: 36.8, y: 28.49), controlPoint1: CGPoint(x: 37.5, y: 27.7), controlPoint2: CGPoint(x: 37.02, y: 28.01))
path3_fillPath.addLine(to: CGPoint(x: 27.05, y: 45.83))
path3_fillPath.addCurve(to: CGPoint(x: 27.28, y: 47.26), controlPoint1: CGPoint(x: 26.83, y: 46.32), controlPoint2: CGPoint(x: 26.92, y: 46.89))
path3_fillPath.addCurve(to: CGPoint(x: 28.71, y: 47.54), controlPoint1: CGPoint(x: 27.65, y: 47.64), controlPoint2: CGPoint(x: 28.21, y: 47.75))
path3_fillPath.addLine(to: CGPoint(x: 37.07, y: 44.03))
path3_fillPath.addCurve(to: CGPoint(x: 38.06, y: 44.02), controlPoint1: CGPoint(x: 37.39, y: 43.89), controlPoint2: CGPoint(x: 37.75, y: 43.89))
path3_fillPath.addLine(to: CGPoint(x: 46.26, y: 47.34))
path3_fillPath.addCurve(to: CGPoint(x: 47.71, y: 47.03), controlPoint1: CGPoint(x: 46.75, y: 47.54), controlPoint2: CGPoint(x: 47.32, y: 47.42))
path3_fillPath.addCurve(to: CGPoint(x: 48, y: 45.59), controlPoint1: CGPoint(x: 48.09, y: 46.64), controlPoint2: CGPoint(x: 48.2, y: 46.07))
path3_fillPath.addLine(to: CGPoint(x: 39.2, y: 28.46))
path3_fillPath.close()
path3_fillPath.usesEvenOddFillRule = true
puckColor.setFill()
path3_fillPath.fill()
}
}