8

How do I change or disable the rotating animation when screen orientation changes from landscape to portrait, or vice versa?

Nils Munch
  • 8,805
  • 11
  • 51
  • 103
Aldrich Co
  • 587
  • 6
  • 21

3 Answers3

16

Yes, it is possible to disable the animation, without breaking everything apart.

The following codes will disable the "black box" rotation animation, without messing with other animations or orientation code:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    [UIView setAnimationsEnabled:YES];
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    [UIView setAnimationsEnabled:NO];
  /* Your original orientation booleans*/
}

Place it in your UIViewController and all should be well. Same method can be applied to any undesired animation in iOS.

Best of luck with your project.

Nils Munch
  • 8,805
  • 11
  • 51
  • 103
  • 8
    For iOS 6+ (and previous) compatibility, you can put [UIView setAnimationsEnabled:NO] in willRotateToInterfaceOrientation:duration: instead of shouldAutorotateToInterfaceOrientation: – Jeff Hay Oct 25 '12 at 22:11
  • 5
    Remember to call the super implementation if you override these. Specifically, the apple docs say that if you are overriding willRotateToInterfaceOrientation:duration: or didRotateFromInterfaceOrientation: that "Your implementation of this method must call super at some point during its execution." – levigroker Aug 22 '13 at 22:21
8

If you dont want your view controllers to rotate just override the shouldAutoRotateToInterface view controller method to return false for whichever orientation you dont want to support...Here is a reference.

In the case that u just want to handle rotation some other way, you can return false in the above methods and register for UIDeviceOrientationDidChangeNotification like so

    NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
       selector:@selector(handleOrientationDidChange:)
           name:UIDeviceOrientationDidChangeNotification
         object:nil];

Now when u get the notifications u can do whatever you want with it...

Daniel
  • 22,363
  • 9
  • 64
  • 71
1

The answer by @Nils Munch above is find for < iOS7. For iOS 7 or later you can use:

- (void) viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
    [UIView setAnimationsEnabled:NO];

    [coordinator notifyWhenInteractionEndsUsingBlock:^(id<UIViewControllerTransitionCoordinatorContext> context) {
        [UIView setAnimationsEnabled:YES];
    }];

    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}
PKCLsoft
  • 1,359
  • 2
  • 26
  • 35
  • Just retested in the debugger, running iOS 8.4 and it's definitely being called when I rotate the device. For < iOS7 it won't call this though. – PKCLsoft Dec 02 '15 at 08:38