导入框架 - import CoreMotion
let altimeter = CMAltimeter()
var currentAltitude:Float = 0.0
func relativeAltitude() {
if CMAltimeter.isRelativeAltitudeAvailable() {
print("高度计是可用的")
} else {
print("高度计不可用")
}
let mainQueue = NSOperationQueue.mainQueue()
altimeter.startRelativeAltitudeUpdatesToQueue(mainQueue) { (data, error) -> Void in
// data.relativeAltitude 从上次运行该闭包开始计算,单位为m
self.currentAltitude += Float(data!.relativeAltitude)
self.print(self.currentAltitude)
}
}