Search code examples
iosxcodecore-motion

How to move view based on device motion in xcode


Based on device movement i need to set the frames to the view. I had tried using core motion, there I'm getting x, y, z values, but unable to get device moved distance based on those coordinates.

[self.manager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMDeviceMotion * _Nullable motion, NSError * _Nullable error) {

    xValue = motion.userAcceleration.x;
    yValue = motion.userAcceleration.y;
    zValue = motion.userAcceleration.z;

}];

Solution

  • What happens is you can try to calculate this distance but you'll have too much error, see this video.

    You didn't state what you're really trying to do, so if you trying to create the parallax effect you can use UIInterpolatingMotionEffect.