Sam Jarman
Sam Jarman

Reputation: 7377

Method triggered by Change in orientation

Is here a way to run a method when the iOS devices orientation changes?

I would like to change only some objects orientations on the screen, and not others.

What delegates do I use etc.

Cheers -A newbie

Upvotes: 12

Views: 18730

Answers (6)

Andrew
Andrew

Reputation: 3969

Just use this code snippet to check the orientation changes.

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
    if UIDevice.currentDevice().orientation.isLandscape.boolValue {
        print("Landscape")
    } else {
        print("Portrait")
    }
}

Upvotes: 0

Tim Kozak
Tim Kozak

Reputation: 4182

My sandbox app: https://github.com/comonitos/programatical_device_orientation

The solution is easy:

in interface (h file) :

BOOL rotated;

in implementation (m file):

  1. rewrite

    -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return rotated; }

2 call [self setup]

-(void) setup 
{
   rotated = YES;
   [[UIDevice currentDevice] setOrientation:UIDeviceOrientationLandscapeLeft];
   rotated = NO;
}

Upvotes: 0

drawnonward
drawnonward

Reputation: 53669

UIDevice posts UIDeviceOrientationDidChangeNotification

UIApplication posts UIApplicationWillChangeStatusBarOrientationNotification and UIApplicationDidChangeStatusBarOrientationNotification and has a related delegate callback for each.

UIViewController receives several orientation related calls triggered by the UIDevice notification if the view controller is part of the controller hierarchy managed by a window.

If you are already using a UIViewController, implement some of the orientation related methods, otherwise register for the UIDevice notifications. The most important UIViewController method is shouldAutorotateToInterfaceOrientation because if that return NO the others are not called.

Upvotes: 6

outis
outis

Reputation: 77400

UIViewControllers are sent willRotateToInterfaceOrientation:duration: just before rotation, and didRotateFromInterfaceOrientation: after rotation.

To configure additional animations, use either willAnimateRotationToInterfaceOrientation:duration: or willAnimateFirstHalfOfRotationToInterfaceOrientation:duration:, didAnimateFirstHalfOfRotationToInterfaceOrientation: and willAnimateSecondHalfOfRotationFromInterfaceOrientation:duration:. The latter are used for two-step animations, which you generally use when you have header or footer views that are moved offscreen for the main transition animation.

Upvotes: 0

Brian Liang
Brian Liang

Reputation: 7774

Depends when you want to react:

If before rotation, override from UIViewController:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
 // do something before rotation
}

If you want to perform something after rotation:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
 // do something after rotation
}

Reference:

http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instm/UIViewController/willRotateToInterfaceOrientation:duration:

Upvotes: 31

Bourne
Bourne

Reputation: 10312

willRotateToInterfaceOrientation: is the method you are possibly looking at. Read up on that one.

Upvotes: 0

Related Questions