前言

	NS_CLASS_AVAILABLE(NA,4_0) @interface CMMotionManager : NSObject
@available(iOS 4.0, *) public class CMMotionManager : NSObject
  • 对于 iPhone 手机来说,画面上下为 y 轴,左右为 x 轴,前后为 z 轴。各自向上、向右和前面为正方向。当向上方向有作用力时,y 属性中设置相应的正值,当向左方向有作用力时,x 属性中设置相应负值。加速度不仅受震动手机时施加作用力的影响,还会持续受重力的影响。因此手机如果垂直拿在手上的话,y 轴负方向将受重力作用, y 属性将一直为负值(最小值为 -1.0)。相反,如果画面的上方向朝向地下,则 y 属性将一直为正值(最大值为 1.0)。当画面与地面水平时,y 属性值为 0 。

  • 加速度传感器以前用 UIAccelerometer 实现,但 Xcode6 后完全废弃了 UIAccelerometer,使用 CoreMotion 替代它能监听到 x、y、z 三个方向的加速度。具体使用步骤如下:

    • 1、引用头文件 CoreMotion/CoreMotion.h,实例化 CMMotionManager 类。
    • 2、向 CMMotionManager 的 accelerometerUpdateInterval 属性中设置通知间隔时间值。
    • 3、使用 [NSOperationQueue currentQueue] 建立一个监听队列。
    • 4、使用 startAccelerometerUpdatesToQueue 方法更新监听队列,并设置回调函数用于接收加速度通知,通知间隔时间已经在第二步设置过。在回调函数中使用 accelerometerData.acceleration 相关属性可以获得 x、y、z 各个方向的加速度。accelerometerData.acceleration 拥有代表 x 轴方向加速度的 x 属性、拥有代表 y 轴方向加速度的 y 属性、拥有代表 z 轴方向加速度的 z 属性。
  • 在 iPhone 开发文档中,推荐使用的通知间隔如下表:

    用途 通知间隔
    检测设备朝向时 1/10 - 1/20
    在游戏中需要实时使用加速度传感器时 1/30 - 1/60
    检测敲击设备或者剧烈摇动设备的情况下 1/70 - 1/100

1、CoreMotion 的创建

  • Objective-C

    	// 引用头文件 #import <CoreMotion/CoreMotion.h>
    
    	// 实例化 CMMotionManager
    CMMotionManager *motionManger = [[CMMotionManager alloc] init]; // 设置通知间隔时间
    motionManger.accelerometerUpdateInterval = 1/20; // 判断加速度值是否可获取
    BOOL available = motionManger.isAccelerometerAvailable; if (available) { // 创建监听队列
    NSOperationQueue *queue = [NSOperationQueue currentQueue]; // 更新监听队列
    [motionManger startAccelerometerUpdatesToQueue:queue withHandler:
    ^(CMAccelerometerData *accelerometerData, NSError *error) { // 获取 x 轴方向的加速度值,typedef double UIAccelerationValue;
    UIAccelerationValue speedX = accelerometerData.acceleration.x; // 获取 y 轴方向的加速度值
    UIAccelerationValue speedY = accelerometerData.acceleration.y; // 获取 z 轴方向的加速度值
    UIAccelerationValue speedZ = accelerometerData.acceleration.z; NSLog(@"%f", speedX);
    NSLog(@"%f", speedY);
    NSLog(@"%f", speedZ);
    }];
    }
  • Swift

    	// 引用头文件 import CoreMotion
    
    	// 实例化 CMMotionManager
    let motionManger:CMMotionManager = CMMotionManager() // 设置通知间隔时间
    motionManger.accelerometerUpdateInterval = 1/20 // 判断加速度值是否可获取
    let available:Bool = motionManger.accelerometerAvailable if available { // 创建监听队列
    let queue:NSOperationQueue = NSOperationQueue.currentQueue()! // 更新监听队列
    motionManger.startAccelerometerUpdatesToQueue(queue, withHandler:
    { (accelerometerData:CMAccelerometerData?, error:NSError?) in // 获取 x 轴方向的加速度值,typedef double UIAccelerationValue;
    let speedX:UIAccelerationValue = accelerometerData!.acceleration.x // 获取 y 轴方向的加速度值
    let speedY:UIAccelerationValue = accelerometerData!.acceleration.y // 获取 z 轴方向的加速度值
    let speedZ:UIAccelerationValue = accelerometerData!.acceleration.z print(speedX)
    print(speedY)
    print(speedZ)
    })
    }

2、系统震动事件处理方法

  • Objective-C

    	// 震动开始,重写 UIResponder 中定义的方法
    - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event { } // 震动结束,重写 UIResponder 中定义的方法
    - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event { } // 震动取消,重写 UIResponder 中定义的方法
    - (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event { }
  • Swift

    	// 震动开始,重写 UIResponder 中定义的方法
    override func motionBegan(motion: UIEventSubtype, withEvent event: UIEvent?) { } // 震动结束,重写 UIResponder 中定义的方法
    override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent?) { } // 震动取消,重写 UIResponder 中定义的方法
    override func motionCancelled(motion: UIEventSubtype, withEvent event: UIEvent?) { }

iOS - CoreMotion的更多相关文章

  1. iOS CoreMotion框架(传感器)

    我们知道iOS的应用真的太多了,很多应用让我们惊叹不已!!!很多意想不到的应用! 比如: 1.电子罗盘指南针之类的应用-让我们知道方向. 2.运动类型软件-让我们知道我们跑步多少公里. 3.社交软件中 ...

  2. iOS CoreMotion 纪录步数

    - (void)startUpdateAccelerometer{    /* 设置采样的频率,单位是秒 */    NSTimeInterval updateInterval = 0.05; // ...

  3. 最新 iOS 框架整体梳理(一)

    前言 这段话其实是我差不多写完文章之后再回过头来写的,原本在写文章之前想写一下写的初衷的,但当我写完之后感觉初衷没有收获更真切一些.其实到这篇为止总结出来的也就三十多个,有些是比较新的框架,有些是我们 ...

  4. iOS开发-CoreMotion框架(加速计和陀螺仪)

    CoreMotion是一个专门处理Motion的框架,其中包含了两个部分加速度计和陀螺仪,在iOS4之前加速度计是由UIAccelerometer类来负责采集数据,现在一般都是用CoreMotion来 ...

  5. iOS开发-CoreMotion框架

    转自: CoreMotion是一个专门处理Motion的框架,其中包含了两个部分 加速度计和陀螺仪,在iOS4之前加速度计是由 UIAccelerometer 类 来负责采集数据,现在一般都是用Cor ...

  6. ios中陀螺仪CoreMotion的用法

    转自:http://code.eoe.cn/471/title/ios涓檧铻轰华CoreMotion鐨勭敤娉 README.md 外部引用 原始文档 以前在iphone中要得到加速度时,只能使用Ac ...

  7. unity导出工程导入到iOS原生工程中详细步骤

    一直想抽空整理一下unity原生工程导入iOS原生工程中的详细步骤.做iOS+vuforia+unity开发这么长时间了.从最初的小小白到现在的小白.中间趟过了好多的坑.也有一些的小小收货.做一个喜欢 ...

  8. iOS - AliPay 支付宝支付

    1.支付宝支付申请 支付宝支付官方签约集成指引 支付宝APP支付官方集成指引 蚂蚁金服开放平台 1.1 支付宝 APP 支付申请步骤 APP 支付:APP 支付是商户通过在移动端应用 APP 中集成开 ...

  9. IOS框架和服务

    在iOS中框架是一个目录,包含了共享资源库,用于访问该资源库中储存的代码的头文件,以及图像.声音文件等其他资源.共享资源库定义应用程序可以调用的函数和方法. iOS为应用程序开发提供了许多可使用的框架 ...

随机推荐

  1. html5 canvas 笔记四(变形 Transformations)

    绘制复杂图形必不可少的方法 save() 保存 canvas 状态 restore() 恢复 canvas 状态 Canvas 的状态就是当前画面应用的所有样式和变形的一个快照. Canvas 的状态 ...

  2. eclipse+maven 无法编译

    Archive for required library: 'F:/mavenLib/org/mybatis/mybatis/3.4.1/mybatis-3.4.1.jar' in project ' ...

  3. C#的OpenFileDialog和SaveFileDialog的常见用法(转)

    OpenFileDialog openFileDialog1 = new OpenFileDialog();            openFileDialog1.InitialDirectory = ...

  4. 第十一章 Android 内核驱动——Alarm

    11.1  基本原理 Alarm 闹钟是 android 系统中在标准 RTC 驱动上开发的一个新的驱动,提供了一个定时器 用于把设备从睡眠状态唤醒,当然因为它是依赖 RTC 驱动的,所以它同时还可以 ...

  5. YTU 3002: 出栈顺序(栈和队列)

    3002: 出栈顺序(栈和队列) 时间限制: 1 Sec  内存限制: 128 MB 提交: 80  解决: 20 题目描述 给出一个入栈序列,和一个出栈序列,判断该出栈序列是否正确. 输入 输入包含 ...

  6. 在Windows Live Writer中插入C# code

    平时都是用Windows Live Writer写博客,发布博客.遇到需要插入代码都是先在notepad中写好,或者是拷贝到notepad,再从notepad中拷到Windows Live Write ...

  7. JAVA基础知识之网络编程——-关于阻塞IO/非阻塞IO/同步IO/异步IO的一些参考文章

    Java NIO之多个Selector的实现Java NIO类库Selector机制解析(上) Java NIO类库Selector机制解析(下) https://www.zhihu.com/ques ...

  8. noi 7627 鸡蛋的硬度

    题目链接:http://noi.openjudge.cn/ch0206/7627/ 题目讲的二分其实是一个误导, d(i,j),表示当前最优策略时,最坏的情况下: 有 J 个鸡蛋,I 个可以怀疑的楼层 ...

  9. python请求java Selenium Webdriver

    下载jar包: selenium-server-standalone-2.44.0.jar 运行jar包: java -jar selenium-server-standalone-2.44.0.ja ...

  10. php 基础复习(2)GD库

    一,生成验证码: 1.生成一张图片: recource imagecreatetruecolor(int $width , int $height)  注意:提前输出图片的header信息,默认是黑色 ...