我正在尝试使用新iPhones中的气压计传感器来显示设备上的垂直速度。我在我的“运动”模型中使用了下面的代码,它的执行或多或少会给出合适的值。
然而,在执行startRelativeAltitudeUpdatesToQueue时,我遇到了更新我的视图的问题。userRelativeAltitude的值被更新,但函数永远不会返回任何内容(可能是因为它执行的startRelativeAltitudeUpdatesTo Queue就像一个循环。?)。我担心整个陈述已经超出了我的理解。
我的初衷是拥有更像startAccelerometerUpdates的功能。有没有更好的方法从气压计上收集数据?
另外,如何检查设备是否有可用的气压计传感器?
- (float) pullRelativeAltitude {
self._altitudeManager = [[CMAltimeter alloc] init];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[self._altitudeManager startRelativeAltitudeUpdatesToQueue:queue withHandler:^(CMAltitudeData *altitudeData, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^ {
_userRelativeAltitude = altitudeData.relativeAltitude.floatValue;
NSLog(@"%f", altitudeData.relativeAltitude.floatValue);
});
}];
NSLog(@"This is relative altitude: %f", _userRelativeAltitude is );
return _userRelativeAltitude;}
我们将非常感谢您的建议!
发布于 2014-09-21 03:54:29
在Swift中,以下代码将检查高度是否可用:
if CMAltimeter.isRelativeAltitudeAvailable() {
// your code
}不过,我也拿不到气压计的读数。
发布于 2014-09-21 11:53:03
不知道你有没有弄明白,但这对我很管用。
有一件事要补充,你可能已经弄明白了,你不能在模拟器中。您需要iPhone 6或6+,并且该应用程序必须在手机上运行:)
- (void)viewDidLoad {
[super viewDidLoad];
BOOL isReady = [CMAltimeter isRelativeAltitudeAvailable];
if( isReady ) {
NSLog(@"yes");
} else {
NSLog(@"no");
}
alt = [[CMAltimeter alloc] init];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[alt startRelativeAltitudeUpdatesToQueue:queue withHandler:^(CMAltitudeData *altitudeData, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^ {
NSLog(@"%f", altitudeData.relativeAltitude.floatValue);
});
}];
}发布于 2014-09-25 14:43:12
我让它工作了,但我必须在视图控制器(而不是模型)中实现代码,以使用新的读取更新UI。对于其他感兴趣的人,我的最终代码最终如下所示:
if ([CMAltimeter isRelativeAltitudeAvailable]) {
self._altitudeManager = [[CMAltimeter alloc] init];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[self._altitudeManager startRelativeAltitudeUpdatesToQueue:queue withHandler:^(CMAltitudeData *altitudeData, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^ {
//provide relative altitude
float relativeAltitude;
relativeAltitude = altitudeData.relativeAltitude.floatValue;
self.userRelativeAltitude.text = [NSString stringWithFormat:@"%.0f m", relativeAltitude];
});
}];
} else {
NSLog(@"barometer not available");
_userVario.text = @"no barometer available";
}谢谢你的回答!
https://stackoverflow.com/questions/25951443
复制相似问题