简体   繁体   中英

iphone autorotation animation

Is it possible to turn off the animation for the autorotation? I want it to rotate, but I just dont want the animation to occur (like an instant switch).

If you really need to, just use setAnimationsEnabled of UIView :

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [UIView setAnimationsEnabled:NO]; // disable animations temporarily
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    [UIView setAnimationsEnabled:YES]; // rotation finished, re-enable them
}

It does the trick for me. Of course it's not recommended to do this unless you have a very good reason to do it.

Just add the following code to overwrite the standard rotation animation:

- (void)viewDidLoad
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:)   name:UIDeviceOrientationDidChangeNotification object:nil];
}

- (void)didRotate:(NSNotification *)notification {
    orientation = [[UIDevice currentDevice] orientation];
    if(orientation == UIDeviceOrientationUnknown || orientation == UIDeviceOrientationFaceDown || orientation == UIDeviceOrientationFaceUp){
        orientation = UIDeviceOrientationPortrait;
    }
    [[UIApplication sharedApplication] setStatusBarOrientation:orientation animated:NO];
    [self positionScreenElements];
}

@Nils Munch unfortunately this does not work properly, the setStatusBarOrientation expects UIInterfaceOrientation not UIDeviceOrientation and casting is bad practice. Correct me if I am wrong

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM