北京iOS开发教程-CoreMotion框架

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

北京千锋互联科技有限公司版权所有北京iOS开发教程-CoreMotion框架CoreMotion是一个专门处理Motion的框架,其中包含了两个部分加速计和陀螺仪,在iOS4之前加速计是由UIAccelerometer类来负责采集数据,现在一般都是用CoreMotion来处理加速度过程,不过由于UIAccelerometer比较简单,同样有人在使用。加速计由三个坐标轴决定,用户最常见的操作设备的动作移动,晃动手机(摇一摇),倾斜手机都可以被设备检测到,加速计可以检测到线性的变化,陀螺仪可以更好的检测到偏转的动作,可以根据用户的动作做出相应的动作,iOS模拟器无法模拟以上动作,真机调试需要开发者账号。加速计通过感知特定方向的惯性力总量,加速计可以测量出加速度和重力。iOS设备内的加速计是一个三轴加速计,也就是说它能够检测到三维空间中的运动或重量。因此,加速计不但可以指示用户握持设备的方式(如自动旋转功能),而且还可以在设备被放在桌子上时指示其正面朝上还是朝下。如图所示展示了加速计所使用的是三轴结构,需要注意的是,加速计对y坐标使用了更标准的惯例,即y轴伸长表示向上的力。如果加速计将Quartz2D作为控制机制,那么必须要转换y坐标轴。使用OpenGLES时(使用加速计控制动画时通常会用到),则不需要转换。如果只需要知道设备的方向,不需要知道具体方向矢量角度,那么可以使用UIDevice进行操作,还可以根据方向就行判断,具体可以参考一下苹果官网代码:-(void)viewDidLoad{//Requesttoturnonaccelerometerandbeginreceivingaccelerometerevents[[UIDevicecurrentDevice]beginGeneratingDeviceOrientationNotifications];[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(orientationChanged:)name:UIDeviceOrientationDidChangeNotificationobject:nil];}-(void)orientationChanged:(NSNotification*)notification{//Respondtochangesindeviceorientation}北京千锋互联科技有限公司版权所有-(void)viewDidDisappear{//Requesttostopreceivingaccelerometereventsandturnoffaccelerometer[[NSNotificationCenterdefaultCenter]removeObserver:self];[[UIDevicecurrentDevice]endGeneratingDeviceOrientationNotifications];}当用户晃动设备的时候,系统会通知每一个在用的设备,可以使本身成为第一响应者:-(BOOL)canBecomeFirstResponder{returnYES;}-(void)viewDidAppear:(BOOL)animated{[selfbecomeFirstResponder];}处理Motion事件有三种方式,开始(motionBegan),结束(motionEnded),取消(motionCancelled):-(void)motionBegan:(UIEventSubtype)motionwithEvent:(UIEvent*)eventNS_AVAILABLE_IOS(3_0);-(void)motionEnded:(UIEventSubtype)motionwithEvent:(UIEvent*)eventNS_AVAILABLE_IOS(3_0);-(void)motionCancelled:(UIEventSubtype)motionwithEvent:(UIEvent*)eventNS_AVAILABLE_IOS(3_0);motionEnded方法中处理:-(void)motionEnded:(UIEventSubtype)motionwithEvent:(UIEvent*)event{if(motion==UIEventSubtypeMotionShake){//FlyElephant[[NSNotificationCenterdefaultCenter]postNotificationName:@FlyElephantobject:self];}}CoreMotion在处理加速计数据和陀螺仪数据的时是一个非常重要的框架,框架本身集成了很多算法获取原生的数据,而且能很好的展现出来,CoreMotion与UIKit不同,连接的是UIEvent而不是事件响应链。CoreMotion相对于接收数据只是更简单的分发motion事件。CMMotionManager类能够使用到设备的所有移动数据(motiondata),CoreMotion框架提供了两种对motion数据的操作方式:pull方式:能够以CoreMotionManager的只读方式获取当前任何传感器状态或是组合数据;push方式:是以块或者闭包的形式收集到想要得到的数据并且在特定周期内得到实时的更新;pull处理方式://判断加速计是否可用if([_motionManagerisAccelerometerAvailable]){//设置加速计采样频率[_motionManagersetAccelerometerUpdateInterval:1/40.0];[_motionManagerstartAccelerometerUpdates];北京千锋互联科技有限公司版权所有}触摸结束:-(void)touchesEnded:(NSSet*)toucheswithEvent:(UIEvent*)event{CMAccelerationacceleration=_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=[[CMMotionManageralloc]init];//判断加速计是否可用if([_motionManagerisAccelerometerAvailable]){//设置加速计频率[_motionManagersetAccelerometerUpdateInterval:1/40.0];//开始采样数据[_motionManagerstartAccelerometerUpdatesToQueue:_quenewithHandler:^(CMAccelerometerData*accelerometerData,NSError*error){NSLog(@%f---%f,accelerometerData.acceleration.x,accelerometerData.acceleration.y);}];}时间设置频率:陀螺仪陀螺仪与加速计的代码在结构上是相同的,不同之处只是在于调用哪些方法和如何访问报告的值,它们非常相似,首先看张陀螺仪旋转的角度图片:北京千锋互联科技有限公司版权所有陀螺仪更新数据也有两种方式,pull方式(startGyroUpdates),push方式(startGyroUpdatesToQueue):staticconstNSTimeIntervalgyroMin=0.01;-(void)startUpdatesWithSliderValue:(int)sliderValue{//DeterminetheupdateintervalNSTimeIntervaldelta=0.005;NSTimeIntervalupdateInterval=gyroMin+delta*sliderValue;//CreateaCMMotionManagerCMMotionManager*mManager=[(APLAppDelegate*)[[UIApplicationsharedApplication]delegate]sharedManager];APLGyroGraphViewController*__weakweakSelf=self;//Checkwhetherthegyroscopeisavailableif([mManagerisGyroAvailable]==YES){//Assigntheupdateintervaltothemotionmanager[mManagersetGyroUpdateInterval:updateInterval];[mManagerstartGyroUpdatesToQueue:[NSOperationQueuemainQueue]withHandler:^(CMGyroData*gyroData,NSError*error){[weakSelf.graphViewaddX:gyroData.rotationRate.xy:gyroData.rotationRate.yz:gyroData.rotationRate.z];[weakSelfsetLabelValueX:gyroData.rotationRate.xy:gyroData.rotationRate.yz:gyroData.rotationRate.z];}];}北京千锋互联科技有限公司版权所有self.updateIntervalLabel.text=[NSStringstringWithFormat:@%f,updateInterval];}-(void)stopUpdates{CMMotionManager*mManager=[(APLAppDelegate*)[[UIApplicationsharedApplication]delegate]sharedManager];if([mManagerisGyroActive]==YES){[mManagerstopGyroUpdates];}}以上就是使用加速计和陀螺仪的简单方法。

1 / 5
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功