-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathViewController.swift
159 lines (138 loc) · 5.15 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
//
// ViewController.swift
// Measure
//
// Created by levantAJ on 8/9/17.
// Copyright © 2017 levantAJ. All rights reserved.
//
import UIKit
import SceneKit
import ARKit
final class ViewController: UIViewController {
@IBOutlet weak var sceneView: ARSCNView!
@IBOutlet weak var targetImageView: UIImageView!
@IBOutlet weak var loadingView: UIActivityIndicatorView!
@IBOutlet weak var messageLabel: UILabel!
@IBOutlet weak var meterImageView: UIImageView!
@IBOutlet weak var resetImageView: UIImageView!
@IBOutlet weak var resetButton: UIButton!
fileprivate lazy var session = ARSession()
fileprivate lazy var sessionConfiguration = ARWorldTrackingSessionConfiguration()
fileprivate lazy var isMeasuring = false;
fileprivate lazy var vectorZero = SCNVector3()
fileprivate lazy var startValue = SCNVector3()
fileprivate lazy var endValue = SCNVector3()
fileprivate lazy var lines: [Line] = []
fileprivate var currentLine: Line?
fileprivate lazy var unit: DistanceUnit = .centimeter
override func viewDidLoad() {
super.viewDidLoad()
setupScene()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
UIApplication.shared.isIdleTimerDisabled = true
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
session.pause()
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
resetValues()
isMeasuring = true
targetImageView.image = UIImage(named: "targetGreen")
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
isMeasuring = false
targetImageView.image = UIImage(named: "targetWhite")
if let line = currentLine {
lines.append(line)
currentLine = nil
resetButton.isHidden = false
resetImageView.isHidden = false
}
}
override var prefersStatusBarHidden: Bool {
return true
}
}
// MARK: - ARSCNViewDelegate
extension ViewController: ARSCNViewDelegate {
func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
DispatchQueue.main.async { [weak self] in
self?.detectObjects()
}
}
func session(_ session: ARSession, didFailWithError error: Error) {
messageLabel.text = "Error occurred"
}
func sessionWasInterrupted(_ session: ARSession) {
messageLabel.text = "Interrupted"
}
func sessionInterruptionEnded(_ session: ARSession) {
messageLabel.text = "Interruption ended"
}
}
// MARK: - Users Interactions
extension ViewController {
@IBAction func meterButtonTapped(button: UIButton) {
let alertVC = UIAlertController(title: "Settings", message: "Please select distance unit options", preferredStyle: .actionSheet)
alertVC.addAction(UIAlertAction(title: DistanceUnit.centimeter.title, style: .default) { [weak self] _ in
self?.unit = .centimeter
})
alertVC.addAction(UIAlertAction(title: DistanceUnit.inch.title, style: .default) { [weak self] _ in
self?.unit = .inch
})
alertVC.addAction(UIAlertAction(title: DistanceUnit.meter.title, style: .default) { [weak self] _ in
self?.unit = .meter
})
alertVC.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
present(alertVC, animated: true, completion: nil)
}
@IBAction func resetButtonTapped(button: UIButton) {
resetButton.isHidden = true
resetImageView.isHidden = true
for line in lines {
line.removeFromParentNode()
}
lines.removeAll()
}
}
// MARK: - Privates
extension ViewController {
fileprivate func setupScene() {
targetImageView.isHidden = true
sceneView.delegate = self
sceneView.session = session
loadingView.startAnimating()
meterImageView.isHidden = true
messageLabel.text = "Detecting the world…"
resetButton.isHidden = true
resetImageView.isHidden = true
session.run(sessionConfiguration, options: [.resetTracking, .removeExistingAnchors])
resetValues()
}
fileprivate func resetValues() {
isMeasuring = false
startValue = SCNVector3()
endValue = SCNVector3()
}
fileprivate func detectObjects() {
guard let worldPosition = sceneView.realWorldVector(screenPosition: view.center) else { return }
targetImageView.isHidden = false
meterImageView.isHidden = false
if lines.isEmpty {
messageLabel.text = "Hold screen & move your phone…"
}
loadingView.stopAnimating()
if isMeasuring {
if startValue == vectorZero {
startValue = worldPosition
currentLine = Line(sceneView: sceneView, startVector: startValue, unit: unit)
}
endValue = worldPosition
currentLine?.update(to: endValue)
messageLabel.text = currentLine?.distance(to: endValue) ?? "Calculating…"
}
}
}