IOS中陀螺仪的使用方法

#import <CoreMotion/CoreMotion.h>
@property (strong) CMMotionManager              *motionManager;

- (void) viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    _motionManager = [[CMMotionManager alloc]init];
    _motionManager.gyroUpdateInterval = 0.1;
    if(_motionManager.gyroAvailable) {
        [_motionManager startGyroUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMGyroData *gyroData, NSError *error) {
            printf("%lf, %lf, %lf\n", gyroData.rotationRate.x, gyroData.rotationRate.y, gyroData.rotationRate.z);
            _glView.rotx -= gyroData.rotationRate.x/8;
            _glView.roty -= gyroData.rotationRate.y/8;
        }];
    }
}

- (void) viewWillDisappear:(BOOL)animated
{
    if(_motionManager.gyroActive)
        [_motionManager stopGyroUpdates];
    [super viewWillDisappear:animated];
}

Build Phases --> Link Binary With Libraries --> [+] --> CoreMotion.framework

 

上一篇:C语言实现简单计算器小项目


下一篇:P1024 一元三次方程求解(分治算法)