目录
目录README.md

MotionProvider

A Combine-based CoreMotion data provider as a Swift Package.

On every update of the device motion data (accelerometer and gyroscope), it provides a struct

struct MotionData {
    var timestamp : Date
    var acc_x : Double // userAcceleration.x
    var acc_y : Double // userAcceleration.y
    var acc_z : Double // userAcceleration.z
    var rot_x : Double // rotationRate.x
    var rot_y : Double // rotationRate.y
    var rot_z : Double // rotationRate.z
}

through a PassthroughSubject<MotionData, Never> called motionWillChange, as well as a published property motion.

If real location data is unavailable on the device (e.g., the Simulator), it provides random fake-motion data scheduled by a timer.

Usage

Starting the Motion Provider

Initialize and start the MotionProvider

let motionProvider = MotionProvider()
motionProvider.start()

Handling the Motion Data

Subscribe to the motionWillChange subject and store the returned Cancellable

cancellableMotion = motionProvider.motionWillChange.sink { md in
    handleMotion(md)
}

The function handleMotion in the sink closure is executed on every MotionData object sent by the MotionProvider.

Also, the MotionProvider is an ObservableObject which has a published property motion that updates the ObservableObject. This dynamic property can directly be accessed in SwiftUI.

Stopping the Motion Provider

Stop the MotionProvider and cancel the subscription

motionProvider.stop()
cancellableMotion?.cancel()
关于
50.0 KB
邀请码
    Gitlink(确实开源)
  • 加入我们
  • 官网邮箱:gitlink@ccf.org.cn
  • QQ群
  • QQ群
  • 公众号
  • 公众号

©Copyright 2023 CCF 开源发展委员会
Powered by Trustie& IntelliDE 京ICP备13000930号