现在您可以通过两行或更少的代码以神奇的方式从加速度计、磁力计、陀螺仪和设备运动中抓取数据。
用于 CoreMotion 框架 的一个简洁且易于操作的包装,完全用 Swift 编写。Core Motion 框架可以让您的应用程序从设备硬件接收运动数据并处理该数据。您可以从 加速度计、陀螺仪 和 磁力计 中检索数据。您还可以从 deviceMotion
数据类型本身获取精确且处理过的陀螺仪和加速度计数据,而不是获取原始值。
与 MotionKit 一起的文章: 链接1 — 链接2 — 链接3
您可以通过尾随闭包或代理方法检索所有值。两种方法都完全受支持。
注意: 所有的提供方法都是异步的,它们在自己的队列中操作,以便您的应用程序能够平稳高效地运行。
首先,初始化 MotionKit 实例。这是一个必要步骤。
let motionKit = MotionKit()
您可以通过几行代码来获取加速度计值。
motionKit.getAccelerometerValues(interval: 1.0){
(x, y, z) in
//Interval is in seconds. And now you have got the x, y and z values here
....
}
以下是一些获取陀螺仪值的代码示例。
motionKit.getGyroValues(interval: 1.0){
(x, y, z) in
//Your processing will go here
....
}
获取磁力计值就像抓饼干一样简单。
motionKit.getMagnetometerValues(interval: 1.0){
(x, y, z) in
//Do something with the retrieved values
....
}
嵌入式框架需要 iOS 8 及以上版本的最低部署目标。
只需将 MotionKit.swift
文件复制到您的 Xcode 项⽬文件夹中,您就可以开始使用了。
如果您想获取加速度计或陀螺仪的处理过后的值,可以直接通过deviceMotion对象获取这些值,或者,您可以通过与尾随闭包和委托无缝配合的独立方法获取各个单独的值。
deviceMotion对象包括
所有这些值都可以通过单独的方法或通过获取deviceMotion对象本身来检索。
motionKit.getDeviceMotionObject(interval: 1.0){
(deviceMotion) -> () in
var accelerationX = deviceMotion.userAcceleration.x
var gravityX = deviceMotion.gravity.x
var rotationX = deviceMotion.rotationRate.x
var magneticFieldX = deviceMotion.magneticField.x
var attitideYaw = deviceMotion.attitude.yaw
....
}
您可以通过Device Motion服务通过几行代码获取经过精细处理的用户加速度,无论是通过尾随闭包还是通过委托方法。
motionKit.getAccelerationFromDeviceMotion(interval: 1.0){
(x, y, z) -> () in
// Grab the x, y and z values
....
}
同样,您也可以通过Device Motion服务获取它。
motionKit.getGravityAccelerationFromDeviceMotion(interval: 1.0) {
(x, y, z) -> () in
// x, y and z values are here
....
}
有趣,以一种神奇的方式获取。
motionKit.getMagneticFieldFromDeviceMotion(interval: 1.0) {
(x, y, z, accuracy) -> () in
// Get the values with accuracy
....
}
motionKit.getAttitudeFromDeviceMotion(interval: 1.0) {
(attitude) -> () in
var roll = attitude.roll
var pitch = attitude.pitch
var yaw = attitude.yaw
var rotationMatrix = attitude.rotationMatrix
var quaternion = attitude.quaternion
....
}
motionKit.getRotationRateFromDeviceMotion(interval: 1.0) {
(x, y, z) -> () in
// There you go, grab the x, y and z values
....
}
针对性能问题,建议您在整个应用程序中只使用一个CMMotionManager实例。确保在完成工作后立即停止接收传感器的更新。您可以在MotionKit中像这样操作。
//Make sure to call the required function when you're done
motionKit.stopAccelerometerUpdates()
motionKit.stopGyroUpdates()
motionKit.stopDeviceMotionUpdates()
motionKit.stopmagnetometerUpdates()
如果您不想使用尾随闭包,我们为您准备了解决方案。MotionKit支持以下委托方法以检索传感器值。
optional func retrieveAccelerometerValues (x: Double, y:Double, z:Double, absoluteValue: Double)
optional func retrieveGyroscopeValues (x: Double, y:Double, z:Double, absoluteValue: Double)
optional func retrieveDeviceMotionObject (deviceMotion: CMDeviceMotion)
optional func retrieveMagnetometerValues (x: Double, y:Double, z:Double, absoluteValue: Double)
optional func getAccelerationValFromDeviceMotion (x: Double, y:Double, z:Double)
optional func getGravityAccelerationValFromDeviceMotion (x: Double, y:Double, z:Double)
optional func getRotationRateFromDeviceMotion (x: Double, y:Double, z:Double)
optional func getMagneticFieldFromDeviceMotion (x: Double, y:Double, z:Double)
optional func getAttitudeFromDeviceMotion (attitude: CMAttitude)
要使用上述委托方法,您必须将MotionKit委托添加到您的ViewController中。
class ViewController: UIViewController, MotionKitDelegate {
...
}
在viewDidLoad方法中,您只需要添加这个。
override func viewDidLoad() {
super.viewDidLoad()
motionKit.delegate = self
......
}
这样,您可能希望实现一个类似于以下方法的委托方法。
func retrieveAccelerometerValues (x: Double, y:Double, z:Double, absoluteValue: Double){
//Do whatever you want with the x, y and z values. The absolute value is calculated through vector mathematics
......
}
func retrieveGyroscopeValues (x: Double, y:Double, z:Double, absoluteValue: Double){
//Do whatever you want with the x, y and z values. The absolute value is calculated through vector mathematics
......
}
如果您想要在一个给定时间点获取任何可用传感器的单个值,您可能会使用我们在MotionKit中提供的一些实用方法。
motionKit.getAccelerationAtCurrentInstant {
(x, y, z) -> () in
....
}
motionKit.getAccelerationAtCurrentInstant {
(x, y, z) -> () in
....
}
motionKit.getAttitudeAtCurrentInstant {
(x, y, z) -> () in
....
}
motionKit.getMageticFieldAtCurrentInstant {
(x, y, z) -> () in
....
}
motionKit.getGyroValuesAtCurrentInstant {
(x, y, z) -> () in
....
}
您可以加入我们的Reddit频道来讨论任何内容。
您也可以在此处提出任何您想要的特性集问题。我们非常乐意听取您的意见。
不要忘记订阅我们的Reddit频道,频道地址为:/r/MotionKit
我们的StackOverflow标签是“MotionKit”
本作品受Creative Commons Attribution-NonCommercial 4.0 International License许可。