-2


I want to know how to detect if an iPhone has moved in the last 60 sec using swift

I came across this code below, but only works when phone is shaken.
Is it possible to detect when phone is moved but not vigorously shaken?

   override func motionEnded(motion: UIEventSubtype,
                          withEvent event: UIEvent?) {
        if motion == .MotionShake {
            let controller = UIAlertController(title: "Shake",
                                           message: "The device is shaken",
                                           preferredStyle: .Alert)

            controller.addAction(UIAlertAction(title: "OK",
                style: .Default,
                handler: nil))

            presentViewController(controller, animated: true, completion: nil)

        }

}
Duncan C
  • 128,072
  • 22
  • 173
  • 272
borna
  • 906
  • 3
  • 12
  • 32
  • Can you please describe more completely what counts as "been moved"? What is the minimum amount of motion that should trigger having been moved? – nhgrif Apr 30 '16 at 14:51
  • You can get access to the accelerometer or gyroscope etc. https://developer.apple.com/library/ios/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/motion_event_basics/motion_event_basics.html – Gruntcakes Apr 30 '16 at 14:54
  • by moved, I mean is not on the desk, table or etc. – borna Apr 30 '16 at 15:18
  • Err, there's no such method in the iPhone SDK called isPhoneOnDesk() or isPhoneOnTable() or wasPhoneOnFloorButNowItsOntheBed(). Your comment, rather than clarifying things, just made things even more unclear as to what it is you are asking for. – Gruntcakes Apr 30 '16 at 16:02
  • Maybe is unclear to you? let say the phone is on your head and you are sleep and not moving. Suddenly you woke up and you moved your head and the phone falls down, hit your nose and is on the floor now. That is considered as the phone moved. Or your mother comes into the room and see you're sleep with a phone on your head. She grabs the phone and gently place it on the table. that is also consider as moved. The sample code I put works when the phone is shaken vigorously. How can I modify that code so that it can detect smallest move? – borna Apr 30 '16 at 21:58

1 Answers1

1

I think you're going to need to use the accelerometer and apply a moderate low pass filter to the output so you ignore very gradual change, but pick up moderate acceleration. You should be able to tune the threshold.

I suggest searching on "ios accellerometer low pass filter". You're going to need to write your own' code.

Duncan C
  • 128,072
  • 22
  • 173
  • 272