The MeasureListenerSurface protocol declares the callbacks required for the measure.
2.1 Declare that your ViewController extension implements the MeasureListenerSurface.
2.2 Add to your view controller 2 UILabels(Result and Error) and a UIButton, make the UIButton disabled and set its title to “start” and the error UILabel to hidden.
2.3 Connect the following UI fields:
@IBOutlet weak var errorLabel: UILabel
@IBOutlet weak var startStopButton: UIButton!
2.4 In the viewDidLoad method add the following code.
errorLabel.isHidden = true
startStopButton.isEnabled = false;
SurfaceSdk.sharedInstance().delegate = self
SurfaceSdk.sharedInstance().startSensors()
}
The startSensors method initialized and calibrates the MeasureListener and must be called before any attempt to measure.
2.5 Add to the viewWillDisappear the following code:
SurfaceSdk.sharedInstance().stopSensors()
}
2.6 Add the following method
self.startButton.setTitle(“Start”, for: .normal)
enableStartStopButton()
}
onMeasureCanBeStarted is called by the MeasureListenerSurface when the manager is ready to start a measurement. What we’ve done in the code is set the init label text to ready, so the user will know he can start a measure and we’ve also made the measure button visible.
2.7 Add the following methods
func onMeasureCanBeStopped(){
enableStartStopButton()
}
func onMeasureCantBeStarted(){
disableStartStopButton()
}
func onMeasureCantBeStopped(){
disableStartStopButton()
}
//This callback will be called when measure process just started
self.startStopButton.setTitle(“Stop”, for: .normal)
}
//This callback will be called when measure process stopped and the result is being calculated
func onMeasureStopped(){
self.startStopButton.setTitle(“Calculating”, for: .normal)
self.disableStartStopButton()
}
self.startStopButton.isEnabled = true
self.startStopButton.alpha = 1
}
func disableStartStopButton(){
self.startStopButton.isEnabled = false
self.startStopButton.alpha = 0.5
}
- onMeasureCanBeStopped is called every time MeasureListener detects that a measurement can be stopped.
- onMeasureCantBeStarted and onMeasureCantBeStopped are called if MeasureListener detects that the device is unable to start or stop (The device is not standing still).
- onMeasureStarted is called every time MeasureListener detects that the device just started the measuring process.
- onMeasureStopped is called every time MeasureListener detects that the device measure process stopped and the result is being calculated
- What we’ve done in the code above is to update the button enable status if can measure or not.
2.8 Form the interface builder connect startStopButton to an IBAction called startStopMeasureing and adding to the following code
to the method.
SizeItCalculationManager.sharedInstance().startStopMeasure();
}
Here is where we do the actual measure code.
if the measurement already started the startStopMeasureing will stop the measurement and inform the
onMeasureStopped otherwise startStopMeasureing will start the measuring and it will inform onMeasureStarted.
2.9 Add the following method
func onMeasureFinished(_ result: NSNumber!, error:NSNumber!){
DispatchQueue.main.async {
if error != nil && error.intValue != 0{
self.errorLabel.isHidden = false;
self.errorLabel.text = “Error : \(String(describing: error!))”;
}else{
self.errorLabel.isHidden = true;
}
self.startStopButton.setTitle(“Start”, for: .normal)
self.result.text = “Result: \(String(format: “%.2f”, result.floatValue))”;
}
self.enableStartStopButton()
}
}
NOTICE! It’s important to check if error and result are nil if you don’t check the application might crash
Here we update the result label according to the result of the measurement.
onMeasureFinished is called after we called the startStopMeasure method when the measure calculation is over.
2.10 Build and run you project
Comments
0 comments
Please sign in to leave a comment.