In Interfacecontroller.swift
I have a class variable var motionManager = CMMotionManager()
and var accTest = [Double]()
which I refer to in this piece of code:
let useOnlyAccelerometer = true
if useOnlyAccelerometer {
motionManager.accelerometerUpdateInterval = 0.1
if motionManager.accelerometerAvailable {
let handler:CMAccelerometerHandler = {(data: CMAccelerometerData?, error: NSError?) -> Void in
self.statusLabel.setText(String(format: "%.2f", data!.acceleration.x))
self.accTest.append(data!.acceleration.x)
}
motionManager.startAccelerometerUpdatesToQueue(NSOperationQueue.currentQueue()!, withHandler: handler)
}
} else {
motionManager.deviceMotionUpdateInterval = 0.1
if motionManager.deviceMotionAvailable {
let handler:CMDeviceMotionHandler = {(motion: CMDeviceMotion?, error: NSError?) -> Void in
self.statusLabel.setText(String(format: "%.2f", motion!.userAcceleration.x))
self.accTest.append(motion!.userAcceleration.x)
}
motionManager.startDeviceMotionUpdatesToQueue(NSOperationQueue.currentQueue()!, withHandler: handler)
}
}
Using directly the accelerometer works. Now, if I change useOnlyAccelerometer
to false
then it does not work. I do not understand the difference or what is going wrong. On the iPhone it does work, and according to the documentation watchOS2 should support CMDeviceMotion
. Any ideas?