How to find out when the device is held upright horizontally and vertically using UIAccelerometer
How to find when the ipod is held straight up in the a开发者_开发技巧ir - both horizontally and vertically.
Can any one help me ?
Thanks in advance.....
You would read the accelerometer values. A value of approx 1 on one axis, and approx 0 on all others would indicate that the devices was stationary and in a vertical (or horizontal) orientation : the 1 indicates 1 G acting through the plane of the device. Of course this would only work on Earth :-) I guess checking for 0 on all but one axis would eliminate that bug!
If I understand your question, maybe with the UIDevice orientation you can know it easily.
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(orientationObserver) name: UIDeviceOrientationDidChangeNotification object: nil];
On the selector take the orientation with
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
This way you avoid check the acceleration and know the orientation of the device.
UIDeviceOrientationPortrait,
UIDeviceOrientationPortraitUpsideDown,
UIDeviceOrientationLandscapeLeft,
UIDeviceOrientationLandscapeRight,
UIDeviceOrientationFaceUp,
UIDeviceOrientationFaceDown
精彩评论