使用我的6我一直在尝试使用CoreMotion的新CMAltimeter读取相对高度和压力.然而,回调永远不会解雇.我有一个非常相似的设置,而是使用加速度计,陀螺仪和磁力计.他们似乎都很好. 想知道是否
想知道是否有人设法获得阅读?
- (void)viewDidLoad { [super viewDidLoad]; if([CMAltimeter isRelativeAltitudeAvailable]){ CMAltimeter *altimeterManager = [[CMAltimeter alloc]init]; [altimeterManager startRelativeAltitudeUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAltitudeData *altitudeData, NSError *error) { // This never fires. NSString *data = [NSString stringWithFormat:@"Altitude: %f %f", altitudeData.relativeAltitude.floatValue, altitudeData.pressure.floatValue]; NSLog(@"%@", data); self.altimeterLabel.text = data; }]; NSLog(@"Started altimeter"); self.altimeterLabel.text = @"-\n-"; } else { NSLog(@"Altimeter not available"); } }
我已经尝试过快速步行,但是这里只有一个高度失去/获得的故事.
我非常尴尬地以如此巨大的疏忽来回答我自己的问题.在原始帖子中,我在viewDidLoad的范围内声明了CMAltimiter,因此它超出了范围并被取消分配.我把它变成了iVar,现在回调了.
#import "ViewController.h" @import CoreMotion; @interface ViewController () @property (nonatomic, strong) CMAltimeter *altimeterManager; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; if([CMAltimeter isRelativeAltitudeAvailable]){ self.altimeterManager = [[CMAltimeter alloc]init]; [self.altimeterManager startRelativeAltitudeUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAltitudeData *altitudeData, NSError *error) { // This now fires properly NSString *data = [NSString stringWithFormat:@"Altitude: %f %f", altitudeData.relativeAltitude.floatValue, altitudeData.pressure.floatValue]; NSLog(@"%@", data); self.altimeterLabel.text = data; }]; NSLog(@"Started altimeter"); self.altimeterLabel.text = @"-\n-"; } else { NSLog(@"Altimeter not available"); } }