当前位置: 移动技术网 > 移动技术>移动开发>IOS > IOS 陀螺仪开发(CoreMotion框架)实例详解

IOS 陀螺仪开发(CoreMotion框架)实例详解

2019年07月24日  | 移动技术网移动技术  | 我要评论

ios陀螺仪 参数意义

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三个方向,参考下图:

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

-(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];
}

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

- (bool)canbecomefirstresponder {
  return yes;
}
 
- (void)viewdidappear:(bool)animated {
  [self becomefirstresponder];
}

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

- (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方法中处理:

- (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处理方式:

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

触摸结束:

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

push处理方式:

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

时间设置频率:

 

陀螺仪

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

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

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];
   }
}

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网