ios设备中有的加速计可以测量出加速度和重力。陀螺仪可用于确定设备的方向与每条坐标轴之间的夹角,可用于读取描述设备围绕其轴的旋转的值。
添加CoreMotion.framework.
以下为例子代码:
添加CoreMotion.framework.
以下为例子代码:
#import <UIKit/UIKit.h>
#import <CoreMotion/CoreMotion.h>
@interface ViewController : UIViewController
@property (strong, nonatomic) CMMotionManager *motionManager;
@property (weak, nonatomic) IBOutlet UILabel *accelerometerLabel;
@property (weak, nonatomic) IBOutlet UILabel *gyroscopeLabel;
@end
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize motionManager;
@synthesize accelerometerLabel;
@synthesize gyroscopeLabel;
- (void)viewDidLoad
{
[super viewDidLoad];
self.motionManager = [[CMMotionManager alloc] init];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
//加速计
if (motionManager.accelerometerAvailable) {
motionManager.accelerometerUpdateInterval = 1.0/10.0;
[motionManager startAccelerometerUpdatesToQueue:queue withHandler:^(CMAccelerometerData *accelerometerData,NSError *error){
NSString *labelText;
if (error) {
[motionManager stopAccelerometerUpdates];
labelText = [NSString stringWithFormat:@"Accelerometer encountered error: %@",error];
}else{
labelText = [NSString stringWithFormat:@"加速计\nx: %+.2f\ny: %+.2f\nz: %+.2f",accelerometerData.acceleration.x,accelerometerData.acceleration.y,accelerometerData.acceleration.z];
}
[accelerometerLabel performSelectorOnMainThread:@selector(setText:) withObject:labelText waitUntilDone:NO];
}];
}else{
accelerometerLabel.text = @"This device has no accelerometer.";
}
//陀螺仪
if (motionManager.gyroAvailable) {
motionManager.gyroUpdateInterval = 1.0/10.0;
[motionManager startGyroUpdatesToQueue:queue withHandler:^(CMGyroData *gyroData,NSError *error){
NSString *labelText;
if (error) {
[motionManager stopGyroUpdates];
labelText = [NSString stringWithFormat:@"Gyroscope encountered error: %@",error];
}else{
labelText = [NSString stringWithFormat:@"陀螺仪\nx: %+.2f\ny: %+.2f\nz: %+.2f",gyroData.rotationRate.x,gyroData.rotationRate.y,gyroData.rotationRate.z];
}
[gyroscopeLabel performSelectorOnMainThread:@selector(setText:) withObject:labelText waitUntilDone:NO];
}];
}else{
gyroscopeLabel.text = @"This device has no gyroscope";
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end