Accelerometer实际是有三个accelerometer组成的,分别代表x y z三个坐标,根据计算这三个坐标的运动探测出设备的移动和方向。
访问设备数据的三种不同方式:
如果只是想探测设备的方向,不必知道方向矢量,使用UIDevices就够了。
如果想设备在设备晃动的时候响应,通过UIKit的motion-event处理方法
如果上述两个都不能满足需求,使用Core Motion Framework访问accelerometer、gyroscope和 device motion classes
Motion Event使用responder chain来处理事件。当用户开始晃动设备的时候,iOS发送第一个motion event给第一个responder来处理。如果第一个放弃处理,则转交给下一个。
处理Motion Event事件可以重载方法 motionBegan:withEvent: , motionEnded:withEvent: , and motionCancelled:withEvent: 了
需要在Info.plist文件里添加相应访问权限,包括:accelerometer, gyroscope
Core Motion Framework通常用来获取accelerometer和gyroscope数据并传给app层进行处理。
Core Motion不会跟UIEvent链接也不会使用responder chain,而是直接和需要的app交互。
CMAccelerometerData 捕获加速感应器的每一个空间轴
CMGyroData 捕获围绕三个空间轴的旋转度
CMDeviceMotion 封装各个测量值,包括坐标、旋转和加速度等
CMMotionManager 是Core Motion的重要一个类,用来管理Motion Event各流程;有两个方式来处理motion data:
Pull app申请获得更新的数据
Push app指定更新的间隔、实现 block 来处理数据;
Handling Accelerometer Events:
Pull: startAccelerometerUpdates
Push: startAccelerometerUpdatesToQueue:withHandler:
Handling Rotation Rate Data:处理该类事件首先创建一个CMMotionManager然后:
Pull: startGyroUpdates 持续更新CMMotionManger的 gyroData 属性
Push: startGyroUpdatesToQueue:withHandler:
Handling Processed Device Motion Data
Pull: startDeviceMotionUpdates 持续更新CMMotionManger的 deviceMotion 属性
Push: startDeviceMotionUpdatesToQueue:withHandler:
Remote Control Events: 允许用户控制app的多媒体,包括音视频内容
为了接受remote control events,app需要做到:
Be the first responder: view或view controller可能需要重载 canBecomeFirstResponder 并发送自己 becomeFirstResponder
Turn on the delivery of remote control events
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
Begin playing audio
remoteControlReceivedWithEvent: 来处理Remote control events
访问设备数据的三种不同方式:
如果只是想探测设备的方向,不必知道方向矢量,使用UIDevices就够了。
如果想设备在设备晃动的时候响应,通过UIKit的motion-event处理方法
如果上述两个都不能满足需求,使用Core Motion Framework访问accelerometer、gyroscope和 device motion classes
Motion Event使用responder chain来处理事件。当用户开始晃动设备的时候,iOS发送第一个motion event给第一个responder来处理。如果第一个放弃处理,则转交给下一个。
处理Motion Event事件可以重载方法 motionBegan:withEvent: , motionEnded:withEvent: , and motionCancelled:withEvent: 了
需要在Info.plist文件里添加相应访问权限,包括:accelerometer, gyroscope
Core Motion Framework通常用来获取accelerometer和gyroscope数据并传给app层进行处理。
Core Motion不会跟UIEvent链接也不会使用responder chain,而是直接和需要的app交互。
CMAccelerometerData 捕获加速感应器的每一个空间轴
CMGyroData 捕获围绕三个空间轴的旋转度
CMDeviceMotion 封装各个测量值,包括坐标、旋转和加速度等
CMMotionManager 是Core Motion的重要一个类,用来管理Motion Event各流程;有两个方式来处理motion data:
Pull app申请获得更新的数据
Push app指定更新的间隔、实现 block 来处理数据;
Handling Accelerometer Events:
Pull: startAccelerometerUpdates
Push: startAccelerometerUpdatesToQueue:withHandler:
Handling Rotation Rate Data:处理该类事件首先创建一个CMMotionManager然后:
Pull: startGyroUpdates 持续更新CMMotionManger的 gyroData 属性
Push: startGyroUpdatesToQueue:withHandler:
Handling Processed Device Motion Data
Pull: startDeviceMotionUpdates 持续更新CMMotionManger的 deviceMotion 属性
Push: startDeviceMotionUpdatesToQueue:withHandler:
Remote Control Events: 允许用户控制app的多媒体,包括音视频内容
为了接受remote control events,app需要做到:
Be the first responder: view或view controller可能需要重载 canBecomeFirstResponder 并发送自己 becomeFirstResponder
Turn on the delivery of remote control events
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
// Turn off remote control event delivery |
[[UIApplication sharedApplication] endReceivingRemoteControlEvents]; |
// Resign as first responder |
[self resignFirstResponder]; |
Begin playing audio
remoteControlReceivedWithEvent: 来处理Remote control events