Warm tip: This article is reproduced from stackoverflow.com, please click
cocoa-touch ios iphone swift core-motion

Calculate a short vertical distance traveled by iPhone

发布于 2020-04-15 09:49:08

Currently I'm working on an app which needs to detect if the iPhone starts and stops moving vertically. I need to be able to detect a pretty short (50-100 cm) vertical distance traveled, i.e. if a person performs a squat.

Is there a way to calculate that the Core Motion framework?

let motion = CMMotionManager()

if motion.isDeviceMotionAvailable {
    self.motion.deviceMotionUpdateInterval = 1.0 / 60.0
    self.motion.showsDeviceMovementDisplay = true

    self.motion.startDeviceMotionUpdates(using: .xMagneticNorthZVertical, to: .main, withHandler: { (data, error) in
        if let validData = data {
            // Just a random minimum acceleration threshold                    
            if validData.userAcceleration.y > 3 {
               print(validData.userAcceleration.y)
            }
         }
     })
}
Questioner
Michael Samoylov
Viewed
26
matt 2020-01-26 20:53

Is there a way to calculate vertical distance traveled with the Core Motion framework?

Core Motion can detect attitude (how the phone is oriented) and acceleration (how the phone starts or stops moving, speeds up or slows down). A smooth vertical movement would not register at all. You might know that the movement started and ended, but not how far apart those events occurred. You might guess something about that based on the initial acceleration (which could let you calculate how fast we got going) and the time before deceleration. But it wouldn’t be very much more than a guess.