当前位置: 代码迷 >> Iphone >> 加紧计和陀螺仪
  详细解决方案

加紧计和陀螺仪

热度:129   发布时间:2016-04-25 05:32:17.0
加速计和陀螺仪

在程序中如果需要创建运动管理器的实例,应由一个实例向整个程序提供加速计和陀螺仪运动服务.因为设备中只有一个加速计和一个陀螺仪,使用单例更合乎逻辑.

创建运动管理器使用框架为:CoreMotion.framework

引入头文件#import <CoreMotion/CoreMotion.h>

//初始化运动管理器    CMMotionManager *motionManager=[[CMMotionManager alloc]init];    //判断设备是否支持加速计和陀螺仪        if (motionManager.accelerometerAvailable&&motionManager.gyroAvailable) {        //设置时间,让加速计每隔0.01秒就发送一次更新    motionManager.accelerometerUpdateInterval=.01;        //接受陀螺仪        motionManager.gyroUpdateInterval=.01;    //启动加速计更新,并制定每次加速计更新都执行程序块    [motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {        //代码块    }];        [motionManager startGyroUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMGyroData *gyroData, NSError *error) {            //代码块        }];    }    else    {        NSLog(@"设备不支持陀螺仪");    }

如果要停止接受加速计和陀螺仪的更新
[motionManager stopAccelerometerUpdates];
[motionManager stopGyroUpdates];

 
  相关解决方案