




全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
北京iOS开发教程-CoreMotion框架CoreMotion是一个专门处理Motion的框架,其中包含了两个部分加速计和陀螺仪,在iOS4之前加速计是由UIAccelerometer类来负责采集数据,现在一般都是用CoreMotion来处理加速度过程,不过由于UIAccelerometer比较简单,同样有人在使用。加速计由三个坐标轴决定,用户最常见的操作设备的动作移动,晃动手机(摇一摇),倾斜手机都可以被设备检测到,加速计可以检测到线性的变化,陀螺仪可以更好的检测到偏转的动作,可以根据用户的动作做出相应的动作,iOS模拟器无法模拟以上动作,真机调试需要开发者账号。加速计通过感知特定方向的惯性力总量,加速计可以测量出加速度和重力。iOS设备内的加速计是一个三轴加速计,也就是说它能够检测到三维空间中的运动或重量。因此,加速计不但可以指示用户握持设备的方式(如自动旋转功能),而且还可以在设备被放在桌子上时指示其正面朝上还是朝下。如图所示展示了加速计所使用的是三轴结构,需要注意的是,加速计对y坐标使用了更标准的惯例,即y轴伸长表示向上的力。如果加速计将Quartz2D作为控制机制,那么必须要转换y坐标轴。使用OpenGL ES时(使用加速计控制动画时通常会用到),则不需要转换。如果只需要知道设备的方向,不需要知道具体方向矢量角度,那么可以使用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 /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; 触摸结束:-(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 allocinit; /判断加速计是否可用 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); ; 时间设置频率:陀螺仪陀螺仪与加速计的代码在结构上是相同的,不同之处只是在于调用哪些方法和如何访问报告的值,它们非常相似,首先看张陀螺仪旋转的角度图片:陀螺仪更新数据也有两种方式,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 stringWithFo
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- (完整版)数学六年级下册期末专题资料试题A卷解析
- 【语文】江苏省南京市瑞金北村小学三年级下册期末复习试题(含答案)
- 小学五年级上学期阅读理解专项英语复习培优试题测试卷
- 五年级下册期中英语质量模拟试题测试卷(含答案)
- 【语文】北京市中关村第二小学四年级上册期末复习试题(含答案)
- (完整版)七年级下册实数数学综合测试卷及答案(二)培优试题
- 2025年初级银行从业资格之初级公司信贷基础试题库和答案
- 2025市政施工员考试试题库附答案
- 2025年职业健康培训考核考试练习题及答案解析
- 2025年大学人力资源管理试题及答案
- 消防员心理测试题库及答案解析
- 2025小升初租房合同模板
- 放射科造影剂过敏反应应急处理预案
- 《大嘴巴纸玩偶》名师课件
- 2025年上海市高考英语热点复习:阅读理解说明文
- 国家管网集团合同范本
- 中医全科学科
- Unit 1 Teenage life单词变形-学生背诵与默写清单-2024-2025学年高中英语人教版(2019)必修第一册
- 铁路技术规章:018铁路军事运输管理办法
- 生物发酵安全培训
- 2024-2025学年广东省深圳市九年级上学期期中数学试题及答案
评论
0/150
提交评论