Search code examples
iosxcodeorientationuiinterfaceorientation

user enable/disable rotation


I want to allow the interface to change only if the flag enableRotation=1. I’ve been messing around with all the rotation methods and am getting nowhere.

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
enableRotation=0;
currentOrientation=fromInterfaceOrientation;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{



NSLog(@"enable rotation call %i\n",enableRotation);
if(enableRotation)
{  
if ((interfaceOrientation== UIInterfaceOrientationPortrait) ||
    (interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
    ||(interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)||(interfaceOrientation == UIInterfaceOrientationLandscapeRight)
    )
{

    return YES;
}

        }
else if (interfaceOrientation==currentOrientation){


    return YES;
}
return NO;
}

This is what I’m using to display the alternate view....

 - (void)orientationChanged:(NSNotification *)notification
{


if (UIDeviceOrientationIsPortrait(deviceOrientation))
{
    [self performSegueWithIdentifier:@"DisplayAlternateView" sender:self];
    isShowingLandscapeView = NO;
  //  enableRotation=0;
    NSLog(@"Rotation disabled\n");

}
else if (UIDeviceOrientationIsLandscape(deviceOrientation) &&
         !isShowingLandscapeView)
{

    [self dismissViewControllerAnimated:YES completion:nil];
    isShowingLandscapeView = YES;
 //   enableRotation=0;
    NSLog(@"Rotation disabled Change to Landscape\n");

}

}


Solution

  • You should get rid of the !isShowingLandscapeView condition. That could cause an issue because you may receive that notification multiple times. Actually get rid of the boolean altogether. You don't need it. You can always get the orientation with [UIDevice currentDevice].orientation or [UIApplication sharedApplication].statusBarOrientation

    In fact, you do not need to register for this notification period. You could just use -(void)willRotateToInterfaceOrientation: (UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration to change the view appropriately.

    Furthermore, you should make this change as well:

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        if(enableRotation)
            return YES;
        else
            return NO;
    }