问题
I am trying to record the data from the iPhone's accelerometer (my own iPhone 5s) and set a label on the screen to that data using String(format: "%.2f", data)
where data is the value for the specific axis I want to record. To do this I set up a CMMotionManager and began recording accelerometer data, and I have a timer that constantly updates the text in the label. However, I am getting an error from Xcode: "fatal error: unexpectedly found nil while unwrapping an Optional value". Here is the relevant code:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//if accelerometer can be used, start it
if (motionManager.accelerometerAvailable) {
motionManager.accelerometerUpdateInterval = 0.1
motionManager.startAccelerometerUpdates()
let timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: Selector("update"), userInfo: nil, repeats: true)
}
}
func update () {
if (motionManager.accelerometerActive) {
accelX.text = String(format: "%.2f", motionManager.accelerometerData.acceleration.x)
}
}
The error stops when I change the accelX.text assignment to a simple string, so I think the optional variable creating the error is something to do with the accelerometer. That's as far as I know, however, so if you have any suggestions, or if I'm doing it completely wrong and there's a better and easier way, I will definitely appreciate it if you help me out.
回答1:
NSHipster has a good article to talk about the core motion: http://nshipster.com/cmdevicemotion/
A better way to regularly update UI with motion data is to use the patter as shown in below:
if manager.accelerometerAvailable {
manager.accelerometerUpdateInterval = 0.1
manager.startAccelerometerUpdatesToQueue(NSOperationQueue.mainQueue()) {
[weak self] (data: CMAccelerometerData!, error: NSError!) in
accelX.text = String(format: "%.2f", data.acceleration.x)
}
}
回答2:
you can get value of x or y or z and print alert of value you want
motionManager.accelerometerUpdateInterval = 5.0
motionManager.startAccelerometerUpdates(to: OperationQueue.current!) { (data, error) in
if let myData = data{
print(myData)
if myData.acceleration.y < -0.2{
let alert = UIAlertController(title: "Message ", message: "new text", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
}
来源:https://stackoverflow.com/questions/27768689/how-do-i-retrieve-accelerometer-data-correctly-with-swift-in-ios