/
ViewController.swift
227 lines (188 loc) · 6.95 KB
/
ViewController.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
//
// ViewController.swift
// SpotlightsDemo
//
// Created by TriNgo on 5/14/20.
// Copyright © 2020 RoverDream. All rights reserved.
//
import UIKit
import Spotlights
class ViewController: UIViewController {
lazy var contentView: UIView = {
let view = UIView(frame: UIScreen.main.bounds)
view.backgroundColor = UIColor(red: 222/255, green: 128/255, blue: 255/255, alpha: 1.0)
return view
}()
lazy var numberOneLabel: UILabel = {
let label = UILabel(frame: CGRect(x: 100, y: 100, width: 60, height: 40))
label.textAlignment = .center
label.text = "One"
return label
}()
lazy var numberTwoLabel: UILabel = {
let label = UILabel(frame: CGRect(x: 350, y: 300, width: 60, height: 40))
label.textAlignment = .center
label.text = "Two"
return label
}()
lazy var numberThreeLabel: UILabel = {
let label = UILabel(frame: CGRect(x: 20, y: 500, width: 60, height: 40))
label.textAlignment = .center
label.text = "Three"
return label
}()
lazy var startSpotlightButton: UIButton = {
let button = UIButton(frame: CGRect(x: 20, y: UIScreen.main.bounds.height - 100,
width: UIScreen.main.bounds.width - 40, height: 40))
button.backgroundColor = .white
button.setTitle("Start Spotlight", for: .normal)
button.setTitleColor(UIColor.blue, for: .normal)
button.setTitleColor(UIColor.darkGray, for: .highlighted)
button.addTarget(self, action: #selector(onClickStartSpotlightButton), for: .touchUpInside)
return button
}()
lazy var stopSpotlightButton: UIButton = {
let button = UIButton(frame: CGRect(x: 20, y: UIScreen.main.bounds.height - 160,
width: UIScreen.main.bounds.width - 40, height: 40))
button.backgroundColor = .white
button.setTitle("Stop Spotlight", for: .normal)
button.setTitleColor(UIColor.blue, for: .normal)
button.setTitleColor(UIColor.darkGray, for: .highlighted)
button.addTarget(self, action: #selector(onClickStopSpotlightButton), for: .touchUpInside)
return button
}()
lazy var nextButton: UIButton = {
let button = UIButton(frame: CGRect(x: UIScreen.main.bounds.width/2, y: UIScreen.main.bounds.height - 100,
width: UIScreen.main.bounds.width/2 - 20, height: 40))
button.backgroundColor = .white
button.setTitle("Next", for: .normal)
button.setTitleColor(UIColor.blue, for: .normal)
button.setTitleColor(UIColor.darkGray, for: .highlighted)
button.addTarget(self, action: #selector(onClickNextButton), for: .touchUpInside)
return button
}()
lazy var prevButton: UIButton = {
let button = UIButton(frame: CGRect(x: 20, y: UIScreen.main.bounds.height - 100,
width: UIScreen.main.bounds.width/2 - 40, height: 40))
button.backgroundColor = .white
button.setTitle("Prev", for: .normal)
button.setTitleColor(UIColor.blue, for: .normal)
button.setTitleColor(UIColor.darkGray, for: .highlighted)
button.addTarget(self, action: #selector(onClickPrevButton), for: .touchUpInside)
return button
}()
lazy var firstOverlay: FirstOverlay = {
let view = FirstOverlay(frame: UIScreen.main.bounds)
view.delegate = self
return view
}()
var spotlight: Spotlight?
init() {
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
super.init(nibName: nil, bundle: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
contentView.addSubview(startSpotlightButton)
[numberOneLabel, numberTwoLabel, numberThreeLabel]
.forEach { contentView.addSubview($0) }
self.view.addSubview(contentView)
}
@objc func onClickStartSpotlightButton() {
// Create sample overlay view
let overlay = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
overlay.addSubview(prevButton)
overlay.addSubview(nextButton)
overlay.addSubview(stopSpotlightButton)
// Create first target as Rectangle shape & apply on numberOneLabel
// Use FirstOverlay (customize view by xib) as overlay view
let firstTarget = TargetBuilder()
.setAnchor(view: numberOneLabel)
.setShape(shape: Rectangle(width: 100, height: 40, cornerRadius: 8.0))
.setOverlay(overlay: firstOverlay)
.setDelegate(delegate: self)
.build()
// Create second target as Circle shape & apply on numberTwoLabel
let secondTarget = TargetBuilder()
.setAnchor(view: numberTwoLabel)
.setShape(shape: Circle(radius: 40))
.setOverlay(overlay: overlay)
.setDelegate(delegate: self)
.build()
// Create third target as Circle shape & apply on numberThreeLabel
let thirdTarget = TargetBuilder()
.setAnchor(view: numberThreeLabel)
.setShape(shape: Circle(radius: 80))
.setOverlay(overlay: overlay)
.setDelegate(delegate: self)
.build()
// Create spotlight with an array of targets
// Set container as current UIViewController
spotlight = SpotlightBuilder()
.setTargets(targets: [firstTarget, secondTarget, thirdTarget])
.setContainer(container: self)
.setSpotlightDelegate(delegate: self)
.build()
/*
* TODO: Create a bunch of similar property targets & apply on all views
let targets = [numberOneLabel, numberTwoLabel, numberThreeLabel].map {
return TargetBuilder()
.setAnchor(view: $0)
.setShape(shape: Circle(radius: 80))
.setOverlay(overlay: overlay)
.setDelegate(delegate: self)
.build()
}
spotlight = SpotlightBuilder()
.setTargets(targets: targets)
.setContainer(container: self)
.setSpotlightDelegate(delegate: self)
.build()
*/
// After all, let start spotlight
spotlight?.start()
}
@objc func onClickStopSpotlightButton() {
// Stop spotlight
spotlight?.finish()
}
@objc func onClickNextButton() {
// Go to next target
spotlight?.next()
}
@objc func onClickPrevButton() {
// Go to previous
spotlight?.previous()
}
}
// MARK: TargetDelegate methods
extension ViewController: TargetDelegate {
func didTargetStart(target: Target, index: Int) {
print("TargetDelegate didTargetStart, index: \(index)")
}
func didTargetEnd(target: Target, index: Int) {
print("TargetDelegate didTargetEnd, index: \(index)")
}
}
// MARK: SpotlightDelegate methods
extension ViewController: SpotlightDelegate {
func didStart(spotlight: Spotlight) {
print("SpotlightDelegate didStart")
}
func didEnd(spotlight: Spotlight) {
print("SpotlightDelegate didEnd")
}
}
// MARK: FirstOverlayDelegate methods
extension ViewController: FirstOverlayDelegate {
func didClickStopSpotlightButton(overlay: FirstOverlay) {
spotlight?.finish()
}
func didClickNextButton(overlay: FirstOverlay) {
spotlight?.next()
}
func didClickPrevButton(overlay: FirstOverlay) {
}
}