-1

I have been playing with CoreMotion for a while right now but I'm facing a problem in detecting device's movements. What I wanted to achieve is detecting my iPhone's movements when on flat surface. I want to know if it's moving to the right or to the left that's all. Imagine a mouse and it's cursor. I'm trying to achieve the same thing, I just want to know if the device is moving to the right or to the left that's all.

I've tried to get Accelerometer's data on X-Axis, but that was not helpful, theoretically, if the value is positive it should be moving to the right & if negative it should be moving into the left. But that was not the case at all.

if let accData = self.motionManager.accelerometerData {
    if accData.acceleration.x > 0 {
        print("Right...")
    }else if accData.acceleration.x < 0 {
        print("Left.....")
    }
}
  • StackOverflow is not a code-writing service. We are always glad to help and support new coders but you need to help yourself first. You are expected to try to write the code yourself. Please read How to create a [Minimal, Complete, and Verifiable example.](https://stackoverflow.com/help/mcve) and [Jon Skeet's SO Checklist](https://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist) – Sanguinary Oct 08 '18 at 11:29
  • 3
    Possible duplicate of [Accelerometer detecting side to side movements](https://stackoverflow.com/questions/2076675/accelerometer-detecting-side-to-side-movements) – Sanguinary Oct 08 '18 at 11:31
  • Thanks @Sanguinary for your reply but that thread is also explaining the theory which is not the same when trying to use it in iOS. – Feras Allaou Oct 08 '18 at 12:36
  • [Detect acc. vertical axis](https://stackoverflow.com/questions/47073389/detect-acceleration-in-absolute-vertical-axis?rq=1) - You might get some useful information from this thread. – Sanguinary Oct 08 '18 at 12:41
  • Thanks again for your suggestion @Sanguinary. However, that code also doesn't work. I've tried it and tried to play a little bit with it. Still nothing. – Feras Allaou Oct 08 '18 at 15:00

1 Answers1

0

After a lot of research it seems that Magnetic field is the best option here is a link which could be useful CMDeviceMotion returns 0 values for magnetic field