Skip to content

Commit 76a6152

Browse files
authored
Use UserPuckStyleKit to draw UserPuckStyleKitView. (#3800)
1 parent 1829405 commit 76a6152

File tree

4 files changed

+144
-81
lines changed

4 files changed

+144
-81
lines changed

MapboxNavigation.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@
367367
B4E6711F27BB09D6004EE9C1 /* SpriteInfoCacheTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B4E6711E27BB09D6004EE9C1 /* SpriteInfoCacheTests.swift */; };
368368
B4E6712227BB0A56004EE9C1 /* SpriteInfoCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = B4E6712027BB09EF004EE9C1 /* SpriteInfoCache.swift */; };
369369
B4F4FEAD27AB1E11003915A9 /* SpriteRepositoryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B4B61B9627A882E200AA127E /* SpriteRepositoryTests.swift */; };
370+
B4FD842827ED1980002662C4 /* UserPuckStyleKit.swift in Sources */ = {isa = PBXBuildFile; fileRef = B4FD842727ED1980002662C4 /* UserPuckStyleKit.swift */; };
370371
C51511D120EAC89D00372A91 /* CPMapTemplate.swift in Sources */ = {isa = PBXBuildFile; fileRef = C51511D020EAC89D00372A91 /* CPMapTemplate.swift */; };
371372
C51DF8661F38C31C006C6A15 /* Locale.swift in Sources */ = {isa = PBXBuildFile; fileRef = C51DF8651F38C31C006C6A15 /* Locale.swift */; };
372373
C520EE901EBB84F9008805BC /* Navigation.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = C520EE921EBB84F9008805BC /* Navigation.storyboard */; };
@@ -942,6 +943,7 @@
942943
B4E19C46268114840011581F /* NavigationLocationProvider.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NavigationLocationProvider.swift; sourceTree = "<group>"; };
943944
B4E6711E27BB09D6004EE9C1 /* SpriteInfoCacheTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SpriteInfoCacheTests.swift; sourceTree = "<group>"; };
944945
B4E6712027BB09EF004EE9C1 /* SpriteInfoCache.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SpriteInfoCache.swift; sourceTree = "<group>"; };
946+
B4FD842727ED1980002662C4 /* UserPuckStyleKit.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserPuckStyleKit.swift; sourceTree = "<group>"; };
945947
C51511D020EAC89D00372A91 /* CPMapTemplate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CPMapTemplate.swift; sourceTree = "<group>"; };
946948
C51DF8651F38C31C006C6A15 /* Locale.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Locale.swift; sourceTree = "<group>"; };
947949
C520EE911EBB84F9008805BC /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Navigation.storyboard; sourceTree = "<group>"; };
@@ -1372,6 +1374,7 @@
13721374
B44177F72649B08400781319 /* UserLocationStyle.swift */,
13731375
35B1E2941F1FF8EC00A13D32 /* UserCourseView.swift */,
13741376
B430D2F925534FDC0088CC23 /* UserHaloCourseView.swift */,
1377+
B4FD842727ED1980002662C4 /* UserPuckStyleKit.swift */,
13751378
);
13761379
name = Puck;
13771380
sourceTree = "<group>";
@@ -2742,6 +2745,7 @@
27422745
35379D0321480E5700FD402E /* RecentItem.swift in Sources */,
27432746
8AD2211D27C43C13000734A5 /* StylableButton.swift in Sources */,
27442747
1FFDFD92249C1AA80091746A /* JunctionView.swift in Sources */,
2748+
B4FD842827ED1980002662C4 /* UserPuckStyleKit.swift in Sources */,
27452749
8A41F63D25BF631500BD6FCF /* NavigationMapView+BuildingHighlighting.swift in Sources */,
27462750
);
27472751
runOnlyForDeploymentPostprocessing = 0;

Sources/MapboxNavigation/UserCourseView.swift

Lines changed: 1 addition & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -233,79 +233,6 @@ class UserPuckStyleKitView: UIView {
233233

234234
override func draw(_ rect: CGRect) {
235235
super.draw(rect)
236-
drawNavigationPuck(fillColor: fillColor, puckColor: drawingPuckColor(), shadowColor: shadowColor, circleColor: fillColor)
237-
}
238-
239-
func drawNavigationPuck(fillColor: UIColor = UIColor(red: 1.000, green: 1.000, blue: 1.000, alpha: 1.000),
240-
puckColor: UIColor = UIColor(red: 0.149, green: 0.239, blue: 0.341, alpha: 1.000),
241-
shadowColor: UIColor = UIColor(red: 0.149, green: 0.239, blue: 0.341, alpha: 0.160),
242-
circleColor: UIColor = UIColor(red: 1.000, green: 1.000, blue: 1.000, alpha: 1.000)) {
243-
244-
let widthRatio = bounds.width / 75
245-
let heightRatio = bounds.height / 75
246-
247-
//// Canvas 2
248-
//// navigation_pluck
249-
//// Oval 7
250-
//// path0_fill Drawing
251-
let path0_fillPath = UIBezierPath(ovalIn: CGRect(x: 9 * widthRatio, y: 9 * heightRatio, width: 57 * widthRatio, height: 57 * heightRatio))
252-
fillColor.setFill()
253-
path0_fillPath.fill()
254-
255-
//// Group 4
256-
//// path1_stroke_2x Drawing
257-
let path1_stroke_2xPath = UIBezierPath()
258-
path1_stroke_2xPath.move(to: CGPoint(x: 37.5 * widthRatio, y: 75 * heightRatio))
259-
path1_stroke_2xPath.addCurve(to: CGPoint(x: 75 * widthRatio, y: 37.5 * heightRatio), controlPoint1: CGPoint(x: 58.21 * widthRatio, y: 75 * heightRatio), controlPoint2: CGPoint(x: 75 * widthRatio, y: 58.21 * heightRatio))
260-
path1_stroke_2xPath.addLine(to: CGPoint(x: 57 * widthRatio, y: 37.5 * heightRatio))
261-
path1_stroke_2xPath.addCurve(to: CGPoint(x: 37.5 * widthRatio, y: 57 * heightRatio), controlPoint1: CGPoint(x: 57 * widthRatio, y: 48.27 * heightRatio), controlPoint2: CGPoint(x: 48.27 * widthRatio, y: 57 * heightRatio))
262-
path1_stroke_2xPath.addLine(to: CGPoint(x: 37.5 * widthRatio, y: 75 * heightRatio))
263-
path1_stroke_2xPath.close()
264-
path1_stroke_2xPath.move(to: CGPoint(x: 75 * widthRatio, y: 37.5 * heightRatio))
265-
path1_stroke_2xPath.addCurve(to: CGPoint(x: 37.5 * widthRatio, y: 0), controlPoint1: CGPoint(x: 75 * widthRatio, y: 16.79 * heightRatio), controlPoint2: CGPoint(x: 58.21 * widthRatio, y: 0))
266-
path1_stroke_2xPath.addLine(to: CGPoint(x: 37.5 * widthRatio, y: 18 * heightRatio))
267-
path1_stroke_2xPath.addCurve(to: CGPoint(x: 57 * widthRatio, y: 37.5 * heightRatio), controlPoint1: CGPoint(x: 48.27 * widthRatio, y: 18 * heightRatio), controlPoint2: CGPoint(x: 57 * widthRatio, y: 26.73))
268-
path1_stroke_2xPath.addLine(to: CGPoint(x: 75 * widthRatio, y: 37.5 * heightRatio))
269-
path1_stroke_2xPath.close()
270-
path1_stroke_2xPath.move(to: CGPoint(x: 37.5 * widthRatio, y: 0))
271-
path1_stroke_2xPath.addCurve(to: CGPoint(x: 0, y: 37.5 * heightRatio), controlPoint1: CGPoint(x: 16.79 * widthRatio, y: 0), controlPoint2: CGPoint(x: 0, y: 16.79 * heightRatio))
272-
path1_stroke_2xPath.addLine(to: CGPoint(x: 18 * widthRatio, y: 37.5 * heightRatio))
273-
path1_stroke_2xPath.addCurve(to: CGPoint(x: 37.5 * widthRatio, y: 18 * heightRatio), controlPoint1: CGPoint(x: 18 * widthRatio, y: 26.73 * heightRatio), controlPoint2: CGPoint(x: 26.73 * widthRatio, y: 18 * heightRatio))
274-
path1_stroke_2xPath.addLine(to: CGPoint(x: 37.5 * widthRatio, y: 0))
275-
path1_stroke_2xPath.close()
276-
path1_stroke_2xPath.move(to: CGPoint(x: 0, y: 37.5 * heightRatio))
277-
path1_stroke_2xPath.addCurve(to: CGPoint(x: 37.5 * widthRatio, y: 75 * heightRatio), controlPoint1: CGPoint(x: 0, y: 58.21 * heightRatio), controlPoint2: CGPoint(x: 16.79 * widthRatio, y: 75 * heightRatio))
278-
path1_stroke_2xPath.addLine(to: CGPoint(x: 37.5 * widthRatio, y: 57 * heightRatio))
279-
path1_stroke_2xPath.addCurve(to: CGPoint(x: 18 * widthRatio, y: 37.5 * heightRatio), controlPoint1: CGPoint(x: 26.73 * widthRatio, y: 57 * heightRatio), controlPoint2: CGPoint(x: 18 * widthRatio, y: 48.27 * heightRatio))
280-
path1_stroke_2xPath.addLine(to: CGPoint(x: 0, y: 37.5 * heightRatio))
281-
path1_stroke_2xPath.close()
282-
shadowColor.setFill()
283-
path1_stroke_2xPath.fill()
284-
285-
//// path0_fill 2 Drawing
286-
let path0_fill2Path = UIBezierPath(ovalIn: CGRect(x: 9 * widthRatio, y: 9 * heightRatio, width: 57 * widthRatio, height: 57 * heightRatio))
287-
circleColor.setFill()
288-
path0_fill2Path.fill()
289-
290-
//// Page 1
291-
//// Fill 1
292-
//// path3_fill Drawing
293-
let path3_fillPath = UIBezierPath()
294-
path3_fillPath.move(to: CGPoint(x: 39.2 * widthRatio, y: 28.46 * heightRatio))
295-
path3_fillPath.addCurve(to: CGPoint(x: 38.02 * widthRatio, y: 27.69 * heightRatio), controlPoint1: CGPoint(x: 39 * widthRatio, y: 27.99 * heightRatio), controlPoint2: CGPoint(x: 38.54 * widthRatio, y: 27.68 * heightRatio))
296-
path3_fillPath.addCurve(to: CGPoint(x: 36.8 * widthRatio, y: 28.49 * heightRatio), controlPoint1: CGPoint(x: 37.5 * widthRatio, y: 27.7 * heightRatio), controlPoint2: CGPoint(x: 37.02 * widthRatio, y: 28.01 * heightRatio))
297-
path3_fillPath.addLine(to: CGPoint(x: 27.05 * widthRatio, y: 45.83 * heightRatio))
298-
path3_fillPath.addCurve(to: CGPoint(x: 27.28 * widthRatio, y: 47.26 * heightRatio), controlPoint1: CGPoint(x: 26.83 * widthRatio, y: 46.32 * heightRatio), controlPoint2: CGPoint(x: 26.92 * widthRatio, y: 46.89 * heightRatio))
299-
path3_fillPath.addCurve(to: CGPoint(x: 28.71 * widthRatio, y: 47.54 * heightRatio), controlPoint1: CGPoint(x: 27.65 * widthRatio, y: 47.64 * heightRatio), controlPoint2: CGPoint(x: 28.21 * widthRatio, y: 47.75 * heightRatio))
300-
path3_fillPath.addLine(to: CGPoint(x: 37.07 * widthRatio, y: 44.03 * heightRatio))
301-
path3_fillPath.addCurve(to: CGPoint(x: 38.06 * widthRatio, y: 44.02 * heightRatio), controlPoint1: CGPoint(x: 37.39 * widthRatio, y: 43.89 * heightRatio), controlPoint2: CGPoint(x: 37.75 * widthRatio, y: 43.89 * heightRatio))
302-
path3_fillPath.addLine(to: CGPoint(x: 46.26 * widthRatio, y: 47.34 * heightRatio))
303-
path3_fillPath.addCurve(to: CGPoint(x: 47.71 * widthRatio, y: 47.03 * heightRatio), controlPoint1: CGPoint(x: 46.75 * widthRatio, y: 47.54 * heightRatio), controlPoint2: CGPoint(x: 47.32 * widthRatio, y: 47.42 * heightRatio))
304-
path3_fillPath.addCurve(to: CGPoint(x: 48 * widthRatio, y: 45.59 * heightRatio), controlPoint1: CGPoint(x: 48.09 * widthRatio, y: 46.64 * heightRatio), controlPoint2: CGPoint(x: 48.2 * widthRatio, y: 46.07 * heightRatio))
305-
path3_fillPath.addLine(to: CGPoint(x: 39.2 * widthRatio, y: 28.46 * heightRatio))
306-
path3_fillPath.close()
307-
path3_fillPath.usesEvenOddFillRule = true
308-
puckColor.setFill()
309-
path3_fillPath.fill()
236+
UserPuckStyleKit.drawNavigationPuck(frame: rect, resizing: .aspectFit, fillColor: fillColor, puckColor: drawingPuckColor(), shadowColor: shadowColor, circleColor: fillColor)
310237
}
311238
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
//
2+
// UserPuckStyleKit.swift
3+
// MapboxNavigation
4+
//
5+
// Created on 2017-10-11.
6+
// Copyright © 2017 Mapbox. All rights reserved.
7+
//
8+
// Generated by PaintCode
9+
// http://www.paintcodeapp.com
10+
//
11+
12+
import UIKit
13+
14+
class UserPuckStyleKit : NSObject {
15+
//// Drawing Methods
16+
17+
@objc dynamic class func drawNavigationPuck(frame targetFrame: CGRect = CGRect(x: 0, y: 0, width: 75, height: 75), resizing: ResizingBehavior = .aspectFit, 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)) {
18+
//// General Declarations
19+
let context = UIGraphicsGetCurrentContext()!
20+
21+
//// Resize to Target Frame
22+
context.saveGState()
23+
let resizedFrame: CGRect = resizing.apply(rect: CGRect(x: 0, y: 0, width: 75, height: 75), target: targetFrame)
24+
context.translateBy(x: resizedFrame.minX, y: resizedFrame.minY)
25+
context.scaleBy(x: resizedFrame.width / 75, y: resizedFrame.height / 75)
26+
27+
//// Canvas 2
28+
//// navigation_pluck
29+
//// Oval 7
30+
//// path0_fill Drawing
31+
let path0_fillPath = UIBezierPath(ovalIn: CGRect(x: 9, y: 9, width: 57, height: 57))
32+
fillColor.setFill()
33+
path0_fillPath.fill()
34+
35+
//// Group 4
36+
//// path1_stroke_2x Drawing
37+
let path1_stroke_2xPath = UIBezierPath()
38+
path1_stroke_2xPath.move(to: CGPoint(x: 37.5, y: 75))
39+
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))
40+
path1_stroke_2xPath.addLine(to: CGPoint(x: 57, y: 37.5))
41+
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))
42+
path1_stroke_2xPath.addLine(to: CGPoint(x: 37.5, y: 75))
43+
path1_stroke_2xPath.close()
44+
path1_stroke_2xPath.move(to: CGPoint(x: 75, y: 37.5))
45+
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))
46+
path1_stroke_2xPath.addLine(to: CGPoint(x: 37.5, y: 18))
47+
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))
48+
path1_stroke_2xPath.addLine(to: CGPoint(x: 75, y: 37.5))
49+
path1_stroke_2xPath.close()
50+
path1_stroke_2xPath.move(to: CGPoint(x: 37.5, y: 0))
51+
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))
52+
path1_stroke_2xPath.addLine(to: CGPoint(x: 18, y: 37.5))
53+
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))
54+
path1_stroke_2xPath.addLine(to: CGPoint(x: 37.5, y: 0))
55+
path1_stroke_2xPath.close()
56+
path1_stroke_2xPath.move(to: CGPoint(x: 0, y: 37.5))
57+
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))
58+
path1_stroke_2xPath.addLine(to: CGPoint(x: 37.5, y: 57))
59+
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))
60+
path1_stroke_2xPath.addLine(to: CGPoint(x: 0, y: 37.5))
61+
path1_stroke_2xPath.close()
62+
shadowColor.setFill()
63+
path1_stroke_2xPath.fill()
64+
65+
//// path0_fill 2 Drawing
66+
let path0_fill2Path = UIBezierPath(ovalIn: CGRect(x: 9, y: 9, width: 57, height: 57))
67+
circleColor.setFill()
68+
path0_fill2Path.fill()
69+
70+
//// Page 1
71+
//// Fill 1
72+
//// path3_fill Drawing
73+
let path3_fillPath = UIBezierPath()
74+
path3_fillPath.move(to: CGPoint(x: 39.2, y: 28.46))
75+
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))
76+
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))
77+
path3_fillPath.addLine(to: CGPoint(x: 27.05, y: 45.83))
78+
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))
79+
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))
80+
path3_fillPath.addLine(to: CGPoint(x: 37.07, y: 44.03))
81+
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))
82+
path3_fillPath.addLine(to: CGPoint(x: 46.26, y: 47.34))
83+
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))
84+
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))
85+
path3_fillPath.addLine(to: CGPoint(x: 39.2, y: 28.46))
86+
path3_fillPath.close()
87+
path3_fillPath.usesEvenOddFillRule = true
88+
puckColor.setFill()
89+
path3_fillPath.fill()
90+
91+
context.restoreGState()
92+
}
93+
94+
@objc(UserPuckStyleKitResizingBehavior)
95+
enum ResizingBehavior: Int {
96+
case aspectFit /// The content is proportionally resized to fit into the target rectangle.
97+
case aspectFill /// The content is proportionally resized to completely fill the target rectangle.
98+
case stretch /// The content is stretched to match the entire target rectangle.
99+
case center /// The content is centered in the target rectangle, but it is NOT resized.
100+
101+
public func apply(rect: CGRect, target: CGRect) -> CGRect {
102+
if rect == target || target == CGRect.zero {
103+
return rect
104+
}
105+
106+
var scales = CGSize.zero
107+
scales.width = abs(target.width / rect.width)
108+
scales.height = abs(target.height / rect.height)
109+
110+
switch self {
111+
case .aspectFit:
112+
scales.width = min(scales.width, scales.height)
113+
scales.height = scales.width
114+
case .aspectFill:
115+
scales.width = max(scales.width, scales.height)
116+
scales.height = scales.width
117+
case .stretch:
118+
break
119+
case .center:
120+
scales.width = 1
121+
scales.height = 1
122+
}
123+
124+
var result = rect.standardized
125+
result.size.width *= scales.width
126+
result.size.height *= scales.height
127+
result.origin.x = target.minX + (target.width - result.width) / 2
128+
result.origin.y = target.minY + (target.height - result.height) / 2
129+
return result
130+
}
131+
}
132+
}

0 commit comments

Comments
 (0)