I have a game in which the orientation of the device affects the state of the game. The user must quickly switch between Landscape, Portrait, and Reverse Landscape orientations. So far I've been registering the game for orientation notifications via:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
But it is far too slow - there seems to be about a second delay between rotating the phone and the notification actually being fired. I need a way to INSTANTLY detect changes in the device's orientation. I have tried experimenting with the gyroscope, but am not yet familiar enough with it to know whether or not it is the solution I am looking for.
Add a notifier in the
viewWillAppear
functionThe orientation change notifies this function
which in-turn calls this function where the moviePlayerController frame is orientation is handled
in
viewDidDisappear
remove the notificationI guess this is the fastest u can have changed the view as per orientation
@vimal answer did not provide solution for me. It seems the orientation is not the current orientation, but from previous orientation. To fix it, I use
[[UIDevice currentDevice] orientation]
Then
With this code I get the current orientation position.
Why you didn`t use
?
Or you can use this
Or this
Hope it owl be useful )
Try making your changes in:
- (void) viewWillLayoutSubviews {}
The code will run at every orientation change as the subviews get laid out again.
That delay you're talking about is actually a filter to prevent false (unwanted) orientation change notifications.
For instant recognition of device orientation change you're just gonna have to monitor the accelerometer yourself.
Accelerometer measures acceleration (gravity included) in all 3 axes so you shouldn't have any problems in figuring out the actual orientation.
Some code to start working with accelerometer can be found here:
How to make an iPhone App – Part 5: The Accelerometer
And this nice blog covers the math part:
Using the Accelerometer
For my case handling
UIDeviceOrientationDidChangeNotification
was not good solution as it is called more frequent andUIDeviceOrientation
is not always equal toUIInterfaceOrientation
because of (FaceDown, FaceUp).I handle it using
UIApplicationDidChangeStatusBarOrientationNotification
: