RxMotionKit - reactive motion sensors managing library 🏃
RxMotionKit is a motion sensors managing library written in Swift.
Library wraps CoreMotion and RxSwift frameworks and provides pleasant way to obtain data from sensors.
RxMotionKit is available through CocoaPods. To install
it, simply specify it in your Podfile
:
pod 'RxMotionKit', '~> 0.7.0'
Then, run the following command:
$ pod install
Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks. To integrate RxMotionKit into your Xcode project using Carthage, specify it in your Cartfile
:
github "rsobolewski/RxMotionKit" ~> 0.7.0
Run carthage update
to build the frameworks and drag them into your Xcode project.
To start updating data from sensors import RxMotionKit
module and get reference to singleton instance of MotionManager
:
import RxSwift
import RxMotionKit
unowned let motionManager = MotionManager.shared
subscribe an Observable:
motionManager.rx_didUpdateMotionData
.subscribe(onNext: { (data) in
// Handle received data
})
and start updating selected sources:
motionManager.startUpdating([
.accelerometr,
.gyroscope,
.magnetometer,
.deviceMotion
],
withInterval: 5.0
)
For convenient usage when you want to obtain few updates you can use take
to automatically stop updates when defined limit is reached:
motionManager.rx_didUpdateMotionData
.take(4)
.subscribe(onNext: { (data) in
// Handle received data
})
To stop updates from selected sources:
motionManager.stopUpdating([
.accelerometr,
.gyroscope,
.magnetometer,
.deviceMotion
])
All motion sources that are available:
accelerometr
- Accelerometergyroscope
- Gyroscopemagnetometer
- MagnetometerdeviceMotion
- DeviceMotionAlso you can conveniently specify all motion sources using MotionSourceType.all
.
There are several types of Observables provided by MotionManager
:
rx_didUpdateMotionData
- general observable for motion data updates, that merges all events from all source updatesrx_didUpdateAccelerometerData
- accelerometer updatesrx_didUpdateGyroscopeData
- gyroscope updatesrx_didUpdateMagnetometerData
- magnetometer updatesrx_didUpdateDeviceMotionData
- device motion updatesAll sources require the update time interval, that must be provided in method which start updates:
let updateTimeInterval = 1.0 // 1 sec
startUpdating([.accelerometer], withInterval: updateTimeInterval)
For device motion updates you can choose a reference frame for attitude, which will be default if you not provide any.
startUpdating([.accelerometer], withInterval: updateTimeInterval, referenceFrame: .xArbitraryZVertical)
If you want to contribute, please submit a pull request. For code consistency please use SwiftLint with configuration from this repository.
Robert Sobolewski, contact me on Twitter
RxMotionKit is available under the MIT license. See the LICENSE file for more info.