博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS - CoreMotion
阅读量:7238 次
发布时间:2019-06-29

本文共 3851 字,大约阅读时间需要 12 分钟。

前言

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 
    // 实例化 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?) {    }

转载于:https://www.cnblogs.com/QianChia/p/5759077.html

你可能感兴趣的文章
objc/runtime.h 查看私有api
查看>>
Linux下执行jmeter脚本
查看>>
java笔记之分支、循环语句
查看>>
我的Ajax学习笔记
查看>>
Struts2遇到Caused by Action class not found这类问题怎么解决
查看>>
ViewPager实现滑动翻页效果
查看>>
自定义HorizontalScrollView的scrollBar
查看>>
轻量高效的开源JavaScript插件和库
查看>>
CSS3-background-clip+background-origin
查看>>
linux yum 安装mysql
查看>>
种下一棵树:有旋Treap
查看>>
设计模式——(Abstract Factory)抽象工厂“改正为简单工厂”
查看>>
图灵热点之阅读篇——七月图书推荐
查看>>
【转载】acedSSGet()函数用法详解
查看>>
bzoj5407: girls
查看>>
BootStrap selectpicker后台动态绑定数据
查看>>
【转】正则基础之——贪婪与非贪婪模式
查看>>
关于 android.net.conn.CONNECTIVITY_CHANGE 7.0之后取消
查看>>
自动化测试的理解
查看>>
微信小程序事件
查看>>