{"id":1247,"url":"https://github.com/MHaroonBaig/MotionKit","last_synced_at":"2025-07-30T20:33:01.358Z","repository":{"id":27324752,"uuid":"30799212","full_name":"MHaroonBaig/MotionKit","owner":"MHaroonBaig","description":"Get the data from Accelerometer, Gyroscope and Magnetometer in only Two or a few lines of code. CoreMotion now made insanely simple  :octocat:   :satellite:    ","archived":false,"fork":false,"pushed_at":"2018-05-07T03:00:53.000Z","size":344,"stargazers_count":1081,"open_issues_count":18,"forks_count":135,"subscribers_count":31,"default_branch":"master","last_synced_at":"2024-11-14T15:22:33.371Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://goo.gl/bpXBlO","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MHaroonBaig.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-02-14T14:50:50.000Z","updated_at":"2024-08-07T09:45:51.000Z","dependencies_parsed_at":"2022-09-07T20:30:37.673Z","dependency_job_id":null,"html_url":"https://github.com/MHaroonBaig/MotionKit","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MHaroonBaig%2FMotionKit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MHaroonBaig%2FMotionKit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MHaroonBaig%2FMotionKit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MHaroonBaig%2FMotionKit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MHaroonBaig","download_url":"https://codeload.github.com/MHaroonBaig/MotionKit/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228187608,"owners_count":17882335,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-01-05T20:15:42.146Z","updated_at":"2024-12-04T20:31:10.042Z","avatar_url":"https://github.com/MHaroonBaig.png","language":"Swift","funding_links":[],"categories":["Hardware","Libs","Swift","Layout"],"sub_categories":["Other Hardware","Hardware","Other free courses","Sensors"],"readme":"#[MotionKit] (http://goo.gl/bpXBlO) — The missing iOS wrapper :notes:\n\n[![Language](http://img.shields.io/badge/language-swift-brightgreen.svg?style=flat\n)](https://developer.apple.com/swift)\n[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)\n[![Version](https://img.shields.io/cocoapods/v/MotionKit.svg?style=flat)](http://cocoadocs.org/docsets/MotionKit)\n[![License](http://img.shields.io/badge/license-MIT-lightgrey.svg?style=flat\n)](http://mit-license.org)\n[![Issues](https://img.shields.io/github/issues/MHaroonBaig/MotionKit.svg?style=flat\n)](https://github.com/MHaroonBaig/MotionKit/issues?state=open)\n[![Join the chat at https://gitter.im/MHaroonBaig/MotionKit](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/MHaroonBaig/MotionKit?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge\u0026utm_content=badge)\n\nNow 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.**\n\nA nice and clean wrapper around the **CoreMotion Framework** written entirely in Swift. The Core Motion framework lets your application receive motion data from device hardware and process that data.\nThe data can be retrieved from **Accelerometer**, **Gyroscope** and **Magnetometer**.\nYou can also get the **refined and processed gyroscope and accelerometer data** from the `deviceMotion` datatype itself instead of getting the raw values.\n\n**Articles accompanying MotionKit:** [Link1](https://medium.com/@PyBaig/distribute-your-swift-code-libraries-and-frameworks-using-cocoapods-b41c62cd7c94) —  [Link2](https://medium.com/@PyBaig/build-your-own-cocoa-touch-frameworks-in-swift-d4ea3d1f9ca3) —  [Link3](https://medium.com/@PyBaig/motionkit-the-missing-ios-coremotion-wrapper-written-in-swift-99fcb83355d0)\n#How does it work\nYou can retrieve all the values either by a trailing closure or by a delegate method. Both the approaches are fully supported.\n\n*__Note:__* All the provided methods are Asynchronous and operate in their own Queue so that your app could run smoothly and efficiently.\n\n##Initialise\nFirst, initialise the MotionKit instance. Its a Must.\n\n\u003cdiv align=\"right\" style=\"color:#FE834C\"\u003e\n\u003ch7\u003e\u003ci\u003eSwift\u003c/i\u003e\u003c/h7\u003e\n\u003c/div\u003e\n```swift\n    let motionKit = MotionKit()\n```\n\u003cdiv align=\"right\"\u003e\n\u003ch7\u003e\u003ci\u003eObjective-C\u003c/i\u003e\u003c/h7\u003e\n\u003c/div\u003e\n```objective-c\n    MotionKit *motionKit = [[NSClassFromString(@\"MotionKit\") alloc] init];\n```\n\n##Getting Accelerometer Values\nYou can get the accelerometer values using just a few lines of code.\n\n\u003cdiv align=\"right\"\u003e\n\u003ch7\u003e\u003ci\u003eSwift\u003c/i\u003e\u003c/h7\u003e\n\u003c/div\u003e\n```swift\n    motionKit.getAccelerometerValues(interval: 1.0){\n        (x, y, z) in\n        //Interval is in seconds. And now you have got the x, y and z values here\n        ....\n      }\n```\n\n\u003cdiv align=\"right\"\u003e\n\u003ch7\u003e\u003ci\u003eObjective-C\u003c/i\u003e\u003c/h7\u003e\n\u003c/div\u003e\n```objective-c\n    [motionKit getAccelerometerValuesWithInterval:1.0 values:^(double x, double y, double z) {\n        // your values here\n    }];\n```\n\n##Getting Gyroscope Values\nGyroscope values could be retrieved by the following few lines of code.\n\n\u003cdiv align=\"right\"\u003e\n\u003ch7\u003e\u003ci\u003eSwift\u003c/i\u003e\u003c/h7\u003e\n\u003c/div\u003e\n```swift\n    motionKit.getGyroValues(interval: 1.0){\n        (x, y, z) in\n        //Your processing will go here\n        ....\n      }\n```\n\n\u003cdiv align=\"right\"\u003e\n\u003ch7\u003e\u003ci\u003eObjective-C\u003c/i\u003e\u003c/h7\u003e\n\u003c/div\u003e\n```objective-c\n    [motionKit getGyroValues:1.0 values:^(double x, double y, double z) {\n        // your values here\n    }];\n```\n\n##Getting Magnetometer Values\nGetting Magnetometer values is as easy as grabbing a cookie.\n\n\u003cdiv align=\"right\"\u003e\n\u003ch7\u003e\u003ci\u003eSwift\u003c/i\u003e\u003c/h7\u003e\n\u003c/div\u003e\n```swift\n    motionKit.getMagnetometerValues(interval: 1.0){\n        (x, y, z) in\n        //Do something with the retrieved values\n        ....\n      }\n```\n\n\u003cdiv align=\"right\"\u003e\n\u003ch7\u003e\u003ci\u003eObjective-C\u003c/i\u003e\u003c/h7\u003e\n\u003c/div\u003e\n```objective-c\n    [motionKit getMagnetometerValues:1.0 values:^(double x, double y, double z) {\n        // your values here\n    }];\n```\n\n#Installation\nEmbedded frameworks require a minimum deployment target of iOS 8.\n###— Using CocoaPods\nJust add this to your Podfile.\n```ruby\npod 'MotionKit'\n```\nNote 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.\n```bash\n[sudo] gem install cocoapods --pre\n```\n\n\n###— Using Carthage\nYou can use [Carthage](https://github.com/Carthage/Carthage) to install `MotionKit` by adding the following line to your `Cartfile`.\n```ruby\n  github \"MHaroonBaig/MotionKit\"\n```\n\n###— Manual Installation\nJust copy the `MotionKit.swift` file into your Xcode project folder, and you're ready to go.\n\n#CMDeviceMotion - as easy as pie\nIn 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.\n\nThe deviceMotion object includes:\n- Acceleration Data\n  - userAcceleration\n  - gravity\n- Calibrated Magnetic Field\n  - magneticField\n- Attitude and Rotation Rate\n  - attitude\n  - rotationRate\n\nAll of the values can be retrieved either by individual methods or by getting the deviceMotion object itself.\n\n###Getting the whole CMDeviceMotion Object\n\n\u003cdiv align=\"right\"\u003e\n\u003ch7\u003e\u003ci\u003eSwift\u003c/i\u003e\u003c/h7\u003e\n\u003c/div\u003e\n```swift\n\n    motionKit.getDeviceMotionObject(interval: 1.0){\n        (deviceMotion) -\u003e () in\n          var accelerationX = deviceMotion.userAcceleration.x\n          var gravityX = deviceMotion.gravity.x\n          var rotationX = deviceMotion.rotationRate.x\n          var magneticFieldX = deviceMotion.magneticField.x\n          var attitideYaw = deviceMotion.attitude.yaw\n          ....\n        }\n\n```\n\n\u003cdiv align=\"right\"\u003e\n\u003ch7\u003e\u003ci\u003eObjective-C\u003c/i\u003e\u003c/h7\u003e\n\u003c/div\u003e\n```objective-c\n[motionKit getDeviceMotionObjectWithInterval:1.0 values:^(CMDeviceMotion *deviceMotion) {\n    // Your values here\n}];\n\n```\n\n###Getting refined values of Acceleration\n\nYou 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.\n\n\u003cdiv align=\"right\"\u003e\n\u003ch7\u003e\u003ci\u003eSwift\u003c/i\u003e\u003c/h7\u003e\n\u003c/div\u003e\n\n```swift\n    motionKit.getAccelerationFromDeviceMotion(interval: 1.0){\n        (x, y, z) -\u003e () in\n          // Grab the x, y and z values\n          ....\n        }\n```\n\n\u003cdiv align=\"right\"\u003e\n\u003ch7\u003e\u003ci\u003eObjective-C\u003c/i\u003e\u003c/h7\u003e\n\u003c/div\u003e\n```objective-c\n    [motionKit getAccelerationFromDeviceMotion:1.0 values:^(double x, double y, double z) {\n        // your values here\n    }];\n```\n\n###Getting Gravitational Acceleration\nAgain, you can access it through the Device Motion service as well.\n\n\u003cdiv align=\"right\"\u003e\n\u003ch7\u003e\u003ci\u003eSwift\u003c/i\u003e\u003c/h7\u003e\n\u003c/div\u003e\n```swift\n      motionKit.getGravityAccelerationFromDeviceMotion(interval: 1.0) {\n          (x, y, z) -\u003e () in\n          // x, y and z values are here\n          ....\n      }\n```\n\n\u003cdiv align=\"right\"\u003e\n\u003ch7\u003e\u003ci\u003eObjective-C\u003c/i\u003e\u003c/h7\u003e\n\u003c/div\u003e\n```objective-c\n    [motionKit getGravityAccelerationFromDeviceMotion:1.0 values:^(double x, double y, double z) {\n        // your values here\n    }];\n```\n\n###Getting Magnetic Field around your device\nInteresting, Get it in a magical way.\n\n\u003cdiv align=\"right\"\u003e\n\u003ch7\u003e\u003ci\u003eSwift\u003c/i\u003e\u003c/h7\u003e\n\u003c/div\u003e\n```swift\n      motionKit.getMagneticFieldFromDeviceMotion(interval: 1.0) {\n        (x, y, z, accuracy) -\u003e () in\n        // Get the values with accuracy\n        ....\n        }\n\n```\n\n\u003cdiv align=\"right\"\u003e\n\u003ch7\u003e\u003ci\u003eObjective-C\u003c/i\u003e\u003c/h7\u003e\n\u003c/div\u003e\n```objective-c\n    [motionKit getMagneticFieldFromDeviceMotion:1.0 values:^(double x, double y, double z) {\n        // your values here\n    }];\n```\n\n###Getting the Attitude metrics\n\n\u003cdiv align=\"right\"\u003e\n\u003ch7\u003e\u003ci\u003eSwift\u003c/i\u003e\u003c/h7\u003e\n\u003c/div\u003e\n```swift\n      motionKit.getAttitudeFromDeviceMotion(interval: 1.0) {\n        (attitude) -\u003e () in\n          var roll = attitude.roll\n          var pitch = attitude.pitch\n          var yaw = attitude.yaw\n          var rotationMatrix = attitude.rotationMatrix\n          var quaternion = attitude.quaternion\n          ....\n        }\n```\n\n\u003cdiv align=\"right\"\u003e\n\u003ch7\u003e\u003ci\u003eObjective-C\u003c/i\u003e\u003c/h7\u003e\n\u003c/div\u003e\n```objective-c\n    [motionKit getAttitudeFromDeviceMotionWithInterval:1.0 values:^(CMAttitude *attitude) {\n        // Your values here\n   }];\n```\n\n###Getting Rotation Rate of your device\n\n\u003cdiv align=\"right\"\u003e\n\u003ch7\u003e\u003ci\u003eSwift\u003c/i\u003e\u003c/h7\u003e\n\u003c/div\u003e\n```swift\n      motionKit.getRotationRateFromDeviceMotion(interval: 1.0) {\n        (x, y, z) -\u003e () in\n        // There you go, grab the x, y and z values\n        ....\n        }\n\n```\n\n\u003cdiv align=\"right\"\u003e\n\u003ch7\u003e\u003ci\u003eObjective-C\u003c/i\u003e\u003c/h7\u003e\n\u003c/div\u003e\n```objective-c\n    [motionKit getRotationRateFromDeviceMotion:1.0 values:^(double x, double y, double z) {\n        // your values here\n    }];\n```\n\n\n\n\n##Precautions\nFor 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.\nYou can do this in MotionKit like this.\n```swift\n\n    //Make sure to call the required function when you're done\n    motionKit.stopAccelerometerUpdates()\n    motionKit.stopGyroUpdates()\n    motionKit.stopDeviceMotionUpdates()\n    motionKit.stopmagnetometerUpdates()\n\n```\n\n##Delegates\nIn 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.\n```swift\n    optional func retrieveAccelerometerValues (x: Double, y:Double, z:Double, absoluteValue: Double)\n    optional func retrieveGyroscopeValues     (x: Double, y:Double, z:Double, absoluteValue: Double)\n    optional func retrieveDeviceMotionObject  (deviceMotion: CMDeviceMotion)\n    optional func retrieveMagnetometerValues  (x: Double, y:Double, z:Double, absoluteValue: Double)\n\n\n    optional func getAccelerationValFromDeviceMotion        (x: Double, y:Double, z:Double)\n    optional func getGravityAccelerationValFromDeviceMotion (x: Double, y:Double, z:Double)\n    optional func getRotationRateFromDeviceMotion           (x: Double, y:Double, z:Double)\n    optional func getMagneticFieldFromDeviceMotion          (x: Double, y:Double, z:Double)\n    optional func getAttitudeFromDeviceMotion               (attitude: CMAttitude)\n\n```\nTo use the above delegate methods, you have to add the MotionKit delegate to your ViewController.\n```swift\n    class ViewController: UIViewController, MotionKitDelegate {\n      ...\n    }\n```\nAnd in the ViewDidLoad method, you simply have to add this.\n```swift\n    override func viewDidLoad() {\n        super.viewDidLoad()\n        motionKit.delegate = self\n        ......\n      }\n\n```\nHaving that done, you'd probably want to implement a delegate method like this.\n```swift\n    func retrieveAccelerometerValues (x: Double, y:Double, z:Double, absoluteValue: Double){\n      //Do whatever you want with the x, y and z values. The absolute value is calculated through vector mathematics\n      ......\n    }\n\n   func retrieveGyroscopeValues (x: Double, y:Double, z:Double, absoluteValue: Double){\n    //Do whatever you want with the x, y and z values. The absolute value is calculated through vector mathematics\n    ......\n   }\n\n```\n\n##Getting just a single value at an instant\nif 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.\n\n###Accelerometer\n\n\u003cdiv align=\"right\"\u003e\n\u003ch7\u003e\u003ci\u003eSwift\u003c/i\u003e\u003c/h7\u003e\n\u003c/div\u003e\n```swift\n    motionKit.getAccelerationAtCurrentInstant {\n        (x, y, z) -\u003e () in\n        ....\n      }\n```\n\n\u003cdiv align=\"right\"\u003e\n\u003ch7\u003e\u003ci\u003eObjective-C\u003c/i\u003e\u003c/h7\u003e\n\u003c/div\u003e\n```objective-c\n    [motionKit getAccelerationAtCurrentInstant:1.0 values:^(double x, double y, double z) {\n        // your values here\n    }];\n```\n\n###Gravitational Acceleration\n\n\u003cdiv align=\"right\"\u003e\n\u003ch7\u003e\u003ci\u003eSwift\u003c/i\u003e\u003c/h7\u003e\n\u003c/div\u003e\n```swift\n    motionKit.getAccelerationAtCurrentInstant {\n      (x, y, z) -\u003e () in\n      ....\n        }\n```\n\n\u003cdiv align=\"right\"\u003e\n\u003ch7\u003e\u003ci\u003eObjective-C\u003c/i\u003e\u003c/h7\u003e\n\u003c/div\u003e\n```objective-c\n    [motionKit getAccelerationAtCurrentInstant:1.0 values:^(double x, double y, double z) {\n        // your values here\n    }];\n```\n\n###Attitude\n\n\u003cdiv align=\"right\"\u003e\n\u003ch7\u003e\u003ci\u003eSwift\u003c/i\u003e\u003c/h7\u003e\n\u003c/div\u003e\n```swift\n    motionKit.getAttitudeAtCurrentInstant {\n      (x, y, z) -\u003e () in\n      ....\n        }\n```\n\n\u003cdiv align=\"right\"\u003e\n\u003ch7\u003e\u003ci\u003eObjective-C\u003c/i\u003e\u003c/h7\u003e\n\u003c/div\u003e\n```objective-c\n    [motionKit getAttitudeAtCurrentInstant:1.0 values:^(double x, double y, double z) {\n        // your values here\n    }];\n```\n\n###Magnetic Field\n\n\u003cdiv align=\"right\"\u003e\n\u003ch7\u003e\u003ci\u003eSwift\u003c/i\u003e\u003c/h7\u003e\n\u003c/div\u003e\n```swift\n    motionKit.getMageticFieldAtCurrentInstant {\n      (x, y, z) -\u003e () in\n      ....\n        }\n```\n\n\u003cdiv align=\"right\"\u003e\n\u003ch7\u003e\u003ci\u003eObjective-C\u003c/i\u003e\u003c/h7\u003e\n\u003c/div\u003e\n```objective-c\n    [motionKit getMageticFieldAtCurrentInstant:1.0 values:^(double x, double y, double z) {\n        // your values here\n    }];\n```\n\n###Gyroscope Values\n\n\u003cdiv align=\"right\"\u003e\n\u003ch7\u003e\u003ci\u003eSwift\u003c/i\u003e\u003c/h7\u003e\n\u003c/div\u003e\n```swift\n    motionKit.getGyroValuesAtCurrentInstant {\n      (x, y, z) -\u003e () in\n      ....\n        }\n```\n\n\u003cdiv align=\"right\"\u003e\n\u003ch7\u003e\u003ci\u003eObjective-C\u003c/i\u003e\u003c/h7\u003e\n\u003c/div\u003e\n```objective-c\n    [motionKit getGyroValuesAtCurrentInstant:1.0 values:^(double x, double y, double z) {\n        // your values here\n    }];\n```\n\n\n\n#Discussion\n- You can join our [Reddit] (https://www.reddit.com/r/MotionKit/) channel to discuss anything.\n\n- You can also open an issue here for any kind of feature set that you want. We would love to hear from you.\n\n- Don't forget to subscribe our Reddit channel, which is [/r/MotionKit] (https://www.reddit.com/r/MotionKit/)\n\n- Our StackOverflow tag is 'MotionKit'\n\n\n#Requirements\n* iOS 7.0+\n* Xcode 6.1\n\n#TODO\n- [ ] Add More Methods\n- [ ] Adding Background Functionality\n\n#License\n\u003ca rel=\"license\" href=\"http://creativecommons.org/licenses/by-nc/4.0/\"\u003e\u003cimg alt=\"Creative Commons License\" style=\"border-width:0\" src=\"https://i.creativecommons.org/l/by-nc/4.0/88x31.png\" /\u003e\u003c/a\u003e\u003cbr /\u003eThis work is licensed under a \u003ca rel=\"license\" href=\"http://creativecommons.org/licenses/by-nc/4.0/\"\u003eCreative Commons Attribution-NonCommercial 4.0 International License\u003c/a\u003e.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FMHaroonBaig%2FMotionKit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FMHaroonBaig%2FMotionKit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FMHaroonBaig%2FMotionKit/lists"}