[IOS]ジャイロとアクセラレータのプログラミング


iPhoneの重力誘導と回転誘導、すなわち用語上のジャイロとアクセルをどのように実現しますか?簡単なデモを使ってみましょう!
シミュレータではこれを実行できないので、コードを貼るしかなく、スクリーンショットはありません.
ViewController.h:
#import  @interface BIDViewController : UIViewController @property (weak, nonatomic) IBOutlet UILabel *accelerometerLabel; @property (weak, nonatomic) IBOutlet UILabel *gyroscopeLabel; @end

ViewController.m:
#import "BIDViewController.h" #import   @interface BIDViewController () @property (strong, nonatomic) CMMotionManager *motionManager; @property (strong, nonatomic) NSOperationQueue *queue; @end  @implementation BIDViewController  - (NSUInteger)supportedInterfaceOrientations {     return UIInterfaceOrientationMaskPortrait; }  - (void)viewDidLoad {     [super viewDidLoad]; 	// Do any additional setup after loading the view, typically from a nib.          self.motionManager = [[CMMotionManager alloc] init];     self.queue = [[NSOperationQueue alloc] init];          if (self.motionManager.accelerometerAvailable) {                  self.motionManager.accelerometerUpdateInterval = 1.0 / 10.0;                  [self.motionManager startAccelerometerUpdatesToQueue:self.queue                                                  withHandler:          ^(CMAccelerometerData *accelerometerData, NSError *error) {                           NSString *labelText;             labelText = [NSString stringWithFormat:                         @"Accelerometer
-----------
x: %+.2f
y: %+.2f
z: %+.2f", accelerometerData.acceleration.x, accelerometerData.acceleration.y, accelerometerData.acceleration.z]; NSLog(@"%@",labelText); }]; } else { self.accelerometerLabel.text = @"This device has no accelerometer."; } if (self.motionManager.gyroAvailable) { self.motionManager.gyroUpdateInterval = 1.0 / 10.0; [self.motionManager startGyroUpdatesToQueue:self.queue withHandler: ^(CMGyroData *gyroData, NSError *error) { NSString *labelText; labelText = [NSString stringWithFormat: @"Gyroscope
--------
x: %+.2f
y: %+.2f
z: %+.2f", gyroData.rotationRate.x, gyroData.rotationRate.y, gyroData.rotationRate.z]; NSLog(@"%@",labelText); }]; } else { self.gyroscopeLabel.text = @"This device has no gyroscope"; } } @end

CoreMotion.frameworkフレームワークをインポートするには