Search code examples
iphoneipadmovesensorsparallax

What sensor to use for creating a parallax effect on iPhone?


I have a scene divided in 3 different planes and I want to move these planes, left or right relative to the rotation of the device on the axis, so when the device is at 0 degree to the surface the planes will be on center. I see this effect on the home screen of a game from my iPod touch (first generation). Which sensor I must work with for creating a similar effect?


Solution

  • To achieve a parallax effect, add the CoreMotion framework to your project and construct a CMMotionManager. Then for a device that has a gyroscope, you can use startDeviceMotionUpdatesToQueue:withHandler: and inspect motion.attitude.roll in your handler block.

    For a device that doesn't have a gyroscope, you can use startAccelerometerUpdatesToQueue:withHandler: and inspect accelerometerData.acceleration.x, or you can use UIAccelerometer and implement UIAccelerometerDelegate. Either way, you'll probably want to create a low-pass filter to help distinguish gravity from linear acceleration. The GLGravity project has an example of this.

    See the section on Motion Events in the Event Handling Guide for iOS.