InertialKit 是一个面向 macOS 的 Swift SDK 项目,用于读取兼容 Mac 笔记本的内置加速度计和陀螺仪,并获取线性加速度、角速度与机身姿态
Important
Mac 运动传感器不是公开、稳定的系统能力,兼容性可能因机型和系统版本而异。
使用时需要具备 root 权限,且不支持 App Sandbox 运行模式
-
选择 File > Add Package Dependencies…
-
输入仓库地址
https://github.com/bob-zebedy/InertialKit.git -
Dependency Rule选择 Exact Version 填写对应的 Tag 版本 -
将
InertialKit添加到对应的 macOS target
在 Package.swift 的依赖列表中添加以下包
dependencies: [
.package(
url: "https://github.com/bob-zebedy/InertialKit.git",
exact: "x.x.x"
)
]然后在对应的 target 中引用
dependencies: [
.product(name: "InertialKit", package: "InertialKit")
]在支持 async throws 的上下文中读取当前运动数据
import InertialKit
let monitor = MotionMonitor()
let sample = try await monitor.sample()
print(sample.linearAcceleration)
print(sample.rotationRate)
print(sample.attitude)使用、兼容性、配置、完整 API 及错误处理请参阅 使用指南