Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Get the data from Accelerometer, Gyroscope and Magnetometer in only Two or a few lines of code. CoreMotion now made insanely simple:octocat: 📡

License

NotificationsYou must be signed in to change notification settings

MHaroonBaig/MotionKit

Repository files navigation

#[MotionKit] (http://goo.gl/bpXBlO) — The missing iOS wrapper 🎶

LanguageCarthage compatibleVersionLicenseIssuesJoin the chat at https://gitter.im/MHaroonBaig/MotionKit

Now you can grab the data from Accelerometer, Magnetometer, Gyroscope and Device Motion in a magical way, with just a Two or a few lines of code.Fully compatible with both Swift and Objective-C.

A nice and clean wrapper around theCoreMotion Framework written entirely in Swift. The Core Motion framework lets your application receive motion data from device hardware and process that data.The data can be retrieved fromAccelerometer,Gyroscope andMagnetometer.You can also get therefined and processed gyroscope and accelerometer data from thedeviceMotion datatype itself instead of getting the raw values.

Articles accompanying MotionKit:Link1Link2Link3#How does it workYou can retrieve all the values either by a trailing closure or by a delegate method. Both the approaches are fully supported.

Note: All the provided methods are Asynchronous and operate in their own Queue so that your app could run smoothly and efficiently.

##InitialiseFirst, initialise the MotionKit instance. Its a Must.

Swift
```swift let motionKit = MotionKit()```
Objective-C
```objective-c MotionKit *motionKit = [[NSClassFromString(@"MotionKit") alloc] init];```

##Getting Accelerometer ValuesYou can get the accelerometer values using just a few lines of code.

Swift
```swift 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 .... }```
Objective-C
```objective-c [motionKit getAccelerometerValuesWithInterval:1.0 values:^(double x, double y, double z) { // your values here }];```

##Getting Gyroscope ValuesGyroscope values could be retrieved by the following few lines of code.

Swift
```swift motionKit.getGyroValues(interval: 1.0){ (x, y, z) in //Your processing will go here .... }```
Objective-C
```objective-c [motionKit getGyroValues:1.0 values:^(double x, double y, double z) { // your values here }];```

##Getting Magnetometer ValuesGetting Magnetometer values is as easy as grabbing a cookie.

Swift
```swift motionKit.getMagnetometerValues(interval: 1.0){ (x, y, z) in //Do something with the retrieved values .... }```
Objective-C
```objective-c [motionKit getMagnetometerValues:1.0 values:^(double x, double y, double z) { // your values here }];```

#InstallationEmbedded frameworks require a minimum deployment target of iOS 8.###— Using CocoaPodsJust add this to your Podfile.

pod'MotionKit'

Note that you have to use CocoaPods version 0.36, the pre-released version which supports swift. If you don't already have it, you can grab it with a single command.

[sudo] gem install cocoapods --pre

###— Using CarthageYou can useCarthage to installMotionKit by adding the following line to yourCartfile.

github"MHaroonBaig/MotionKit"

###— Manual InstallationJust copy theMotionKit.swift file into your Xcode project folder, and you're ready to go.

#CMDeviceMotion - as easy as pieIn case if you want to get the processed values of Accelerometer or Gyroscope, you can access the deviceMotion object directly to get those values, or, you can access the individual values from the standalone methods which work seamlessly with Trailing Closures and Delegates.

The deviceMotion object includes:

  • Acceleration Data
    • userAcceleration
    • gravity
  • Calibrated Magnetic Field
    • magneticField
  • Attitude and Rotation Rate
    • attitude
    • rotationRate

All of the values can be retrieved either by individual methods or by getting the deviceMotion object itself.

###Getting the whole CMDeviceMotion Object

Swift
```swift
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      ....    }
<div align="right"><h7><i>Objective-C</i></h7></div>```objective-c[motionKit getDeviceMotionObjectWithInterval:1.0 values:^(CMDeviceMotion *deviceMotion) {    // Your values here}];

###Getting refined values of Acceleration

You can get the refined and processed userAccelaration through the Device Motion service by just a few lines of code, either by a Trailing Closure or through Delegation method.

Swift
    motionKit.getAccelerationFromDeviceMotion(interval:1.0){(x, y, z)->()in          // Grab the x, y and z values....}
Objective-C
```objective-c [motionKit getAccelerationFromDeviceMotion:1.0 values:^(double x, double y, double z) { // your values here }];```

###Getting Gravitational AccelerationAgain, you can access it through the Device Motion service as well.

Swift
```swift motionKit.getGravityAccelerationFromDeviceMotion(interval: 1.0) { (x, y, z) -> () in // x, y and z values are here .... }```
Objective-C
```objective-c [motionKit getGravityAccelerationFromDeviceMotion:1.0 values:^(double x, double y, double z) { // your values here }];```

###Getting Magnetic Field around your deviceInteresting, Get it in a magical way.

Swift
```swift motionKit.getMagneticFieldFromDeviceMotion(interval: 1.0) { (x, y, z, accuracy) -> () in // Get the values with accuracy .... }
<div align="right"><h7><i>Objective-C</i></h7></div>```objective-c    [motionKit getMagneticFieldFromDeviceMotion:1.0 values:^(double x, double y, double z) {        // your values here    }];

###Getting the Attitude metrics

Swift
```swift 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 .... }```
Objective-C
```objective-c [motionKit getAttitudeFromDeviceMotionWithInterval:1.0 values:^(CMAttitude *attitude) { // Your values here }];```

###Getting Rotation Rate of your device

Swift
```swift motionKit.getRotationRateFromDeviceMotion(interval: 1.0) { (x, y, z) -> () in // There you go, grab the x, y and z values .... }
<div align="right"><h7><i>Objective-C</i></h7></div>```objective-c    [motionKit getRotationRateFromDeviceMotion:1.0 values:^(double x, double y, double z) {        // your values here    }];

##PrecautionsFor performance issues, it is suggested that you should use only one instance of CMMotionManager throughout the app. Make sure to stop receiving updates from the sensors as soon as you get your work done.You can do this in MotionKit like this.

    //Make sure to call the required function when you're done    motionKit.stopAccelerometerUpdates()    motionKit.stopGyroUpdates()    motionKit.stopDeviceMotionUpdates()    motionKit.stopmagnetometerUpdates()

##DelegatesIn case if you dont want to use the trailing closures, we've got you covered. MotionKit supports the following Delegate methods to retrieve the sensor values.

optionalfunc retrieveAccelerometerValues(x:Double, y:Double, z:Double, absoluteValue:Double)    optionalfunc retrieveGyroscopeValues(x:Double, y:Double, z:Double, absoluteValue:Double)    optionalfunc retrieveDeviceMotionObject(deviceMotion:CMDeviceMotion)    optionalfunc retrieveMagnetometerValues(x:Double, y:Double, z:Double, absoluteValue:Double)    optionalfunc getAccelerationValFromDeviceMotion(x:Double, y:Double, z:Double)    optionalfunc getGravityAccelerationValFromDeviceMotion(x:Double, y:Double, z:Double)    optionalfunc getRotationRateFromDeviceMotion(x:Double, y:Double, z:Double)    optionalfunc getMagneticFieldFromDeviceMotion(x:Double, y:Double, z:Double)    optionalfunc getAttitudeFromDeviceMotion(attitude:CMAttitude)

To use the above delegate methods, you have to add the MotionKit delegate to your ViewController.

classViewController:UIViewController,MotionKitDelegate{...}

And in the ViewDidLoad method, you simply have to add this.

overridefunc viewDidLoad(){        super.viewDidLoad()        motionKit.delegate=self......}

Having that done, you'd probably want to implement a delegate method like this.

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......}

##Getting just a single value at an instantif you want to get just a single value of any of the available sensors at a given time, you could probably use some of the our handy methods provided in MotionKit.

###Accelerometer

Swift
```swift motionKit.getAccelerationAtCurrentInstant { (x, y, z) -> () in .... }```
Objective-C
```objective-c [motionKit getAccelerationAtCurrentInstant:1.0 values:^(double x, double y, double z) { // your values here }];```

###Gravitational Acceleration

Swift
```swift motionKit.getAccelerationAtCurrentInstant { (x, y, z) -> () in .... }```
Objective-C
```objective-c [motionKit getAccelerationAtCurrentInstant:1.0 values:^(double x, double y, double z) { // your values here }];```

###Attitude

Swift
```swift motionKit.getAttitudeAtCurrentInstant { (x, y, z) -> () in .... }```
Objective-C
```objective-c [motionKit getAttitudeAtCurrentInstant:1.0 values:^(double x, double y, double z) { // your values here }];```

###Magnetic Field

Swift
```swift motionKit.getMageticFieldAtCurrentInstant { (x, y, z) -> () in .... }```
Objective-C
```objective-c [motionKit getMageticFieldAtCurrentInstant:1.0 values:^(double x, double y, double z) { // your values here }];```

###Gyroscope Values

Swift
```swift motionKit.getGyroValuesAtCurrentInstant { (x, y, z) -> () in .... }```
Objective-C
```objective-c [motionKit getGyroValuesAtCurrentInstant:1.0 values:^(double x, double y, double z) { // your values here }];```

#Discussion

#Requirements

  • iOS 7.0+
  • Xcode 6.1

#TODO

  • Add More Methods
  • Adding Background Functionality

#LicenseCreative Commons License
This work is licensed under aCreative Commons Attribution-NonCommercial 4.0 International License.

About

Get the data from Accelerometer, Gyroscope and Magnetometer in only Two or a few lines of code. CoreMotion now made insanely simple:octocat: 📡

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp