-
Notifications
You must be signed in to change notification settings - Fork 134
/
TourViewController.swift
243 lines (194 loc) · 8.54 KB
/
TourViewController.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
import UIKit
protocol TourDelegate: AnyObject {
func endTourTapped()
func nextTapped()
}
class TourViewController: UIViewController, TourDelegate {
private let tourSteps: [TourStep]
var overrideStatusBarStyle = AppTheme.defaultStatusBarStyle()
// this is not a weak var on purpose, nothing retains an FeatureTour object so we will until it dismisses
var delegate: FeatureTour?
private let fillLayer = CAShapeLayer()
private let defaultTopBottomPadding: CGFloat = 120
private let dialogPadding: CGFloat = 50
private var topAnchor: NSLayoutConstraint!
private var bottomAnchor: NSLayoutConstraint!
private lazy var tourView: TourView = {
let tourView = TourView(stepCount: tourSteps.count)
tourView.delegate = self
tourView.translatesAutoresizingMaskIntoConstraints = false
return tourView
}()
private var upToStep = 0
init(tourSteps: [TourStep]) {
self.tourSteps = tourSteps
super.init(nibName: "TourViewController", bundle: nil)
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
}
private var haveSetupView = false
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if !haveSetupView {
haveSetupView = true
setup()
}
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if fillLayer.path?.boundingBox.maxY != view.bounds.maxY, upToStep == 0 {
fillLayer.path = UIBezierPath(rect: view.bounds).cgPath
}
}
private func setup() {
guard let firstStep = tourSteps.first else { return }
view.layoutIfNeeded()
view.backgroundColor = UIColor.clear
fillLayer.fillRule = .evenOdd
fillLayer.fillColor = UIColor.black.withAlphaComponent(0.5).cgColor
view.layer.addSublayer(fillLayer)
fillLayer.path = UIBezierPath(rect: view.bounds).cgPath
view.addSubview(tourView)
if view.bounds.width > 530 {
// for large widths, used a fixed size dialog
NSLayoutConstraint.activate([
tourView.leadingAnchor.constraint(greaterThanOrEqualTo: view.leadingAnchor, constant: 14),
view.trailingAnchor.constraint(greaterThanOrEqualTo: tourView.trailingAnchor, constant: 14),
tourView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
tourView.widthAnchor.constraint(equalToConstant: 500)
])
} else {
// for smaller more phone sized views use a fixed padding from the edge
NSLayoutConstraint.activate([
tourView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 14),
view.trailingAnchor.constraint(equalTo: tourView.trailingAnchor, constant: 14)
])
}
bottomAnchor = view.bottomAnchor.constraint(equalTo: tourView.bottomAnchor, constant: defaultTopBottomPadding)
topAnchor = tourView.topAnchor.constraint(equalTo: view.topAnchor, constant: defaultTopBottomPadding)
bottomAnchor.isActive = true
displayStep(step: firstStep)
}
override var preferredStatusBarStyle: UIStatusBarStyle {
overrideStatusBarStyle
}
// MARK: - TourDelegate
func endTourTapped() {
if upToStep < tourSteps.count - 1 {
// user cancelled early
delegate?.tourCancelled(at: upToStep)
} else {
// user got all the way through
delegate?.tourEnded()
}
}
func nextTapped() {
if upToStep == tourSteps.count - 1 {
// we're at the last step, the tour is over
delegate?.tourEnded()
} else {
proceedToNextStep()
}
}
private func proceedToNextStep() {
upToStep += 1
let step = tourSteps[upToStep]
displayStep(step: step)
}
private func displayStep(step: TourStep) {
view.layoutIfNeeded()
UIView.animate(withDuration: Constants.Animation.defaultAnimationTime) {
if let highlightView = step.featureHighlight, let superview = highlightView.superview {
var itemRect = superview.convert(highlightView.frame, to: self.view)
itemRect.origin.y += step.yTranslation
if step.anchorToTop {
self.topAnchor.constant = itemRect.maxY + self.dialogPadding
} else {
self.bottomAnchor.constant = self.view.frame.height - itemRect.minY + self.dialogPadding
}
} else if let spotlight = step.spotlight {
let itemCenter = spotlight.superview.convert(spotlight.center, to: self.view)
var itemRect = CGRect(origin: CGPoint(x: itemCenter.x - spotlight.radius, y: itemCenter.y - spotlight.radius), size: CGSize(width: spotlight.radius, height: spotlight.radius))
itemRect.origin.y += step.yTranslation
if step.anchorToTop {
self.topAnchor.constant = itemRect.maxY + self.dialogPadding
} else {
self.bottomAnchor.constant = self.view.frame.height - itemRect.minY + self.dialogPadding
}
} else {
self.topAnchor.constant = self.defaultTopBottomPadding
self.bottomAnchor.constant = self.defaultTopBottomPadding
}
self.topAnchor.isActive = step.anchorToTop
self.bottomAnchor.isActive = !step.anchorToTop
self.view.layoutIfNeeded()
}
tourView.position = upToStep
tourView.step = step
updateCutOut(step: step, animated: upToStep > 1)
}
private func updateCutOut(step: TourStep, animated: Bool) {
let path = UIBezierPath(rect: view.bounds)
if let highlightView = step.featureHighlight, let superview = highlightView.superview {
var itemRect = superview.convert(highlightView.frame, to: view)
itemRect.origin.y += step.yTranslation
if itemRect.width < 80 {
// for small items draw a circle
let cutoutRect = itemRect.insetBy(dx: -20, dy: -20)
let cutOutPath = UIBezierPath(roundedRect: cutoutRect, cornerRadius: 40)
path.append(cutOutPath)
} else {
// for bigger items draw a rounded rect
let cutoutRect = itemRect.insetBy(dx: -20, dy: 0)
let cutOutPath = UIBezierPath(roundedRect: cutoutRect, cornerRadius: 40)
path.append(cutOutPath)
}
path.usesEvenOddFillRule = true
} else if let spotlight = step.spotlight {
let spotlightCenter = spotlight.superview.convert(spotlight.center, to: view)
let cutOutPath = UIBezierPath(arcCenter: spotlightCenter, radius: spotlight.radius, startAngle: 0, endAngle: CGFloat(2 * Double.pi), clockwise: true)
path.append(cutOutPath)
}
if !animated || step.featureHighlight == nil || step.spotlight == nil {
fillLayer.path = path.cgPath
fillLayer.removeAllAnimations()
return
}
CATransaction.begin()
CATransaction.setCompletionBlock {
self.fillLayer.path = path.cgPath
}
let pathAnimation = CABasicAnimation(keyPath: "path")
pathAnimation.fromValue = fillLayer.path
pathAnimation.toValue = path.cgPath
pathAnimation.duration = Constants.Animation.defaultAnimationTime
pathAnimation.fillMode = .forwards
pathAnimation.isRemovedOnCompletion = false
pathAnimation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
fillLayer.add(pathAnimation, forKey: "path")
CATransaction.commit()
}
// MARK: - Animate In/Out
func animateIn() {
view.alpha = 0
view.layoutIfNeeded()
UIView.animate(withDuration: Constants.Animation.defaultAnimationTime, delay: 0, options: .curveEaseOut, animations: { [weak self] in
self?.view.alpha = 1
self?.view?.layoutIfNeeded()
}, completion: nil)
}
func animateOut() {
view?.layoutIfNeeded()
UIView.animate(withDuration: Constants.Animation.defaultAnimationTime, animations: { [weak self] in
self?.view.alpha = 0
self?.view?.layoutIfNeeded()
}) { [weak self] _ in
self?.delegate?.controllerDidAnimateOut()
}
}
}