Detecting iOS orientation change instantly Detecting iOS orientation change instantly ios ios

Detecting iOS orientation change instantly


Add a notifier in the viewWillAppear function

-(void)viewWillAppear:(BOOL)animated{  [super viewWillAppear:animated];  [[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(orientationChanged:)    name:UIDeviceOrientationDidChangeNotification  object:nil];}

The orientation change notifies this function

- (void)orientationChanged:(NSNotification *)notification{   [self adjustViewsForOrientation:[[UIApplication sharedApplication] statusBarOrientation]];}

which in-turn calls this function where the moviePlayerController frame is orientation is handled

- (void) adjustViewsForOrientation:(UIInterfaceOrientation) orientation {    switch (orientation)    {        case UIInterfaceOrientationPortrait:        case UIInterfaceOrientationPortraitUpsideDown:        {         //load the portrait view            }            break;        case UIInterfaceOrientationLandscapeLeft:        case UIInterfaceOrientationLandscapeRight:        {        //load the landscape view         }            break;        case UIInterfaceOrientationUnknown:break;    }}

in viewDidDisappear remove the notification

-(void)viewDidDisappear:(BOOL)animated{   [super viewDidDisappear:animated];   [[NSNotificationCenter defaultCenter]removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];}

I guess this is the fastest u can have changed the view as per orientation


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


Why you didn`t use

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

?

Or you can use this

-(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

Or this

-(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation

Hope it owl be useful )