星空网 > 软件开发 > 操作系统

加速计和陀螺仪

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

创建运动管理器使用框架为: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];

 



原标题:加速计和陀螺仪

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流