您好,登錄后才能下訂單哦!
#import <UIKit/UIKit.h> @interface BIDViewController : UIViewController<UIAccelerometerDelegate> @property (weak, nonatomic) IBOutlet UILabel *accelerometerLabel; @property (weak, nonatomic) IBOutlet UILabel *gyroscopeLabel; @end
#import "BIDViewController.h" #import <CoreMotion/CoreMotion.h> @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\n-----------\nx: %+.2f\ny: %+.2f\nz: %+.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\n--------\nx: %+.2f\ny: %+.2f\nz: %+.2f", gyroData.rotationRate.x, gyroData.rotationRate.y, gyroData.rotationRate.z]; NSLog(@"%@",labelText); }]; } else { self.gyroscopeLabel.text = @"This device has no gyroscope"; } } @end
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。