Giter Site home page Giter Site logo

maximbilan / ios-coremotion-example Goto Github PK

View Code? Open in Web Editor NEW
105.0 9.0 20.0 105 KB

Core Motion in iOS using Swift

License: MIT License

Swift 100.00%
ios motion accelerometer device-motion motion-framework swift gyroscope accelerometer-data coremotion motionevent

ios-coremotion-example's Introduction

Core Motion in iOS using Swift

Users generate motion events when they move, shake, or tilt the device. These motion events are detected by the device hardware, specifically, the accelerometer and the gyroscope. The Core Motion framework lets your application receive motion data from the device hardware and process that data.

alt tag

To get started import the Core Motion framework like this:

import CoreMotion

Now create an instance of CMMotionManager object. The app can use it to receive four types of motion: raw accelerometer data, raw gyroscope data, raw magnetometer data, and processed device-motion data (which includes accelerometer, rotation-rate, and attitude measurements).

let motionManager = CMMotionManager()

For starting accelerometer updates you need to call the next method:

func startAccelerometerUpdates()

Finally, read the accelerometer data as often as you want.

if let accelerometerData = motionManager.accelerometerData {
}

You can change accelerometerUpdateInterval, the interval, in seconds, for providing accelerometer updates to the block handler.

The same story for Gyroscope, Magnetometer and Device motion.

func startGyroUpdates()
func startMagnetometerUpdates()
func startDeviceMotionUpdates()

Happy coding!

ios-coremotion-example's People

Contributors

maximbilan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ios-coremotion-example's Issues

UI API called on a background thread

The debugger logs the following warning.

System: XCode 10,
Device: XS Max with iOS 12

=================================================================
Main Thread Checker: UI API called on a background thread: -[UIApplication applicationState]
PID: 2020, TID: 456048, Thread name: com.apple.CoreMotion.MotionThread, Queue name: com.apple.root.default-qos.overcommit, QoS: 0
Backtrace:
4 libobjc.A.dylib 0x000000019c263894 + 56
5 CoreMotion 0x00000001a2ac47a4 CoreMotion + 305060
6 CoreMotion 0x00000001a2ac4cd8 CoreMotion + 306392
7 CoreMotion 0x00000001a2ac4be8 CoreMotion + 306152
8 CoreMotion 0x00000001a2af63cc CoreMotion + 508876
9 CoreMotion 0x00000001a2af642c CoreMotion + 508972
10 CoreFoundation 0x000000019cffc888 + 28
11 CoreFoundation 0x000000019cffc16c + 276
12 CoreFoundation 0x000000019cff7470 + 2324
13 CoreFoundation 0x000000019cff6844 CFRunLoopRunSpecific + 452
14 CoreFoundation 0x000000019cff75a8 CFRunLoopRun + 84
15 CoreMotion 0x00000001a2af5d64 CoreMotion + 507236
16 libsystem_pthread.dylib 0x000000019cc71a04 + 132
17 libsystem_pthread.dylib 0x000000019cc71960 _pthread_start + 52
18 libsystem_pthread.dylib 0x000000019cc79df4 thread_start + 4
2018-09-22 22:21:21.764356+0200 CoreMotionExample[2020:456048] [reports] Main Thread Checker: UI API called on a background thread: -[UIApplication applicationState]
PID: 2020, TID: 456048, Thread name: com.apple.CoreMotion.MotionThread, Queue name: com.apple.root.default-qos.overcommit, QoS: 0
Backtrace:
4 libobjc.A.dylib 0x000000019c263894 + 56
5 CoreMotion 0x00000001a2ac47a4 CoreMotion + 305060
6 CoreMotion 0x00000001a2ac4cd8 CoreMotion + 306392
7 CoreMotion 0x00000001a2ac4be8 CoreMotion + 306152
8 CoreMotion 0x00000001a2af63cc CoreMotion + 508876
9 CoreMotion 0x00000001a2af642c CoreMotion + 508972
10 CoreFoundation 0x000000019cffc888 + 28
11 CoreFoundation 0x000000019cffc16c + 276
12 CoreFoundation 0x000000019cff7470 + 2324
13 CoreFoundation 0x000000019cff6844 CFRunLoopRunSpecific + 452
14 CoreFoundation 0x000000019cff75a8 CFRunLoopRun + 84
15 CoreMotion 0x00000001a2af5d64 CoreMotion + 507236
16 libsystem_pthread.dylib 0x000000019cc71a04 + 132
17 libsystem_pthread.dylib 0x000000019cc71960 _pthread_start + 52
18 libsystem_pthread.dylib 0x000000019cc79df4 thread_start + 4
x -0.942871 y 0.002350 z -0.331863 @ 74169.691657
x -0.367126 y -0.548035 z 0.735352 @ 74169.706594
x -85.905334 y 34.749908 z -41.169586 @ 74169.674090

Motion permission?

Do you know is it needed to ask permission before using altimeter or accelerometer data?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.