iOS 中的多个传感器



我有两个不同的POC,一个用于加速度计,一个用于GPS。但是,我不理解将两个应用程序结合起来的体系结构。我需要在应用程序加载时初始化加速和 GPS。我将主视图与加速度相关联,但也需要与设备的位置相关联。

我目前的架构是工作区中的项目

  • 主应用
  • 实用工具 应用程序
  • 服务应用
  • 域应用

主应用程序视图控制器继承

: UIView控制器

所有连接正确,加速度按预期工作。

在 Utility CoreLocationUtility 类中,我让它继承了 CLLocationManagerDelegate。

问题是,如何从 AccelDelegate 类型的同一视图中注册委托?

如果要使视图控制器同时充当加速度计和 GPS 的委托,请在其头文件中声明它遵守这两个委托协议:

@interface ViewController : UIViewController <CLLocationManagerDelegate, UIAccelerometerDelegate> {
    CLLocationManager *myLocationManager; // an instance variable
}
 // ... your definitions
@end

然后在您的视图控制器中的某个位置

[UIAccelerometer sharedAccelerometer].delegate = self;
myLocationManager = [[CLLocationManager alloc] init];
myLocationManager.delegate = self;

然后在视图控制器中的其他地方,将两个协议的委托方法

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
    // ... your code
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
   // ... your code

}

这应该有效,虽然可能有错别字,但我还没有编译这个。另外,根据我的经验,位置管理器代码往往会变得非常大。最好将其放在自己的类中,由 ViewController 实例化。

谁能解释为什么 UIAccelerometerDelegate 协议中的唯一方法被弃用?

最新更新