当前位置:首页 > 通信资讯 > 正文

ios陀螺仪 参数意义

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 self.mmanager = [[cmmotionmanager alloc]init]; self.mmanager.devicemotionupdateinterval = 0.5; if (self.mmanager.gyroavailable) { [self.mmanager startdevicemotionupdatestoqueue:[nsoperationqueue currentqueue] withhandler:^(cmdevicemotion * _nullable motion, nserror * _nullable error) { nslog(@"rotationrate x:%.2lf y:%.2lf z:%.2lf ",motion.useracceleration.x,motion.useracceleration.y,motion.useracceleration.z); }]; }

x轴 头 靠近 负数 y参数
x轴 头 远离 正数

y轴 左侧 高 正数 x参数
y轴 右侧 高 负数

下面介绍下coremotion框架

coremotion是一个专门处理motion的框架,其中包含了两个部分加速度计和陀螺仪,在ios4之前加速度计是由uiaccelerometer类来负责采集数据,现在一般都是用coremotion来处理加速度过程,不过由于uiaccelerometer比较简单,同样有人在使用。加速计由三个坐标轴决定,用户最常见的操作设备的动作移动,晃动手机(摇一摇),倾斜手机都可以被设备检测到,加速计可以检测到线性的变化,陀螺仪可以更好的检测到偏转的动作,可以根据用户的动作做出相应的动作,ios模拟器无法模拟以上动作,真机调试需要开发者账号。

加速计

加速计的x,y,z三个方向,参考下图:

ios 陀螺仪开发(coremotion框架)实例(ios 陀螺仪开发(coremotion框架)实例)

如果只需要知道设备的方向,不需要知道具体方向矢量角度,那么可以使用uidevice进行操作,还可以根据方向就行判断,具体可以参考一下苹果官网代码:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 -(void) viewdidload { // request to turn on accelerometer and begin receiving accelerometer events [[uidevice currentdevice] begingeneratingdeviceorientationnotifications]; [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(orientationchanged:) name:uideviceorientationdidchangenotification object:nil]; } - (void)orientationchanged:(nsnotification *)notification { // respond to changes in device orientation } -(void) viewdiddisappear { // request to stop receiving accelerometer events and turn off accelerometer [[nsnotificationcenter defaultcenter] removeobserver:self]; [[uidevice currentdevice] endgeneratingdeviceorientationnotifications]; }

当用户晃动设备的时候,系统会通知每一个在用的设备,可以使本身成为第一响应者:

?
1 2 3 4 5 6 7 - (bool)canbecomefirstresponder { return yes; } - (void)viewdidappear:(bool)animated { [self becomefirstresponder]; }

处理motion事件有三种方式,开始(motionbegan),结束(motionended),取消(motioncancelled):

?
1 2 3 - (void)motionbegan:(uieventsubtype)motion withevent:(uievent *)event ns_available_ios(3_0); - (void)motionended:(uieventsubtype)motion withevent:(uievent *)event ns_available_ios(3_0); - (void)motioncancelled:(uieventsubtype)motion withevent:(uievent *)event ns_available_ios(3_0);

motionended方法中处理:

?
1 2 3 4 5 6 7 - (void)motionended:(uieventsubtype)motion withevent:(uievent *)event { if (motion == uieventsubtypemotionshake) { // flyelephant http://www.cnblogs.com/xiaofeixiang [[nsnotificationcenter defaultcenter] postnotificationname:@"flyelephant" object:self]; } }

coremotion在处理加速计数据和陀螺仪数据的时是一个非常重要的框架,框架本身集成了很多算法获取原生的数据,而且能很好的展现出来,coremotion与uikit不同,连接的是uievent而不是事件响应链。coremotion相对于接收数据只是更简单的分发motion事件。

cmmotionmanager类能够使用到设备的所有移动数据(motion data),core motion框架提供了两种对motion数据的操作方式:

pull方式:能够以coremotionmanager的只读方式获取当前任何传感器状态或是组合数据;

push方式:是以块或者闭包的形式收集到想要得到的数据并且在特定周期内得到实时的更新;

pull处理方式:

?
1 2 3 4 5 6 7 8 //判断加速计是否可用 if ([_motionmanager isaccelerometeravailable]) { // 设置加速计采样频率 [_motionmanager setaccelerometerupdateinterval:1 / 40.0]; [_motionmanager startaccelerometerupdates]; } else { nslog(@"博客园-flyelephant"); }

触摸结束:

?
1 2 3 4 -(void)touchesended:(nsset *)touches withevent:(uievent *)event{ cmacceleration acceleration=_motionmanager.accelerometerdata.acceleration; nslog(@"%f---%f---%f",acceleration.x,acceleration.y,acceleration.z); }

push处理方式:

?
1 2 3 4 @property (strong,nonatomic) cmmotionmanager *motionmanager; @property (strong,nonatomic) nsoperationqueue *quene; _motionmanager=[[cmmotionmanager alloc]init];<br>//判断加速计是否可用<br>if ([_motionmanager isaccelerometeravailable]) {<br> // 设置加速计频率<br> [_motionmanager setaccelerometerupdateinterval:1 / 40.0];<br> //开始采样数据<br> [_motionmanager startaccelerometerupdatestoqueue:_quene withhandler:^(cmaccelerometerdata *accelerometerdata, nserror *error) {<br> nslog(@"%f---%f",accelerometerdata.acceleration.x,accelerometerdata.acceleration.y);<br> }];<br>} else {<br> nslog(@"博客园-flyelephant");<br>}<br>

时间设置频率:

ios 陀螺仪开发(coremotion框架)实例(ios 陀螺仪开发(coremotion框架)实例)

陀螺仪

陀螺仪其实主要方法和方式和加速计没有区别,先看张陀螺仪旋转的角度图片:

ios 陀螺仪开发(coremotion框架)实例(ios 陀螺仪开发(coremotion框架)实例)

陀螺仪更新数据也有两种方式,pull方式(startgyroupdates),push方式(startgyroupdatestoqueue):

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 static const nstimeinterval gyromin = 0.01; - (void)startupdateswithslidervalue:(int)slidervalue { // determine the update interval nstimeinterval delta = 0.005; nstimeinterval updateinterval = gyromin + delta * slidervalue; // create a cmmotionmanager cmmotionmanager *mmanager = [(aplappdelegate *)[[uiapplication sharedapplication] delegate] sharedmanager]; aplgyrographviewcontroller * __weak weakself = self; // check whether the gyroscope is available if ([mmanager isgyroavailable] == yes) { // assign the update interval to the motion manager [mmanager setgyroupdateinterval:updateinterval]; [mmanager startgyroupdatestoqueue:[nsoperationqueue mainqueue] withhandler:^(cmgyrodata *gyrodata, nserror *error) { [weakself.graphview addx:gyrodata.rotationrate.x y:gyrodata.rotationrate.y z:gyrodata.rotationrate.z]; [weakself setlabelvaluex:gyrodata.rotationrate.x y:gyrodata.rotationrate.y z:gyrodata.rotationrate.z]; }]; } self.updateintervallabel.text = [nsstring stringwithformat:@"%f", updateinterval]; } - (void)stopupdates{ cmmotionmanager *mmanager = [(aplappdelegate *)[[uiapplication sharedapplication] delegate] sharedmanager]; if ([mmanager isgyroactive] == yes) { [mmanager stopgyroupdates]; } }

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。