-
Notifications
You must be signed in to change notification settings - Fork 318
/
Copy pathTurnArrowView.swift
117 lines (105 loc) · 4.1 KB
/
TurnArrowView.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
import UIKit
import MapboxDirections
import MapboxCoreNavigation
import SDWebImage
@IBDesignable
@objc(MBTurnArrowView)
public class TurnArrowView: UIView {
public dynamic var primaryColor: UIColor = .defaultTurnArrowPrimary {
didSet {
setNeedsDisplay()
}
}
public dynamic var secondaryColor: UIColor = .defaultTurnArrowSecondary {
didSet {
setNeedsDisplay()
}
}
public var step: RouteStep? {
didSet {
setNeedsDisplay()
}
}
public var isStart = false {
didSet {
setNeedsDisplay()
}
}
public var isEnd = false {
didSet {
setNeedsDisplay()
}
}
@IBInspectable
var scale: CGFloat = 1 {
didSet {
setNeedsDisplay()
}
}
override public func draw(_ rect: CGRect) {
super.draw(rect)
transform = CGAffineTransform.identity
guard let step = step else {
if isStart {
StyleKitArrows.drawStarting(primaryColor: primaryColor, scale: scale)
} else if isEnd {
StyleKitArrows.drawDestination(primaryColor: primaryColor, scale: scale)
}
return
}
var flip: Bool = false
let type: ManeuverType = step.maneuverType ?? .turn
let angle: Int = Int(wrap((step.finalHeading ?? abs(0)) - (step.initialHeading ?? abs(0)), min: -180, max: 180))
let direction: ManeuverDirection = step.maneuverDirection ?? ManeuverDirection(angle: angle)
switch type {
case .merge:
StyleKitArrows.drawMerge(primaryColor: primaryColor, secondaryColor: secondaryColor, scale: scale)
flip = [.right, .slightRight, .sharpRight].contains(direction)
case .takeOffRamp:
StyleKitArrows.drawOfframp(primaryColor: primaryColor, secondaryColor: secondaryColor, scale: scale)
flip = [.right, .slightRight, .sharpRight].contains(direction)
case .reachFork:
StyleKitArrows.drawFork(primaryColor: primaryColor, secondaryColor: secondaryColor, scale: scale)
flip = [.right, .slightRight, .sharpRight].contains(direction)
case .takeRoundabout, .turnAtRoundabout:
StyleKitArrows.drawRoundabout(primaryColor: primaryColor, secondaryColor: secondaryColor, scale: scale)
case .arrive:
switch direction {
case .right:
StyleKitArrows.drawArriveright(primaryColor: primaryColor, secondaryColor: secondaryColor, scale: scale)
case .left:
StyleKitArrows.drawArriveright(primaryColor: primaryColor, secondaryColor: secondaryColor, scale: scale)
flip = true
default:
StyleKitArrows.drawArrive(primaryColor: primaryColor, scale: scale)
}
default:
switch direction {
case .right:
StyleKitArrows.drawArrow45(primaryColor: primaryColor, scale: scale)
flip = false
case .slightRight:
StyleKitArrows.drawArrow30(primaryColor: primaryColor, scale: scale)
flip = false
case .sharpRight:
StyleKitArrows.drawArrow75(primaryColor: primaryColor, scale: scale)
flip = false
case .left:
StyleKitArrows.drawArrow45(primaryColor: primaryColor, scale: scale)
flip = true
case .slightLeft:
StyleKitArrows.drawArrow30(primaryColor: primaryColor, scale: scale)
flip = true
case .sharpLeft:
StyleKitArrows.drawArrow75(primaryColor: primaryColor, scale: scale)
flip = true
case .uTurn:
StyleKitArrows.drawArrow180(primaryColor: primaryColor, scale: scale)
flip = angle < 0
default:
StyleKitArrows.drawArrow0(primaryColor: primaryColor, scale: scale)
}
}
transform = CGAffineTransform(scaleX: flip ? -1 : 1, y: 1)
}
}